var pos1=0;
var pos2=0;
var pos3=0;

function resizeme() { }


window.onload=function() {
	cargarEventos();
	cargarBanners();
	
	var so = new SWFObject("flash/flash_principal.swf", "Presentacion", "666", "118", "8", "#FFFFFF");
	so.write("contFlash");
	
	
	if ($('listadoPoblacionesMapa')) {
		var jSelect = new JSelect('listadoPoblacionesMapa', {
							width: 260,
							height: 120,
							onmouseover: mostraPunt,
							onshowlist: desactivaMapa,
							onhidelist: activaMapa
						});
	}
	
	if ($('listadoPoblaciones')) {
		var jSelect = new JSelect('listadoPoblaciones', {
							width: 260,
							height: 197
						});
	}
	
	if ($('horizontal_carousel').childNodes[1].firstChild.childNodes.length>0) {
		new UI.Carousel("horizontal_carousel");
	} 
	
	var arrOver=$$('#overlay');
	
	if (arrOver) {
		for (var k=0;k<arrOver.length;k++) {
			arrOver[k].style.display='none';
			
			if (k>0) {
				arrOver[k].parentNode.removeChild(arrOver[k]);
			}
		}
	}
	
	var arrLight=$$('#lightbox');
	
	if (arrLight) {
		for (var k=0;k<arrLight.length;k++) {
			arrLight[k].style.display='none';
			
			if (k>0) {
				arrLight[k].parentNode.removeChild(arrLight[k]);
			}
		}
	}
}

function iniciaMapa() {
	$('imgMap').style.display='block';
	TJPzoomswitch($('imgMap'));	
}

function mostraPunt() {
	var x=this.getAttribute('x');
	var y=this.getAttribute('y');
	
	if (!($('mapSpot'))) {
		var d=document.createElement('div');
		d.id="mapSpot";
		var i=document.createElement('img');
		i.src="img/spot.gif";
		i.style.cursor="pointer";
		
		d.appendChild(i);
		$('mapCont').insertBefore(d,$('imgMap'));
	} else {
		d=$('mapSpot');
		i=$('mapSpot').firstChild;
	}
	
	d.style.position="absolute";
	d.style.width='20px';
	d.style.height='20px';
	d.style.marginTop=y+"px";
	d.style.marginLeft=x+"px";
	i.setAttribute('alt', this.firstChild.firstChild.nodeValue);
	i.setAttribute('title', this.firstChild.firstChild.nodeValue);
	i.setAttribute('hrf', this.href);
	i.onclick=function() {
		window.location=this.getAttribute('hrf');
	}
}

function activaMapa() {
	if ($('mapSpot')) {
		$('mapSpot').parentNode.removeChild($('mapSpot'));
	}
	TJPzoomswitch($('imgMap'));
}

function desactivaMapa() {
	TJPzoomswitch($('imgMap'));
}

function cargarEventos() {
	var url="../xml/cargarEvento.php";
	var params="fecha="+$('fIni').value;
	var ajax=new Ajax.Updater ( $('evento'), url, {
		method: 'get',
		parameters: params
	});
}

function cargarBanners() {
	var url="../xml/listaBanners.php";
	var ajax=new Ajax.Request(url, {
		method: 'get',
		onComplete: function(reply) {
			var nodePart=reply.responseXML.selectNodes("//banners/tipos");
			
			for (var k=0;k<nodePart.length;k++) {
				cargarBanners2(nodePart[k]);
			}
			
			setTimeout("posaBanner1();",4000);
		}
	});
}

