/*----------------------------------------------------------------------------\
|                     AIP on CD navigation environment  1.0                   |
|-----------------------------------------------------------------------------|
|                          Created by Javier Fenoll                           |
|                            For AIS Spain - Aena                             |
|-----------------------------------------------------------------------------|
| Loading AIP estructure from XML to tree, loading chart type text, 	      |
| automatic menu creation, and other supported features.                      |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| This  software is  available under the  GPL licenses  mentioned below.      |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| GPL - The GNU General Public License    http://www.gnu.org/licenses/gpl.txt |
| Permits anyone the right to use and modify the software without limitations |
| as long as proper  credits are given  and the original  and modified source |
| code are included. Requires  that the final product, software derivate from |
| the original  source or any  software  utilizing a GPL  component, such  as |
| this, is also licensed under the GPL license.                               |
|-----------------------------------------------------------------------------|
| Dependencies:    xtree.js (under the GLP license                            |
|                  hashtable.js						      |
|                  xmlextras.js						      |
|----------------------------------------------------------------------------*/

function LoadChartTypes(sFile) {
	
	var xmlChartTypes = XmlDocument.create();
	xmlChartTypes.async = false;
	
	xmlChartTypes.load(sFile);
	
	LoadChartTypesElements(xmlChartTypes,chartTypes);	
	
	
}

function LoadChartTypesElements(node,chartTypes) {
	
	
	
	if (node == null)
		return "";
	
	if (node.nodeType == 1) {
		
		var id = "";		
		var desc = "";
		
		var attrs = node.attributes;
		for (var i = 0; i < attrs.length; i++) { 			
			
			if (attrs[i].nodeName == "id")
				id = attrs[i].nodeValue.toUpperCase();
			
			if (attrs[i].nodeName == "desc")
				desc = attrs[i].nodeValue;
		}
		
		Loading(0, desc);
				
		chartTypes.put(id,desc);
						
	}
	var cs = node.childNodes;
	for (var i = 0; i < cs.length; i++)
		LoadChartTypesElements(cs[i],chartTypes);
}


function TreeLoadAIPFile(sFile) {

	var xmlAIP = XmlDocument.create();
	xmlAIP.async = false;		
	xmlAIP.load(sFile);
	
	RootAip = new TreeAipElement();
	RootAip.parentNode = treeAIP;
	RootAip.node = xmlAIP;
	RootAip.loadChilds();
			
}

function TreeAipElement() {

	var parentNode;
	var childs;
	var node;
	this.loadChilds = TreeLoadAIPElements;
	
}


