var xmlHttp;

function GetXmlHttpObject(){
	xmlHttp = null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch(e){
		//Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e){
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

var G_Intervalo_Img_Inicio;
var G_I_Img = 0;
function ajustarTamanno(numPrimeraImg){
	
	
	if(G_Intervalo_Img_Inicio){ clearInterval(G_Intervalo_Img_Inicio); }
	if(G_Intervalo_Fundido){ clearInterval(G_Intervalo_Fundido); }
	
	if(document.getElementById('divPie')){
		document.getElementById('divBackGreen').style.height = document.getElementById('divGeneral').offsetHeight;
		
		if(document.getElementById('divPie').offsetTop < 229){ // 550 - 120 (desde donde empieza)
			document.getElementById('divPie').style.position = 'absolute';
			document.getElementById('divPie').style.top = '229px';
			
			if(document.getElementById('divGeneral').offsetHeight < 600){ // A 550 (top) y 50 (bottom)
				var newTopDireccion = document.getElementById('divGeneral').offsetHeight - 79; // 50 (bottom) + 29 (tamaño div)
				var txtNewTopDireccion = newTopDireccion + 'px';
				
				document.getElementById('divDireccion').style.top = txtNewTopDireccion;
			}
			
			var arrOffsetHeight = new Array(
					document.getElementById('divCamino').offsetHeight,
					document.getElementById('divTitulo2').offsetHeight,
					document.getElementById('divAvance').offsetHeight,
					document.getElementById('divTexto').offsetHeight,
					document.getElementById('divPie').offsetHeight,
					document.getElementById('divTexto').style.top.replace('px', '')
			);
			var newHeight = 250;
	
			for(key in arrOffsetHeight){
				newHeight = parseInt(newHeight) + parseInt(arrOffsetHeight[key]);
			}
	
			if(newHeight > document.getElementById('divGeneral').offsetHeight){
				var txtNewHeight = newHeight + 'px';
				document.getElementById('divGeneral').style.height = txtNewHeight;
			}
		}
		
		if(document.getElementById('divBlanco')){
			var txtNewWidth = (document.getElementById('divGeneral').offsetWidth - 752) + 'px'
			var txtNewHeight2 = (document.getElementById('divGeneral').offsetHeight - 200) + 'px'
			document.getElementById('divBlanco').style.width = txtNewWidth;
			document.getElementById('divBlanco').style.height = txtNewHeight2;
			document.getElementById('divDireccion').style.display = 'none';
		}
	} else {
		document.getElementById('divBackGreen').style.height = '600px';
		
		G_I_Img = numPrimeraImg;
		G_Intervalo_Img_Inicio = setInterval("cambiarImagenInicio();", 4000);
	}
}

var G_Intervalo_Fundido;
var G_Opacity = 100;
function cambiarImagenInicio(){
	G_Opacity = 100;
	G_Intervalo_Fundido = setInterval("fundido()", 50);
}

function fundido(){
	G_Opacity -= 10;
	var txtOpacity = Math.abs(G_Opacity);
	
	document.getElementById('imgFondoInicio').style.opacity = txtOpacity / 100;
	document.getElementById('imgFondoInicio').style.filter = 'alpha(opacity=' + txtOpacity + ')';
	
	if(G_Opacity == 0){
		document.getElementById('imgFondoInicio').src = '';
		if(G_I_Img < 9){ G_I_Img++; } else { G_I_Img = 1; }
		var numImg = G_I_Img;
		var img = 'img_home_0' + numImg;
		document.getElementById('imgFondoInicio').src = 'imagenes/inicio/' + img + '.jpg';
	} else if(G_Opacity <= -100){
		clearInterval(G_Intervalo_Fundido);
	}
}


function activarAudio(sw, recienLlegado){
	var img = document.getElementById('imgAudio');
	var audio = '';

	if(((!sw) && (recienLlegado == 'Y')) || ((sw) && (img.src.indexOf('-off') > -1))){
		// Si es la primera página que se ve (recién llegado) o si lo conecta el usuario
		// Encender
		audio = 'on';
		popup(1);
	} else if((sw) && (img.src.indexOf('-on') > -1)){
		// Si lo desconecta el usuario
		// Apagar
		audio = 'off';
		popup(0);
	}
}

function popup(sw){
	var img = document.getElementById('imgAudio');
	var opciones="toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=341, height=104, top=10000, left=10000";
	var audio;
	
	if(sw == 1){
		// Encendido desde web
		img.src = img.src.replace('-off', '-on');
		var popup = window.open('audio-on.php', 'popupAudio', opciones);
		popup.blur();
		audio = 'on';
	} else if(sw == 0){
		// Apagado desde web
		img.src = img.src.replace('-on', '-off');
		var popup = window.open('audio-off.php', 'popupAudio', opciones);
		popup.blur();
		audio = 'off';
	} else if(sw == 2){
		// Cerrado desde ventana
		img.src = img.src.replace('-on', '-off');
		audio = 'off';
	}
	
	aModificarAudio(audio);
}
