var W3CDOM = (document.createElement && document.getElementsByTagName);

var e = {
	addEvent : function(obj, evType, fn, useCapture){
		if (obj.addEventListener){
			obj.addEventListener(evType, fn, useCapture);
			return true;
		}else if (obj.attachEvent){
			var r = obj.attachEvent("on"+evType, fn);
			return r;
		}else {
			return false;
		}
	}
}

document.getElementsByClassName = function(className, container){
   var data = tags = [];
   var obj = document.getElementById("wrapper");
   var node = aux = null;		
	if(container) node = (typeof(container) == "object") ? container : document.getElementById(container);
	else node = (obj) ? obj : document;		   
	aux = node.getElementsByTagName("*");		
	tags = (document.all) ? node.all : aux;	
   for(var i=0;i<tags.length;i++) { if(tags[i].className == className || tags[i].className.indexOf(className) != -1) data[data.length]=tags[i];}
   return data;
}

var siteMap = {
	setLinks:function(){
		var content = document.getElementsByClassName("contentMap", "wrapperPopup");		
		var listSectionsLinks = document.getElementsByTagName("h2");
		var listLinks = [];		
		// enlaces principales
		for(var i = 0; i < listSectionsLinks.length; i++){
			listSectionsLinks[i].getElementsByTagName("a")[0].onclick = siteMap.loadURL;
		}			
	},
	loadURL:function(){		
		window.opener.location.href = this.href;
		self.close();
		return false;
	}
}

var analytics = {
	
	load:function(){
		var htmlBody=document.getElementsByTagName("body")[0];
		var imgElement = document.createElement("img");
		imgElement.className = "imgAnalytics";
			if(sensorUri!=0){
				imgElement.src  = sensorUri;
				htmlBody.appendChild(imgElement);
			}
	
	}
}

var navigationCombos={
	validaCmb:function(id){
		var form = document.getElementById(id);		
		var cmb = form.getElementsByTagName("select")[0];
		var sel= cmb[cmb.selectedIndex].value;				
		if (sel=="-1"){
			cmb.style.backgroundColor = "#fdcd07";
			return false;
		}
		return true;
	},
	goCmb:function(id){
		navigationCombos.validaCmb(id);
		var form = document.getElementById(id);		
		var cmb = form.getElementsByTagName("select")[0];
		var action = form.action; 
		var sel= cmb[cmb.selectedIndex].value;		
		if(sel != -1){
			var selValues = sel.split("||");
			var url = selValues[0];
			var windowName = selValues[1];
			var windowProperties = selValues[2];
			var newWindow = selValues[3];
			if(newWindow.toUpperCase() == "NO"){				
				document.location.href = url;
			}else if ( (newWindow.toUpperCase() == "SI") && (windowProperties != "null")){
				if(arguments.length>1){ cartografia.init(url,windowName,windowProperties,arguments[1]); }
				else{popUp.openWindowUser(url, windowName, windowProperties);}
			}else if ( (newWindow.toUpperCase() == "SI") && (windowProperties == "null") ){
				if(arguments.length>1){ Cartografia.init(url,windowName,windowProperties,arguments[1]); }
				else{ popUp.openWindow(url,'yes','750','400',"auto"); }
			}						
		} 
	},
	goCmbAiportsModule:function(id){
		navigationCombos.validaCmb(id);
		var form = document.getElementById(id);		
		var cmb = form.getElementsByTagName("select")[0];
		var sel= cmb[cmb.selectedIndex].value;
		if(sel != -1){
			urllink = urlcontext+pagename+Language+assettype+sitename[sel];
			document.location.href = urllink;
		}
	}
}

var popUp = {
	winn : null, 	 
	openWindow : function (cual, scrol, wid, heig, windowName, atributos){ 
		var LeftP, TopP, wid, heig;
		var caracteristicas = "";
		LeftP = (screen.width) ? (screen.width-wid) / 2 : 0; 
		TopP = (screen.height) ? (screen.height-heig) / 2 : 0; 
		if(popUp.winn){ 
			if(!popUp.winn.closed) popUp.winn.close(); 
			popUp.winn = null; 
		}
		if(!windowName) windowName = "";
		if(atributos){
			caracteristicas = "Directories=yes, Location=yes , Menubar=yes, Status=yes, Titlebar=yes, Toolbar=yes,"
		}
		window.focus(); 
		popUp.winn=eval("open('"+cual+"','"+windowName+"','"+caracteristicas+"width="+wid+",height="+heig+",top="+TopP+",left="+LeftP+",scrollbars="+scrol+",resizable=yes')");  
	},
	openWindowUser : function (cual, windowName, properties){ 		
		popUp.winn=eval("open('"+cual+"','"+windowName+"','"+properties+"')");  
	},
	checkExternalLinks:function() {
		 if (!document.getElementsByTagName) return; 
		 var anchors = document.getElementsByTagName("a"); 		 
		 for(var i = 0; i < anchors.length; i++)
			   if (anchors[i].getAttribute("href") &&  anchors[i].getAttribute("rel") == "popup") 
			   		anchors[i].target = "_blank"; 		 
	},
	openParent:function(cual){
		window.opener.document.location.href = cual;
		self.close();
	},
	pintoHerramientas:function(){
		var layHerramientas = document.getElementById("headPopup");
		var listElement= document.createElement("ul");
		
		var liElementPrint = document.createElement("li");
		liElementPrint.className = "print";
		
		var lnkPrint = document.createElement("a");
		lnkPrint.setAttribute("href","#");
		lnkPrint.setAttribute("title",literal["Imprimir"]);
		lnkPrint.onclick = function(){
			window.print();
			return false;
		}
		lnkPrint.appendChild(document.createTextNode(literal["Imprimir"]));
		liElementPrint.appendChild(lnkPrint);
		

		var liElementClose = document.createElement("li");
		liElementClose.className = "close";
		liElementClose.id = "cerrar";

		var lnkClose = document.createElement("a");
		lnkClose.setAttribute("href","#");
		lnkClose.setAttribute("title",literal["Cerrar"]);
		lnkClose.onclick = function(){
			window.close();
			return false;
		}
		lnkClose.appendChild(document.createTextNode(literal["Cerrar"]));
		liElementClose.appendChild(lnkClose);
		
		listElement.appendChild(liElementPrint);
		listElement.appendChild(liElementClose);

		layHerramientas.appendChild(listElement);
	}

}

var load={
	existeId:function(cid){
		if(document.getElementById(cid)) return true;
		return false;
	},
	onlyThisElement:function(tag,obj){
		var newObj=[]
		for(var xx=0;xx<obj.childNodes.length;xx++){
			if(obj.childNodes[xx].tagName==tag.toUpperCase()){
				newObj[newObj.length]=obj.childNodes[xx]
			}
		}
		return newObj
	},
	existeClass:function(className){
		var content = document.getElementById("content");
		var existsClass = false;
		if(content){			
			if(content.className.indexOf(className) != -1) existsClass = true;
		}	
		return existsClass;		
	},
	existeName:function(cname){
		if(document.getElementsByName(cname)) return true;
		return false;
	},		
	setEvents:function(){
		popUp.checkExternalLinks();
		
		if(load.existeId("headPopup")) popUp.pintoHerramientas();
		
		if(load.existeId("wrapperPopup") && document.getElementsByClassName("contentMap", "wrapperPopup").length != 0) siteMap.setLinks();
		
		analytics.load();
	}
	
}
if(W3CDOM) e.addEvent(window, "load", load.setEvents, false);