function TreeLoadAIPElements() {

	var nextParentNode = null;
	

	if (this.node == null)
		return "";
	
	if (this.node.nodeType == 1) {
		
		var id = "";
		var link = "";
		var title = "";
		var title_lang = "";
		var linkextra = "";
		var tipoAer = false;
		var target = "IFRMCONTENT";
		var wef = "";
		var valid=""
		
		
		// busco los atributos que me interesa y los cargo
		var attrs = this.node.attributes;
		for (var i = 0; i < attrs.length; i++) { 			
			
			
			
			if (attrs[i].nodeName == "name") {
				id = attrs[i].nodeValue;
				// un poco cutre pero permite cambiar los caracteres 
				// de idioma en el nombre
				if (idioma == "es") 
					id = id.replace("_TO_","_A_");
					id = id.replace("_to_","_a_");
				}
			
			if (attrs[i].nodeName == "link") {
				link = "AIP/" + attrs[i].nodeValue;
				target = "AIP_PDF";
				}
			
			if (attrs[i].nodeName == "linkextra")
				linkextra = "AIP/" + attrs[i].nodeValue;
				
			if (attrs[i].nodeName == "title_" + idioma )
				title_lang = attrs[i].nodeValue;
			
			if (attrs[i].nodeName == "title"  )
				title = attrs[i].nodeValue;
			
			if (attrs[i].nodeName == "wef"  )
				wef = attrs[i].nodeValue;
			
			if (attrs[i].nodeName == "valid"  )
				valid = attrs[i].nodeValue;
			
			if (attrs[i].nodeName == "type") {
				if (attrs[i].nodeValue.toUpperCase() == 'AER' )
					tipoAer = true;
				}
			
			}

		// Menos en los aerodromos todos van en mayuscula
		if ( ! tipoAer) id = id.toUpperCase();
			
		if (wef != "") {
			
			// pongo la wef en página principal
			document.getElementById("WEF").innerHTML = 
				document.getElementById("vTexts_wefTxt").innerHTML + wef;
			
			// wef in DD/MM/YYYY
			var now = new Date();
			var today = new Date(now.getYear(),now.getMonth(),now.getDate());
			var wefDate = new Date(wef.substring(6,10),wef.substring(3,5)-1,wef.substring(0,2));
			var difference = today.getTime() - wefDate.getTime();				
			if (difference < 0) alert(document.getElementById("vTexts_wef").innerHTML + " : " + wef);
			
		}
		/*
		if (valid != "") {
			
			// valid in DD/MM/YYYY
			var now = new Date();
			var today = new Date(now.getYear(),now.getMonth(),now.getDate());
			var wefDate = new Date(valid.substring(6,10),valid.substring(3,5)-1,valid.substring(0,2));
			var difference = today.getTime() - wefDate.getTime();			
			if (difference > 0) alert(document.getElementById("vTexts_valid").innerHTML);
			
		}
		*/
		// si no hay cadena del idioma selecciona la de defecto
		if (title_lang == "") title_lang = title;	
		
		// si viene sin padre en un root nuevo
		if (this.parentNode==null && this.node.nodeName == "DOC") {			
			treeAIP[indexTreeAIP] = new WebFXTree(id,link);			
			nextParentNode = treeAIP[indexTreeAIP];			
			treeAIP[indexTreeAIP].target = target;
			treeAIP[indexTreeAIP].title = title_lang;
			treeAIP[indexTreeAIP].onclick="treeClickNode();"
			treeAIP[indexTreeAIP].icon="support/treeimg/aip_c.png";
			treeAIP[indexTreeAIP].openIcon="support/treeimg/aip.png";
			treeAIP[indexTreeAIP].linkextra = linkextra;
			indexTreeAIP++;
			}			
		// si por lo menos tenia id lo cargo en el arbol
		else if (id != ""){			
			nextParentNode = this.parentNode.add(new WebFXTreeItem( id,link ));			
			nextParentNode.target = target;
			nextParentNode.title = title_lang;
			nextParentNode.onclick="treeClickNode();"
			nextParentNode.linkextra = linkextra;	
			nextParentNode.tipoAer = tipoAer;	
			}			
		
		}
	else {
		nextParentNode = this.parentNode;
	}
			
	var cs = this.node.childNodes;
	
	
		
	for (var i = 0; i < cs.length; i++) {
				
		var ElemAip = new TreeAipElement();
		ElemAip.parentNode = nextParentNode;
		ElemAip.node = cs[i];
		ElemAip.loadChilds();
				
	}
	
	
		
}

// variables globales
var treeAIP;
var bTreeShow;
var indexTreeAIP;
var chartTypes;
var sTree;
// guarda la ventana con copntenido AIP
var wAIP_PDF = null;
var idioma = ""

function LongLoad() {
	
	
	TreeLoadAIPFile("AIP/aip.xml");	
		
	LoadChartTypes("language/" + idioma + "/chartsText.xml");
		
	sTree = "";
	
	Loading(30,document.getElementById("vTexts_loading30").innerHTML);
	window.setTimeout( "LongLoad2()"  , 50 );
	
}

function LongLoad2() {

	
	
	for (var i = 0;i<indexTreeAIP;i++)
		sTree = sTree + treeAIP[i] ;
	
	
	Loading(60,document.getElementById("vTexts_loading60").innerHTML);
	window.setTimeout( "LongLoad3()"  , 50 );
	
	
	}	
	
