// JavaScript Document
$("a").live("click",function(){
	return window.documentListo;//previene clicks, sin que este el document ready		  
});
window.documentListo = false;
$(function () {
	window.documentListo = true;
	//Imagenes con mapas en xhtml 1.1 validado y funcionando
	$("img[usemap]").each(function () {
		this.useMap="#"+$(this).attr("usemap");
	});
	$("map").each(function () {
		$(this).attr("name",this.id);								
	});
	//Seccion menu superior
	$("#menu > ul > li > ul").hide();
	$("#menu > ul > li").bind("mouseenter mousemove", function () {
		if($(this).children().eq(1).css("display")=="none"){
			$(this).children().eq(1).slideDown("fast", function () {
				var elUL = this;
				$("#menu > ul > li").children("ul").each(function () {
					if(this != elUL)$(this).hide();
				});
			});
		}
	}).bind("mouseleave",function () {
		$(this).children().eq(1).slideUp("normal");
	});
	//Seccion menu instituciones
	$("#enlacesInstituciones > ul > li > ul").hide().children().children().css("opacity", 0).hide();
	$("#enlacesInstituciones > ul > li").bind("mouseenter mousemove", function () {
		if($(this).children("ul").css("display")=="none"){
			$(this).children("ul").show("fast");
			//si es el mismo elemento del que estamos hablando, anular el hide;
			if(typeof(window.elElement) != "undefined" && window.elElement==this)window.clearTimeout(window.cerrarD);
		}
	}).bind("mouseleave", function () {
		//si no es el mismo elemento del que estamos hablando, hide pal elemento;
		if(typeof(window.elElement) != "undefined" && window.elElement!=this)$(window.elElement).children().hide("fast");//Ocultar el anterior
		window.elElement = this;
		window.cerrarD = window.setTimeout(function () {
			$(window.elElement).children().hide("fast");
		},500);
	}).children().children().bind("mouseenter mousemove", function () {
		if($(this).children().css("display")=="none"){
			var position = $(this).children().offset();
			$(this).siblings().children().hide();//ocultar todos
			$(this).children().show("fast",function () {
				$(this).fadeTo("fast", 1);
				if($(this).offset().left>=(850))$(this).animate({marginLeft: "-"+($(this).outerWidth()+3)+"px"});
			});
		}
	}).bind("mouseleave", function () {
		$(this).children().hide().fadeTo("slow", 0);
	});
	//Seccion menu oficinas
	$("#banderas > ul > li").children("ul").corner("round 5px").hide().fadeTo(1, 0);//Oculto cuando dom ready y redondeo
	$("#banderas > ul > li").bind("mouseenter mousemove", function (){
		if($(this).children("ul").css("display")== "none"){
			$("#banderas > ul > li").children("ul").hide().fadeTo(0, 0);//Oculto todos
			var elUL = $(this).children("ul").show().fadeTo("fast",1);
			elUL.css("margin-top", "-"+(elUL.outerHeight()+$(this).children("img").outerHeight()+13)+"px");//small position fix
		}
	}).parent().bind("mouseleave", function (){
		$(this).children().children("ul").fadeTo("normal", 0, function () {
			$(this).hide();
		});													  
	});
	//Imagenes cabecera
	//var fondos = ["Foto_apertura1.jpg","Foto_apertura2.jpg","Foto_apertura3.jpg","Foto_apertura4.jpg","Foto_apertura5.jpg","Foto_apertura6.jpg","Foto_apertura7.jpg","Foto_apertura8.jpg","Foto_apertura9.jpg"];
	fondos = ["Foto_aperturaPagina.jpg"];
	var fondo = 0;
	var rutaSlides = "/images/slides/";
	var cuenta = 0;
	var capas = ["#cabeceraHome","#cabeceraHome2"];
	var elIntervalo = 5000;//numero de milesimas de segundo entre cambios de fondo
	var transicion = 4000;//El numero de milesimas para el cambio de la opacidad
	//$("#laCabecera").css("opacity",1);//esto causa problemas con la transparencia en IE 7 y 8
	//precargar las imagenes
	for(var i=0;i<fondos.length;i++) {
		var imagen = new Image();
		//$(imagen).load(function () { console.log("cargada imagen: "+this.src)});
		imagen.src = rutaSlides+fondos[i];
	}
	function cambiarFondo() {
		cuenta++;
		var capa1 = capas[0];
		var capa2 = capas[1];
		if(cuenta%2==0) {
			capa2 = capas[0];
			capa1 = capas[1];
		}
		$(capa1).css("background-image","url("+rutaSlides+fondos[fondo]+")").animate({
			opacity: "1"
		},transicion);
		$(capa2).animate({
			opacity: "0"
		},transicion);
		fondo = typeof fondos[fondo+1] == "undefined" ? 0 : fondo+1;
	}
	$("#laCabecera").click(function(){document.location.href="http://www.latinoaustralia.com/feria-educativalae2010"}).css("cursor","pointer");
	cambiarFondo();
	window.setInterval(cambiarFondo,elIntervalo);
	//Mas cosas desplegables
	$("h2.abreme").css("cursor", "pointer").each(function (e) {
		var encontrado = false;
		var nEncontrado = 1;
		$(this).nextAll().each(function (i) {
			if(encontrado === false) {
				nEncontrado = i;
			}
			if(this.nodeName.toLowerCase() == "h2"){
				encontrado = true;	
			}
		}).slice(0,nEncontrado).wrapAll("<div class=\"bordeaditos\"></div>");
	});
	$(".bordeaditos").css("padding","15px").css("margin-top","12px").css("border","solid 1px #CCCCCC").corner("round 15px").hide();
	$(".bordeaditos").prev().bind("click",function (e) {
		$(this).css("background-image", "url(images/j_arrow_down.png)");
		var elem = $(this).next();
		if(elem.css("display")=="none"){
			elem.slideDown();
		}else{
			elem.slideUp();
			$(this).css("background-image", "url(images/j_arrow.png)");
		}
	});
	//Seccion Google Mapas
	var removeCopy = false;//Warning, dont change to true
	function removerCreditos() {
		var capaId = window.capaId;
		var hijos = $("#"+capaId).children();
		if(hijos.length <= 2){
			window.setTimeout(removerCreditos,200);
			return;
		}
		hijos.eq(1).remove();
		hijos.eq(2).remove();
	}
	window.cargaAjax=function () {
		$("#mapa_oficina").html($("#mapa_oficina").html()+' .');
	}
	function showGMap () {
		if(typeof google.maps.LatLng === "undefined" || typeof window.showMap === "undefined"){
			window.setTimeout(showGMap,200);
			return;
		}
		if(removeCopy)window.setTimeout(removerCreditos,1000);
		$("#mapa_oficina").html($("#mapa_oficina").html()+'<br />Cargando Mapa .');
		try{
			window.showMap(window.latitud,window.longitud,window.capaId,window.msgBox,window.latitud2,window.longitud2);
		}
		catch(e){
			window.showMap(window.latitud,window.longitud,window.capaId,window.msgBox);
		}
		$("#mapa_oficina").html($("#mapa_oficina").html()+'<br />Error cargando mapa.');
		window.clearInterval(intervalId);
	}
	window.getScript2 = function (src) {
		var s = document.createElement("script");
		s.type = "text/javascript";
		s.src = src;
		document.getElementsByTagName("head")[0].appendChild(s);
	}
	if(typeof window.latitud !== "undefined" && window.latitud != '' && (typeof window.mostrarMapa !== "undefined" && window.mostrarMapa !== false)) {
		$("#mapa_oficina").html($("#mapa_oficina").html()+' .');
		var intervalId = window.setInterval(window.cargaAjax,500);
		$("#mapa_oficina").html($("#mapa_oficina").html()+'<br />Conectando a Google .');
		$.getScript("ajaxproxy.php?url=http://maps.google.com/maps/api/js?sensor=false", function(){
		$("#mapa_oficina").html($("#mapa_oficina").html()+'<br />Conectando al API de Google Maps .');
			$.getScript("javascript/googleMaps.js", function(){
				$("#mapa_oficina").html($("#mapa_oficina").html()+'<br />Api Cargada');
				showGMap();
			});
		});
	}else{
		//$("#mapa_oficina").html("No se detecto posicion");	
		$("#mapa_oficina").remove();
	}
	/* para decodificar emails */
	$(".elEmail").each(function (){
		$(this).html(html_entity_decode($(this).html())).prev("span").remove();							 
	});
	/* Para abrir el chat */
	$("#chatOfc,#chat,#chatI,#chatV").bind("click", function () {
		window.abrirPopUp(window.laePersonaHref,640,480);		  
	}).css("cursor","pointer");
	/* Abre una popup con la url, ancho y alto dados, sin barras ni nada, y centrada*/
	window.abrirPopUp = function (url,ancho,alto) {
		ventana = window.open(url,'','resizable=yes,width='+ancho+',height='+alto);
		if(!ventana){
			alert("PopUp blocker detected");
		}else{
			x = (screen.width - ancho) / 2;
			y = (screen.height - alto) / 2;
			ventana.moveTo(x, y);
		}	
	}
	/* home */
	$("#enlacesHome").corner("round 5px");
	/* jQuery + UFO */
	var cuenta = 0;
	$("a.flash").each(function () {
		cuenta++;
		var src = this.href;
		var ancho = $(this).width();
		var alto = $(this).height();


		var id = 'flash'+cuenta;
		var estilos = this.style;
		/*for(laestilo in this.currentStyle){
			console.log(laestilo+'='+this.currentStyle[laestilo]);
		}*/
		if(typeof(window.getComputedStyle) != "undefined"){
			var estilos = window.getComputedStyle( this, null );
		}else{
			var estilos = this.currentStyle;
		}
		$(this).wrap("<div id='"+id+"'></div>");
		var FO = { movie:src, width:ancho, height:alto, majorversion:"6", build:"0", wmode:"opaque"};
		UFO.create(FO, id);
		var repite = 0;
		for(estilo in estilos){
			if(estilo=='opacity' || estilos[estilo] == 'opacity')repite++;
			if(repite>1)break;//Empieza a repetir con camelCase
			if(estilo == 'backgroundPositionX' || estilo == 'backgroundPositionY' || typeof(estilos[estilo]) !== "string"){
				continue;
			}
			try{
				var elEstilo = estilos.getPropertyValue(estilos[estilo]);
				if(typeof(elEstilo)!=="string")continue;//Por aqui en chrome a un object null
				//console.log(estilos[estilo]+'='+typeof(elEstilo)+"|");
			}
			catch(e){
				var elEstilo = $(this).css(estilo);	
				//console.log(estilo+'='+e);	
			}
			if(estilo!='length' && estilo!='parentRule' && typeof(elEstilo) == "string"){
				//console.log(estilo+'='+$("#"+id).css(estilo));
				try{
					if(!isNaN(estilo))estilo = estilos[estilo];
					if(estilo == 'cssText' || estilo == 'margin' || estilo == 'padding')continue;
					$("#"+id).css(estilo,elEstilo);
					//console.log($("#"+id).css("margin-bottom")+'=>'+estilo+'='+$("#"+id).css(estilo)+'('+elEstilo+')');
				}catch(e){
					//console.log(estilo+'='+typeof(elEstilo)+"\n"+e);	
				}
				//console.log(estilo+'='+$("#"+id).css(estilo));
			}else{
				//console.log(estilo+'='+typeof(elEstilo));	
			}
			
		}//);

	});
	/* Fix last-child selector IE */
	$("#becas li:last-child, #eventos li:last-child").css("border-bottom","none");
	/* Eventos */
	$(".fechaC").corner("round 5px");
	/* form busqueda */
	$("input[type=image]").bind("mouseenter mouseleave", function () {
		var src = this.src;
		var alt = this.alt;
		this.src = alt;
		this.alt = src;
	}).css("float", "right");
	var textS = "Haga click Aqui para buscar";
	$("#boxSearch").val(textS).bind("focus", function () {
		$(this).css("color","#000000");
		if($(this).val()==textS)$(this).val("");									
	}).bind("blur", function () {
		$(this).css("color","#C8C8C8");
		if($(this).val()=="")$(this).val(textS);		
	});
	//becas
	$("#t").bind("change", function () {
		$(this).parent().submit();								 
	}).next("input[type=submit]").remove();//Si remueve el submit, funciona el change event?
	$("div#pagBar div a").parent().css("cursor","pointer").bind("click", function () {
		window.self.location.href=$(this).children().attr("href");
	});
	//home
	$("#visitanos,#visitanosI").css("cursor","pointer").bind("click",function(){
		window.self.location.href = window.oficinasURL;
	});
	$("#escribenos,#escribenosI").css("cursor","pointer").bind("click",function(){
		window.self.location.href = window.contactoURL;
	});
	window.mostrarVideo = function (videoPath,capaId,title,description,ancho,alto,imagen,hideInfo) {
		//verificar valores por defecto
		var flashvars = {};
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		// CUSTUMIZABLE PARAMETERS ///////////////////////////////////////////////////////////////////////
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		// video width
		if(typeof(ancho)=="undefined"){
			var stageW = 466;
		}else{
			var stageW = ancho;	
		}
		
		// video height		NOTE: you should include the control bar height
		if(typeof(alto)=="undefined"){
			var stageH = 366;
		}else{
			var stageH = alto;	
		}
		
		// hideInfo
		if(typeof(hideInfo)=="undefined"){
			// view information button 		 NOTE: this will display the title and description of the video
			flashvars.viewInfoButton 		=	"true";
			// view video time				( true / false )
			flashvars.viewTime				=	"true";

			// view scale button			( true / false )
			flashvars.viewScaleButton 		=	"true";
		}else{
			// view information button 		 NOTE: this will display the title and description of the video
			flashvars.viewInfoButton 		=	"true";
			// view video time				( true / false )
			flashvars.viewTime				=	"false";
			// view scale button			( true / false )
			flashvars.viewScaleButton 		=	"false";
		}
		
		///////////////////////////////////////////////////////////////////////
		// PATHS //////////////////////////////////////////////////////////////
		///////////////////////////////////////////////////////////////////////

		// image path
		if(typeof(imagen)=="undefined" || imagen == ''){
			flashvars.imagePath			= "vid/images/Apertura-466x311_australia.jpg";
		}else{
			flashvars.imagePath			= imagen;
		}

		// video path
		flashvars.videoPath 		= '../'+videoPath;

		// video title
		flashvars.title 			= title;

		// video description
		flashvars.description 		= description;
					
		///////////////////////////////////////////////////////////////////////
		// VIEW CONTROLS ////////////////////////////////////////////////////
		///////////////////////////////////////////////////////////////////////

		

		// view fullscreen button		( true / false )
		flashvars.viewFullscreenButton 	=	"true";

		

		// view volume controls			( true / false )
		flashvars.viewVolumeControls 	=	"true";

		
		// view big middle button		( true / false )
		flashvars.viewBigPlayButton 	=	"false";
		
		// view right click menu		( true / false )
		flashvars.viewRightClickMenu 	=	"true";
		
		////////////////////////////////////////////////////////////////////////
		// MOUSE FUNCTIONS //////////////////////////////////////////////////
		////////////////////////////////////////////////////////////////////////

		// mouse hide							( true / false )
		flashvars.mouseHide				=	"true";

		// mouse hide after # (seconds)		NOTE : Must be a hole number !
		flashvars.mouseHideTime			=	"3";

		// double click for toggle size view	( true / false )
		flashvars.doubleClick			=	"true";

		// click the video for play/pause		( true / false )
		flashvars.oneClick				=	"true";
		
		////////////////////////////////////////////////////////////////////////
		// KEYBOARD FUNCTIONS ///////////////////////////////////////////////
		////////////////////////////////////////////////////////////////////////

		// play/pause on SPACE key 		( true / false )
		flashvars.spaceKey				=	"true";
		
		////////////////////////////////////////////////////////////////////////
		// VIDEO FUNCTIONS ///////////////////////////////////////////////////
		////////////////////////////////////////////////////////////////////////

		// video loop				( true / false )
		flashvars.videoLoop			=	"false";

		// video auto play			( true / false )
		flashvars.videoAutoPlay		=	"false";
		
		// video buffer time		( seconds )
		flashvars.videoBufferTime	=	"0.1";
		
		// timeline interval	
		flashvars.tlInterval		=	"100000";

		// sound volume at start 		NOTE :	1=Max	0=Min
		flashvars.soundVolume		=	"0.8";

		// size the video starts at
		// can be set to 1, 2 and 3
		// 1 for narmol size view
		// 2 for aspect view 
		// 3 for full size view
		flashvars.fullSizeView		=	"3";
		
		////////////////////////////////////////////////////////////////////////
		// VISUAL APPEARANCE  /////////////////////////////////////////////////
		////////////////////////////////////////////////////////////////////////
		
		// spacing between the controls
		flashvars.spacing 			=	"10";
		
		// control bar height		( height )
		flashvars.controlHeight		=	"25";
		
		// vulume scrub lenght 		( lenght )
		flashvars.volumeLengthW		= 	"50";
		
		// controls background		( colors / alphas )
		flashvars.color1 			= 	"0xECF0F3";
		flashvars.color2 			= 	"0xE1E1E1";
		flashvars.alpha1 			= 	"1";
		flashvars.alpha2 			= 	"1";
		
		// controls border			( color / alpha )
		flashvars.borderColor 		= 	"0xFFFFFF";
		flashvars.borderAlpha		= 	"1";
		
		// time view ////////////////////////////////////
		// time view background		( colors / alphas )
		flashvars.timeColor1		= 	"0xBFBFBF";
		flashvars.timeColor2		= 	"0xBFBFBF";
		flashvars.timeAlpha1		= 	"1";
		flashvars.timeAlpha2		= 	"1";

		// time view text color		( color )
		flashvars.timeTextColor1	= 	"0xffffff";
		flashvars.timeTextColor2	= 	"0x888888";

		
		// scrubber /////////////////////////////////////////////////////
		// scrubber height			( height )
		flashvars.scrubberHeight 	=	"3";

		// scrubber background 		( color / alpha )
		flashvars.scrubberColor1	= 	"0xC6C6C6";
		flashvars.scrubberAlpha1	= 	"1";

		// scrubber					( color / alpha )
		flashvars.scrubberColor2	= 	"0xCF0500";
		flashvars.scrubberAlpha2	= 	"1";

		// scrubber glow filter		( color / alpha )
		flashvars.filterColor		= 	"0xFF928F";
		flashvars.filterAlpha		= 	"1";
		
		// control buttons	///////////////////////////////////////////////
		// control buttons color	( color )
		flashvars.buttonColor		= 	"0x929292";
		
		// info view /////////////////////////////////////////////////////
		// title color					( color )
		flashvars.titleColor			=	"0x47d2ff";

		// description color			( color )
		flashvars.descriptionColor		=	"0xD3D3D3";

		// info background				( color / alpha )
		flashvars.infoBackgroundColor	=	"0x000000";
		flashvars.infoBackgroundAlpha	=	"0.5";
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		var params = {};
		params.scale = "noscale";
		params.allowfullscreen = "true";
		params.salign = "tl";
		params.bgcolor = "000000";
		
		var attributes = {};
		//console.log("vid/video-player.swf"+"capaId='"+capaId+"'stageW='"+stageW+"'stageH='"+stageH+"'version='"+"9.0.0"+"false='"+false+"'flashvars='"+flashvars.videoPath+"'params='"+params+"'attributes='"+attributes+"'");
		swfobject.embedSWF("vid/video-player.swf", capaId, stageW, stageH, "9.0.0", false, flashvars, params, attributes);

	}
	/*Enlace en toda la capa, sugerencia de Catalina*/
	$("#videos div a").each(function () {
		var enlace = this.href;
		$(this).parent().css("cursor","pointer").click(function () {
			window.self.location.href=enlace;
		});
	});
	$("select[name]=f").bind("change", function () {
		$(this).parent().submit();			
	});
	/*calendar*/
	$(".calendarCuadrito").corner("round 5px");
	$(".descripcionCalendar").hide();
	$(".clickDescripcion").parent().css("cursor","pointer").attr("title",clickAquiParaVerMas).bind("click", function(){
		var elem = $(this).children(".clickDescripcion").siblings(".descripcionCalendar");
		if(elem.css("display")=="none"){
			elem.slideDown(); 
		}
		else{
			elem.slideUp(); 	
		}
	});
	$("#bopacas a img").animate({
			opacity: "0.5"
		},"fast").bind("mouseenter", function (){
			//$("#bopacas a img").fadeTo(1,0.5);
			//$("#bopacas a img").css("opacity",0.5);
		$(this).animate({
			opacity: "1"
		},"fast");												   
	}).bind("mouseleave", function (){
		$(this).animate({
			opacity: "0.5"
		},"fast");											   
	});
	if(window.bloqueaClick){
		function bloquearClick(e){
			if(typeof(e.which)!="undefined" && e.which == 3){
				alert(window.mensajeNoClick || "Forbbiden");
				return false;
			}
		}
		$("*").live("mousemove",function(e){
			return bloquearClick(e);
		});
		$("*").live("click",function(e){
			return bloquearClick(e);
		});
		$("*").live("mouseup",function(e){
			return bloquearClick(e);
		});
		$("*").live("mousedown",function(e){
			return bloquearClick(e);
		});
	}
	$(".tablaestilizada tr:odd").addClass("trestilizadaI");
	$(".tablaestilizada tr:even").addClass("trestilizadaP");
	$(".tablaestilizada tr:first-child").removeClass("trestilizadaP").addClass("trencabecado");
	$(".abrepopup").bind("click", function (event) {
		event.preventDefault(); 
		var enlace = $(this).parent().attr("href");
		window.abrirPopUp(enlace,620,540);
	}).css("cursor","pointer");
	$("#archivos a").attr("target","_blank");
	$("#chained").scrollable({hoverClass: 'hover',size: 1}).circular().navigator().mousewheel().autoscroll({
		steps: 1,
		interval: 4000
	});	
});
$(window).load(function(){
	$("a[href=http://www.webstat.com/]").parent().css("position","absolute").css("top","-5000px").css("width","1px");
});