
var $fondos;
var imgFondos;


$(document).ready(function() {


	if ($("h2").length == 0){
		$("#menuSecundario li:first").addClass("lineaTop");
		$("#menuSecundario li:first").css("padding-top",10);
	}
	if ( $("#menuSecundario").outerHeight() + $("h2").outerHeight() < $("#menuPrincipal .selected").position().top){
		$("#menuSecundario").height($("#menuPrincipal .selected").position().top - $("h2").outerHeight() );
	}
	if ( $(".columna2").outerHeight() + $("h2").outerHeight() < $("#menuPrincipal .selected").position().top){
		$(".columna2").height($("#menuPrincipal .selected").position().top - $("h2").outerHeight() );
	}

	
	// Random image
	
	imgFondos = [		"fondo-1.jpg",
						"fondo-2.jpg",
						"fondo-3.jpg",
						"fondo-4.jpg",
						"fondo-5.jpg",
						"fondo-6.jpg"
						];
	imgFondosRandom = [	"fondo-1.jpg",
						"fondo-2.jpg",
						"fondo-3.jpg",
						"fondo-4.jpg",
						"fondo-5.jpg",
						"fondo-6.jpg",
						"fondo-7.jpg",
						"fondo-8.jpg",
						"fondo-9.jpg",
						"fondo-10.jpg",
						"fondo-11.jpg"
						];
	
	if ($("#home").length == 0){
		var r = Math.floor(Math.random() * imgFondosRandom.length);
		$("body").css("background","url(img/"+imgFondosRandom[r]+") #000 fixed center top no-repeat");
	} else {
		initHome();
	}
	
	
	// Se asigna el comportamiento a los links de galería de fotos
	
	if($('.galeria a').length > 0)
	{
		$('.galeria a').lightBox();
	}
	
	// Se asigna el comportamiento a los links a videos
	
	if($('.video').length > 0)
	{
		$(".video").click(function(){
			
			
			var src = $(this).attr("href");
			
			var contenido = "<object id='mediaPlayer' width='450' height='400' classid='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95' codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701' standby='Loading Microsoft Windows Media Player components...' type='application/x-oleobject'>";
			 contenido += "<param name='fileName' value='"+src+"'><param name='AutoSize' value='0'><param name='autoStart' value='true'><param name='showControls' value='true'><param name='ShowStatusBar' value='true'><param name='loop' value='0'><embed type='application/x-mplayer2'";
			contenido += "pluginspage='http://microsoft.com/windows/mediaplayer/en/download/' src='"+src+"' width='450' height='400' autostart='1' autosize='0' showcontrols='1' showtracker='1' showstatusbar='1' showdisplay='0' videoborder3d='0' loop='0'  /></embed></object>";
			contenido += "<div style='text-align:right'><a href='#' id='closeVideo'><img src='img/lightbox-btn-close.gif' alt='cerrar' /></a></div>";
			
			
			/*
			var contenido = '<OBJECT ID="MediaPlayer" WIDTH="192" HEIGHT="190" CLASSID="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" STANDBY="Loading Windows Media Player components..." TYPE="application/x-oleobject">';
			contenido += '<PARAM NAME="FileName" VALUE="'+src+'">';
			contenido += '<PARAM name="autostart" VALUE="true">';
			contenido += '<PARAM name="ShowControls" VALUE="true">';
			contenido += '<param name="ShowStatusBar" value="true">';
			contenido += '<PARAM name="ShowDisplay" VALUE="false">';
			contenido += '<EMBED TYPE="application/x-mplayer2" SRC="'+src+'" NAME="MediaPlayer" WIDTH="192" HEIGHT="190" ShowControls="1" ShowStatusBar="1" ShowDisplay="0" autostart="1"> </EMBED>';
			contenido += '</OBJECT>';
			*/
			
		$.modal(
			contenido,
				{
					minHeight:420,
					minWidth: 450,
					maxWidth: 450,
					overlayClose:true
				}
			);
			$("#closeVideo").click(function(){
				$.modal.close();
				return false;
			});
			return false;
		});
	}
	

});




function initHome() {


	$("#black").height( $(document).height() );
	$("#black").width( $(window).width() );
	
	
	loadFondo(0 , function(){
		$("#black").height( $(document).height() );
		$("body").css("background","url(img/"+imgFondos[0]+") #000 fixed center top no-repeat");
		$("#black").fadeTo(1000,1).fadeOut(4000, function(){
			secuencia(1);
		});
	});
	

	window.onresize = function(){
		$("#black").width( $(window).width() );
	}


}



function secuencia(i){
	loadFondo(i,function(){
		// espera
		$("#black").fadeTo(4000, 1, function(){
			// tapa
			$("#black").height( $(document).height() );
			$("#black").fadeIn(4000, function(){
				//asigna fondo
				$("body").css("background","url(img/"+imgFondos[i]+") #000 fixed center top no-repeat");
				//destapa
				$("#black").fadeOut(4000, function(){
					i++;
					if(i==imgFondos.length) {
						i=0;
					}
					// nueva secuencia
					secuencia(i);
				});
			});
		});

	});
}

function loadFondo(i, callback) {
	var img = new Image();
	$(img).load(function () {
		callback();
	}).error(function () {
		 alert("error");
	}).attr('src', "img/"+imgFondos[i]);
}