function LongLoad3() {
		
	document.getElementById("TREE").innerHTML = sTree;
	
	
	bTreeShow = false;
	treeExpand(false);
	
	Loading(100,"");
	document.getElementById("ALL").className = "NONE";
	
	
	
}

function Loading(percentage, message) {	
	if (percentage >= 100)
		document.getElementById("LOADING").innerHTML = "";
	else
		document.getElementById("LOADING").innerHTML = 
			"<p>" + message + "</p>"
			+ "<p><img src='support/img/loading" + percentage + ".gif' /></p>" 
			+ "<p>" + document.getElementById("vTexts_wait").innerHTML + "</p>";
	
	
}

function Load() {
	treeAIP = new Array(4);
	indexTreeAIP = 0;
	chartTypes = new Hashtable();
	
	idioma = document.getElementById("vTexts_lang").innerHTML;
	
	Loading(0,document.getElementById("vTexts_loading").innerHTML);
	
	
	
	window.setTimeout("LongLoad()", 50);
		
	

}

function treeClickNode(nodoSel){
			
	
	//primero oculto esto por si estaba visible
	document.getElementById("IFRMAUTOCONTENTEXTRA").className="HIDE";
	
	// si llega sin nodo es el que esta seleccionado
	if (nodoSel== null) {
		// esto selecciona bien aunque tenga mas de un arbol
		nodoSel = treeAIP[0].getSelected();
		}
		
	//si el elemnto no tiene link hago lo de autocontent		
	if ( nodoSel.action == "javascript:void(0);") {
		
		autocontent(nodoSel.id,0);		
		wAIP_PDF = null;
		}
	else {	
		// para enganchar la ventana
		wAIP_PDF = open("","AIP_PDF","width=400,height=400,resizable=yes");				
				
	}
		
	// el tema de los textos de location
	// ademas busco el padre más cercano sin link y hago autocontent
	var s= "";
	var doAuto = true;
	for (;;) {
		
		if (nodoSel.action == "javascript:void(0);" && doAuto) {
			autocontent(nodoSel.id,0);
			// solo lo hago con el primero
			doAuto = false;
		}
		
		s = "<a href='" + nodoSel.action +  "' target = '" + nodoSel.target + "' onclick='locationControl(\"" + nodoSel.id + "\");' >" + nodoSel.text + "</a>    " + s;
		nodoSel = nodoSel.parentNode;
		if (nodoSel == null) break;
		s = " / " + s;
		}
	s = "<a href='language/" + idioma + "/inicio.html' target='IFRMCONTENT' onclick='locationControl(0)'>" + document.getElementById("vTexts_start").innerHTML + "</a> / " + s;		
	document.getElementById("LOCATION").innerHTML = s; 
	
	DetalleFocus();
	
	}

function DetalleFocus() {	
	if (wAIP_PDF != null) window['wAIP_PDF'].focus();		
	}
	
function treeControl() {
	
	if (bTreeShow) {
		document.getElementById("IFRMCONTENT").style.left="30%";
		document.getElementById("IFRMCONTENT").style.width="69%";
		document.getElementById("IFRMAUTOCONTENT").style.left="30%";
		document.getElementById("IFRMAUTOCONTENT").style.width="69%";
		document.getElementById("IFRMAUTOCONTENTEXTRA").style.left="30%";
		document.getElementById("IFRMAUTOCONTENTEXTRA").style.width="69%";
		
		document.getElementById("HTreeControl").value=document.getElementById("HTreeControl").value1;
		
		document.getElementById("TREEBACKGROUND").style.zIndex=2;
		document.getElementById("TREE").style.zIndex=2;
		document.getElementById("TREECONTROL_HS").style.zIndex=2;
		document.getElementById("TREECONTROL_HSBACKGROUND").style.zIndex=2;
		
		bTreeShow = false;
		}
	else {	
		document.getElementById("IFRMCONTENT").style.left="1%";
		document.getElementById("IFRMCONTENT").style.width="98%";
		document.getElementById("IFRMAUTOCONTENT").style.left="1%";
		document.getElementById("IFRMAUTOCONTENT").style.width="98%";
		document.getElementById("IFRMAUTOCONTENTEXTRA").style.left="1%";
		document.getElementById("IFRMAUTOCONTENTEXTRA").style.width="98%";
		
		
		document.getElementById("HTreeControl").value=document.getElementById("HTreeControl").value2;
		
		document.getElementById("TREEBACKGROUND").style.zIndex=-1;
		document.getElementById("TREE").style.zIndex=-1;
		document.getElementById("TREECONTROL_HS").style.zIndex=-1;
		document.getElementById("TREECONTROL_HSBACKGROUND").style.zIndex=-1;
		
		bTreeShow = true;
		}

	}

	