function cargarBanners2(nodePart) {
	
	if (nodePart.getAttribute("cod")=="1") {
		d=$('megaBanner');
		
		for (var k=0;k<nodePart.childNodes.length;k++) {
			var a1=document.createElement('a');
			a1.target="blank";
			if (nodePart.childNodes[k].firstChild) {
				a1.href=nodePart.childNodes[k].firstChild.nodeValue;
			} else {
				a1.href="#";
			}
			
			if (nodePart.childNodes[k].getAttribute("tipo")!="swf") {
				var img1=document.createElement("img");
				img1.src="../../admin/html/uploads/banners/"+nodePart.childNodes[k].getAttribute("tip")+"/"+nodePart.childNodes[k].getAttribute("cod")+"."+nodePart.childNodes[k].getAttribute("tipo");
				img1.style.display='none';
				a1.appendChild(img1);
				d.appendChild(a1);
			} else {
				var dSwf=new Element('div', { 'id':'swf'+nodePart.childNodes[k].getAttribute("cod") });
				a1.appendChild(dSwf);
				d.appendChild(a1);
				var so = new SWFObject("../../admin/html/uploads/banners/"+nodePart.childNodes[k].getAttribute("tip")+"/"+nodePart.childNodes[k].getAttribute("cod")+"."+nodePart.childNodes[k].getAttribute("tipo"), "flash"+nodePart.childNodes[k].getAttribute("cod"), "728", "90", "8", "#FFFFFF");
				so.write(dSwf.id);
				dSwf.style.display='none';
			}
		}
		if (d.firstChild) {
			d.firstChild.firstChild.style.display='block';
		}
	}
	
	if (nodePart.getAttribute("cod")=="2") {
		d=$('botonSuperior');
		
		for (var k=0;k<nodePart.childNodes.length;k++) {
			var a2=document.createElement('a');
			a2.target="blank";
			if (nodePart.childNodes[k].firstChild) {
				a2.href=nodePart.childNodes[k].firstChild.nodeValue;
			} else {
				a2.href="#";
			}
			
			if (nodePart.childNodes[k].getAttribute("tipo")!="swf") {
				var img2=document.createElement("img");
				img2.src="../../admin/html/uploads/banners/"+nodePart.childNodes[k].getAttribute("tip")+"/"+nodePart.childNodes[k].getAttribute("cod")+"."+nodePart.childNodes[k].getAttribute("tipo");
				img2.style.display='none';
				a2.appendChild(img2);
				d.appendChild(a2);
			} else {
				var dSwf=new Element('div', { 'id':'swf'+nodePart.childNodes[k].getAttribute("cod") });
				a2.appendChild(dSwf);
				d.appendChild(a2);
				var so = new SWFObject("../../admin/html/uploads/banners/"+nodePart.childNodes[k].getAttribute("tip")+"/"+nodePart.childNodes[k].getAttribute("cod")+"."+nodePart.childNodes[k].getAttribute("tipo"), "flash"+nodePart.childNodes[k].getAttribute("cod"), "234", "90", "8", "#FFFFFF");
				so.write(dSwf.id);
				dSwf.style.display='none';
			}
		}
		if (d.firstChild) {
			d.firstChild.firstChild.style.display='block';
		}
		
	}
	
	if (nodePart.getAttribute("cod")=="3") {
		d=$('contRobaPaginas');
		
		for (var k=0;k<nodePart.childNodes.length;k++) {
			var a3=document.createElement('a');
			a3.target="blank";
			if (nodePart.childNodes[k].firstChild) {
				a3.href=nodePart.childNodes[k].firstChild.nodeValue;
			} else {
				a3.href="#";
			}
			
			if (nodePart.childNodes[k].getAttribute("tipo")!="swf") {
				var img3=document.createElement("img");
				img3.src="../../admin/html/uploads/banners/"+nodePart.childNodes[k].getAttribute("tip")+"/"+nodePart.childNodes[k].getAttribute("cod")+"."+nodePart.childNodes[k].getAttribute("tipo");
				img3.style.display='none';
				a3.appendChild(img3);
				d.appendChild(a3);
			} else {
				var dSwf=new Element('div', { 'id':'swf'+nodePart.childNodes[k].getAttribute("cod") });
				a3.appendChild(dSwf);
				d.appendChild(a3);
				var so = new SWFObject("../../admin/html/uploads/banners/"+nodePart.childNodes[k].getAttribute("tip")+"/"+nodePart.childNodes[k].getAttribute("cod")+"."+nodePart.childNodes[k].getAttribute("tipo"), "flash"+nodePart.childNodes[k].getAttribute("cod"), "300", "300", "8", "#FFFFFF");
				so.write(dSwf.id);
				dSwf.style.display='none';
			}
		}
		if (d.firstChild) {
			d.firstChild.firstChild.style.display='block';
		}
		
	}
}

function posaBanner1() {
	var d=$('megaBanner');
	
	if (d.childNodes[pos1]) {
		d.childNodes[pos1].firstChild.style.display='none';
		pos1++;
		
		if (pos1==d.childNodes.length) {
			pos1=0;
		}
		
		d.childNodes[pos1].firstChild.style.display='block';
	}
		
		posaBanner2();
		posaBanner3();
		setTimeout("posaBanner1();",20000);
}

function posaBanner2() {
	var d=$('botonSuperior');
	
	if (d.childNodes[pos2]) {
		d.childNodes[pos2].firstChild.style.display='none';
		pos2++;
		
		if (pos2==d.childNodes.length) {
			pos2=0;
		}
		
		d.childNodes[pos2].firstChild.style.display='block';
	}
}

function posaBanner3() {
	var d=$('contRobaPaginas');
	
	if (d.childNodes[pos3]) {
		d.childNodes[pos3].firstChild.style.display='none';
		pos3++;
		
		if (pos3==d.childNodes.length) {
			pos3=0;
		}
		
		d.childNodes[pos3].firstChild.style.display='block';
	}
}