/*-window.onload = function () { //Si no se usa protorype
	preparaModal();
}-*/
// Event.observe(window, 'load', preparaModal);//Si se usa prototype

Event.observe(window, 'load', function(){
	preparaModal();
	rolloverIcono();
})

var ieVer=0;
if(/compatible; MSIE 6.0/i.test(navigator.userAgent)) {var ieVer=6;}
if(/compatible; MSIE 7.0/i.test(navigator.userAgent)) {var ieVer=7;}
if(/compatible; MSIE 8.0/i.test(navigator.userAgent)) {var ieVer=8;}
var xmlHttp;

function preparaModal () {
	overDIV = document.createElement('div');
		overDIV.id = "tiny-over";
		overDIV.style.display = "none";
	
	loadP = document.createElement('p');
		loadP.appendChild(document.createTextNode('Cargando...'));
		
		loadPImg = document.createElement('img');
		loadPImg.setAttribute('src','img/loading.gif');
		loadPImg.setAttribute('alt','Cargando ....');
		loadPImg.style.display="inline";
		loadP.appendChild(loadPImg);

		
		
		 
		
	loadDIV = document.createElement('div');
		loadDIV.id = "tiny-load";
		loadDIV.style.display = "none";
		loadDIV.appendChild(loadP);
	
	tboxDIV = document.createElement('div');
		tboxDIV.id = "tiny-tbox";
		tboxDIV.style.display = "none";
	
	document.getElementsByTagName('body')[0].appendChild(overDIV);
	//document.getElementsByTagName('body')[0].appendChild(loadDIV);
	document.getElementsByTagName('body')[0].appendChild(tboxDIV);
	
	
	preparaModal2();
}

function preparaModal2 () {
todoEnlace = document.getElementsByTagName('a');
	for (i=0;i<todoEnlace.length;i++) {
		if (todoEnlace[i].rel == "tiny-show") {
			todoEnlace[i].onclick = function () {
				urlEnlace = this.href;
				preparaModal3(urlEnlace);
				return false;
			}
		}
	}
}

function preparaModal3 (urlVentana) {
	
	if (ieVer>0 && ieVer<7) {
		
		alturaNavegador = document.documentElement.clientHeight;
		alturaNavegador = (alturaNavegador/100)*20;
		offset = alturaNavegador;
		altura = document.documentElement.scrollTop + offset;
		
		$('tiny-tbox').style.top = altura;
		//$('tiny-load').style.top = altura;
		
		$('tiny-over').style.height = document.body.scrollHeight;
		$('tiny-over').style.width = "100%";
		
		$('wrapper').style.zIndex = "1";
		$('tiny-tbox').style.zIndex = "1";
		$('tiny-over').style.zIndex = "1";
		$('tiny-over').style.filter = 'alpha(opacity=40)';
		
	}
	quitaElementos();
	
	if (ieVer == 0) {
			document.getElementById('tiny-over').style.zIndex = '2019';
			//document.getElementById('tiny-load').style.zIndex = '2020';
			document.getElementById('tiny-tbox').style.zIndex = '2021';
		}
	$('tiny-over').style.display = "block";
	//$('tiny-load').style.display = 'block';
	
	xmlHttp = createXMLHttpRequest();
	xmlHttp.open("GET", urlVentana);
	xmlHttp.onreadystatechange = onCompleteData;
	xmlHttp.send(null);
	
	return false;
}