function locationControl(cual) {
	
	var nodoSel = null;
	
	// busco el elemento nodo en todos los arboles
	for (var i = 0; i< indexTreeAIP ; i++) {
		nodoSel = treeAIP[i].getNode(cual,"id")
		if (nodoSel != null ) break;
		}
	
	// si no esta busco por text
	if ( nodoSel == null ) {
		for (var i = 0; i< indexTreeAIP ; i++) {
			nodoSel = treeAIP[i].getNode(cual,"text")
			if (nodoSel != null ) break;
			}
		
	}
	

	
	if (nodoSel != null ) {
		nodoSel.viewNode();
		// entonces lo selecciono y reescribo location de forma adecuada		
		nodoSel.select();
		treeClickNode(nodoSel)				
		}
	else {
		showIfrmContent();
		document.getElementById("LOCATION").innerHTML = "..."; 
		}
		
	
	
}

function treeExpand(state) {

	if (state) { 
		for(var i = 0; i<indexTreeAIP;i++)
			treeAIP[i].expandAll();
		}
	else {
		for(var i = 0; i<indexTreeAIP;i++) {			
			treeAIP[i].collapseAll();
			}
		}	
	
}

function botonAct(elBoton, state) {

	if (state == true) {
		elBoton.className = "botonHi"
		}
	else {
		elBoton.className = "botonLo"
		}

}