function completaModal () {
			
		$('tiny-tbox').style.display = "block";
		$('tiny-over').onclick = cerrarVentana;
		

	
	//abre una nueva ventana modal
	enlacesModales = document.getElementById('tiny-tbox').getElementsByTagName('a');
	for (i=0;i<enlacesModales.length;i++) {
		if (enlacesModales[i].rel == 'tiny-show') {
			enlacesModales[i].onclick = function () {
				urlSegundaModal = this.href;
				cerrarVentana('parcial');
				preparaModal3(urlSegundaModal);

				return false;
			}
		}
	}
	
	// se adjunta boton cerrar
	var boton = document.createElement('input');
	boton.type = 'button';
	boton.value = 'Cerrar';
	Element.addClassName(boton, 'btngris');
	boton.observe('click', cerrarVentana);	
	var parrafoBotones = $$('p.postales_botones_vm')[0];
	parrafoBotones.insertBefore(boton, parrafoBotones.firstDescendant());
	
	// se elimina link volver
	var linkVolver = $('volver');
	if(linkVolver){
		parrafoBotones.removeChild(linkVolver);
	}
	
	
}
function cerrar_vent_modal () {
	cerrarVentana('completo');
	$$('#api div.capas')[0].style.display = '';
}
function cerrarVentana (modo) {
			if (modo != 'parcial') {$('tiny-over').style.display = "none";/*$('tiny-load').style.display = "none";*/}
			$('tiny-tbox').remove();
			
			tboxDIV = document.createElement('div');//este bloque esta repe, es mejor crear una funcion
			tboxDIV.id = "tiny-tbox";
			tboxDIV.style.display = "none";
			document.getElementsByTagName('body')[0].appendChild(tboxDIV);
			
			ponElementos();
			
			return false;
		}

function quitaElementos () {
	calendario2 = document.getElementById('bio_calendar');
	if (!calendario2 && ieVer>0 && ieVer<7) {
		ocSel = document.getElementsByTagName('select');
		for (i=0; i<ocSel.length; i++) {
				ocSel[i].style.visibility = "hidden";
		}
	}
	if (calendario2 && ieVer>0) {
		calendario2.style.display = 'none';
	}
	
	ocOb = document.getElementsByTagName('object');
		for (i=0; i<ocOb.length; i++) {
				ocOb[i].style.display = "none";
		}
	
	/*if ($('apiCapa-0')) {$('apiCapa-0').style.display = 'none';}
	if ($('apiCapa-1')) {$('apiCapa-1').style.display = 'none';}*/if ($('apiEnlace-2')) {$$('#api div.capas')[0].addClassName('oculto');}
}
function ponElementos () {
	calendario2 = document.getElementById('bio_calendar');
	if (!calendario2 && ieVer>0 && ieVer<7) {
		ocSel = document.getElementsByTagName('select');
		for (i=0; i<ocSel.length; i++) {
				ocSel[i].style.visibility = "";
		}
	}
	if (calendario2 && ieVer>0) {
		calendario2.style.display = 'block';
	}
	
	ocOb = document.getElementsByTagName('object');
		for (i=0; i<ocOb.length; i++) {
				ocOb[i].style.display = "";
		}
	
	/*if ($('apiCapa-0')) {$('apiCapa-0').style.display = '';}
	if ($('apiCapa-1')) {$('apiCapa-1').style.display = 'none';}
	if ($('apiCapa-2')) {$('apiCapa-2').style.display = 'none';}
	if ($('apiEnlace-0')) {$('apiEnlace-0').up().style.display = 'none';}
	if ($('apiEnlace-1')) {$('apiEnlace-1').up().style.display = '';}
	if ($('apiEnlace-2')) {$('apiEnlace-2').up().style.display = '';}*/
	if ($('apiEnlace-2')) {$$('#api div.capas')[0].removeClassName('oculto');}
	
	return false;
}

function onCompleteData() {
	if (xmlHttp.readyState == 4) {
		//new Insertion.Bottom('tiny-tbox', xmlHttp.responseText)
		$('tiny-tbox').innerHTML = xmlHttp.responseText;
		completaModal();
	}
}
function createXMLHttpRequest() {    
	if (window.ActiveXObject) {     
		return new ActiveXObject("Microsoft.XMLHTTP");  
	} else if (window.XMLHttpRequest) {     
		return new XMLHttpRequest();    
	}
}



function rolloverIcono(){
	var icon = $$('p.ampliar a[rel=tiny-show] img');
	if(!icon) return;
	icon.each(function(el){
		el.imgReposo = el.src;
		el.imgHover = el.imgReposo.substring(0, el.src.lastIndexOf('.')) + '_hover.gif';
		
		el.observe('mouseover', function(){
			el.src = el.imgHover;	
		})
		el.observe('mouseout', function(){
			el.src = el.imgReposo;										
		})
	})
}