// si order = 1 es por titulo si 0 es por codigo
function autocontent(cual, order) {
	
	var LastCod = "";
	var nodoSel = null;
	
	for (var i = 0; i< indexTreeAIP ; i++) {
		nodoSel = treeAIP[i].getNode(cual,"id")
		if (nodoSel != null ) break;
		}
		
	if (nodoSel == null ) return;
	
	// no tiene accion, hago autocontent
	document.getElementById("IFRMCONTENT").className="HIDE";
	document.getElementById("IFRMAUTOCONTENT").className="IFRMCONTENT";
	
	var s="<p>";
	s += "<h1>" + nodoSel.text + " <a class='minor'>" + nodoSel.title  + "</a>";
	
	// ahora cargo el array de los hijos
	var aChilds = new Array;
	for (var j = 0; j < nodoSel.childNodes.length; j++){
		aChilds[j] = nodoSel.childNodes[j];
		}
		
	// solo ordena en AD2
	if (  nodoSel.text == "AD_2") {

		var ordertipe = document.getElementById("vTexts_order1").innerHTML;
		var ordervalue = 1;
		if (order == 1) {
			ordertipe = document.getElementById("vTexts_order2").innerHTML;
			var ordervalue = 0;
			}
		// lo que realmente ordena si no se queda como en el arbol
		if (order == 1 )
			aChilds.sort(compareNodesTitle);
		
		s += "<br /><a href='javascript:void(0);' onclick='autocontent(\"" + nodoSel.id  + "\"," + ordervalue + ");'>" + ordertipe + "</a>";
		
	}
	
	// si tiene linkextra pongo aqui el control para verlo o no
	//var linkextraText= "(ver contenido extra)";	
	//if (nodoSel.linkextra != "")
	//	s += "<a target='IFRMAUTOCONTENTEXTRA' href='" + nodoSel.linkextra + "' onclick='linkextraShow(true);'>" + linkextraText + "</a>";
	
	s += "</p></h1>";
	
	if (nodoSel.tipoAer)
		s = s + "<table class='AerMenutable' >";
	else
		s = s + "<table class='menutable' >";
	
	var theCod = "";
	var indexTheCod = 0;
	
	for (var i=0,j=0  ; i< aChilds.length ; i++, j++) { 	
		
		// si es aerodromo y cambia el codigo, es que es otra clase de carta,
		// y pongo el título
		
		indexTheCod = aChilds[i].text.indexOf("_",0);
		
		if ( indexTheCod > 0) 
			theCod = aChilds[i].text.substring(0, indexTheCod  );
		else
			theCod = aChilds[i].text;				
		
		if (nodoSel.tipoAer && theCod  != LastCod) {
			
			LastCod = theCod;
			
			if ( chartTypes.containsKey(LastCod)  ) {
			
				// para que la primera linea de cada tipo de carta sea del mismo tipo
				j=0;
				s = s + "</table>";
				s = s + "<p class='tableTitle'>" + chartTypes.get(LastCod) + "</p>";
				s = s + "<table class='AerMenutable' >";
				
			}
		}
		
		if (j%2)
			s = s + "<tr>";
		else
			s = s + "<tr class='ODD'>";
		
		// si es un aerodromo no pongo el codigo de las cartas
		if (! nodoSel.tipoAer) {
			s = s + "<td class='COD'>" ;
			s = s + "<a href='" + aChilds[i].action +  "' target = '" + aChilds[i].target + "' onclick='locationControl(\"" + aChilds[i].id + "\");' >";
			s = s + aChilds[i].text ;
			s = s + "</a>" ;
			s = s + "</td>";
		}
				
		s = s + "<td class='TIT'>";
		s = s + "<a href='" + aChilds[i].action +  "' target = '" + aChilds[i].target + "' onclick='locationControl(\"" + aChilds[i].id + "\");' >";
		s = s + aChilds[i].title ;
		s = s + "</a>" ;
		s = s + "</td>";
		s = s + "</tr>" ;
		
		
		}
	s = s + "</table>"
		
		
	document.getElementById("IFRMAUTOCONTENT").innerHTML = s; 	
	
}

function compareNodesTitle(node1, node2) {
	
	if (node1.title > node2.title) return 1;
	if (node1.title < node2.title) return -1;	
	return 0;
	
}

function compareNodesText(node1, node2) {
	
	if (node1.text > node2.text) return 1;
	if (node1.text < node2.text) return -1;	
	return 0;
	
}

function linkextraShow(show) {
	
	if (show)
		document.getElementById("IFRMAUTOCONTENTEXTRA").className="IFRMCONTENT"
	else
		document.getElementById("IFRMAUTOCONTENTEXTRA").className="HIDE"
}

function setElementSelect(code) {
	
	var nodoSel = null;
	
	// busca el nodo por su text y hace treenodeclick	
	for (var i = 0; i< indexTreeAIP ; i++) {
		nodoSel = treeAIP[i].getNode(code,"text")
		if (nodoSel != null ) break;
		}
		
	if (nodoSel == null ) return;
	
	nodoSel.viewNode();
	nodoSel.select();
	treeClickNode(nodoSel);
	
}

function showIfrmContent() {
	document.getElementById("IFRMAUTOCONTENTEXTRA").className="HIDE";	
	document.getElementById("IFRMAUTOCONTENT").className="HIDE";
	
	document.getElementById("IFRMCONTENT").className="IFRMCONTENT";
		
}

function showFind() {

	//primero oculto esto por si estaba visible
	showIfrmContent();	
	document.getElementById("IFRMCONTENT_IFRAME").src = "../Help/language/" + idioma + "/Busq.html" ;	
	
}
