(function ($) {
	$.fn.vAlign = function(container) {
		return this.each(function(i){
			if(container == null) {
				container = 'div';
			}
			$(this).html("<" + container + ">" + $(this).html() + "</" + container + ">");
			var el = $(this).children(container + ":first");
			var elh = $(el).height(); //new element height
			var ph = $(this).height(); //parent height
			var nh = (ph - elh) / 2; //new height to apply
			$(el).css('margin-top', nh);
		});
	};
})(jQuery);

// Preloader de "Join Us".
var enviaFormularioJoinUs = function() {
	xajax.dom.create("loading","div", "loading");
  	xajax.$('loading').innerHTML='<div class="procesando"><img src="img/loading_form.gif" width="39" height="13" class="middle loading" alt=""/></div>';
}

// Preloader de "Find Us".
var enviaFormularioFindUs = function() {
	xajax.dom.create("loading","div", "loading");
  	xajax.$('loading').innerHTML='<div class="procesando"><img src="img/loading_form.gif" width="39" height="13" class="middle loading" alt=""/></div>';
}

function comprueba_buscador (ID_busqueda, valor) {

	if(document.getElementById(ID_busqueda).value == ''){
		document.getElementById(ID_busqueda).value = 'search...';	
	} else{
		if(document.getElementById(ID_busqueda).value == 'search...'){
			document.getElementById(ID_busqueda).value = '';
		} else {
			document.getElementById(ID_busqueda).value = valor;	
		}
	}

}

function mostrarVideo (box, url, screenshot, width, height){
	
	var so = new SWFObject("swf/player.swf", "player", ""+ width +"", ""+ height +"", "9", "#000000");
	so.addParam("allowfullscreen ", "true");
	//so.addVariable("idvideo", url); <!-- cambiar por la ruta del video -->
	so.addParam('flashvars',"file="+ url +"&image="+ screenshot +"&autostart=false");   	
	//so.addVariable("idimagen", screenshot); <!-- cambiar por la ruta de la imagen -->
	so.write(box);
	
}

// DOM Ready General.
$(document).ready(function(){												
						
	// Header.						
	$("#select_language").click(function(){
		if($(".languages").is(":hidden")){										 
			$(".languages").slideToggle();
		}
	});
	
	$(".languages .language").each(function(){
		var lang = $("#select_language").html();
		var lang_clear = $(lang).html();
		if(lang_clear == $(this).html()){
			$(this).addClass('selected');
		}
	});
	
	$(".languages .language").click(function(){											 
		var language = $(this).html();
		document.location.href = "index.php?lang=" + language;
	});	

	var mouse_is_inside = false;
    $("body").mouseup(function(){ 
        if(! mouse_is_inside) $(".languages").slideUp(1)
    });	
	
	// Menu.
	$("#dropline li.current").children("ul").css("left", "0px").show();
	if($("#dropline li.current").children("ul").length != 0){
		$("#dropline li.current").children(":first-child").addClass('clase_current');
	} else {
		$("#dropline li.current").children(":first-child").addClass('clase_current').addClass('clase_current_vacio');
	}
	$(".sub_left").each(function(){
		$(this).find("a:last").addClass('no_bg');
	});
	
	$(".sub_right").each(function(){
		$(this).find("a:first").addClass('no_bg');
	});
	
	$("#dropline li").hover(function(){
									 
		if(this.className.indexOf("current") == -1)  {
			
			getCurrent = $(this).parent().children("li.current:eq(0)");
			
			if(this.className.indexOf("top") != -1)  {
				$(this).children("a:eq(0)").css("color","#000");
			} else {
				$(this).children("a:eq(0)").css("color","#000");
			}
			
			if (getCurrent = 1) {
				$(this).parent().children("li.current:eq(0)").children("ul").hide();
			}
			
			$(this).children("ul:eq(0)").css("left", "0px").show();
			
			if($(this).children("ul").length > 0){
				$("#dropline li.current").children(":first-child").removeClass('clase_current');
				$(this).children(":first-child").addClass('clase_current');				
			}
			
			if($(this).parent().find("ul").length != 0){
				$("#dropline li.current").children(":first-child").removeClass('clase_current');
			}
			
		} else {
			
			$("#dropline li.current").children(":first-child").addClass('clase_current');
		
		}
		
	}, function(){
		
		if(this.className.indexOf("current") == -1)  {

			getCurrent = $(this).parent().children("li.current:eq(0)");
			
			if(this.className.indexOf("top") != -1) {
				$(this).children("a:eq(0)").css("color","#999");
			} else {
				$(this).children("a:eq(0)").css("color","#999");
			}
			
			if (getCurrent = 1 ) {
				$(this).parent().children("li.current:eq(0)").children("ul").show();;
			}
			
			$(this).children("ul:eq(0)").css("left", "-99999px").hide();
			
			if($(this).children("ul").length > 0){
				$("#dropline li.current").children(":first-child").addClass('clase_current');
				$(this).children(":first-child").removeClass('clase_current');				
			}
			
			if($(this).parent().find("ul").length != 0){
				$("#dropline li.current").children(":first-child").addClass('clase_current');
			}
			
		} else {
			
			$("#dropline li.current").children(":first-child").addClass('clase_current');	
		
		}
	
	});
	// Fin de Menu.
	
	// Home.
	$(".data_collection .title_collection h4").each(function(){
		var sombra = '<b>'+ $(this).text() +'</b>';				
		$(this).before(sombra);										   
	}); 
	
	$(".points").hover(function(){
		$(this).find('.bubble').show();
		$(".text_bubble").each(function(){
			var alto_text_bubble = $(this).height();									
			$(this).parent('.mid_bubble').height(alto_text_bubble - 20);							
		});		
	}, function(){
		$(this).find('.bubble').hide();		
	});
	
	// My Favorites.
	$(".item").each(function(index){
		if((index + 1)%3 == 0){
			$(this).addClass('no_margin_right');
		}
	});				

	$('.item').hover(function(){
		$(this).find(".boxcaption").stop().animate({top:'0px'},{queue:false,duration:500,easing: 'easeOutBounce'});
	}, function() {
		$(this).find(".boxcaption").stop().animate({top:'-158px'},{queue:false,duration:200});
	});	
	
	// Virtual Playground 1.
	$(".gallery ul li").each(function(index){
		if((index + 1)%3 == 0){
			$(this).addClass('no_margin_right');
		}
	});	
	
	// Virtual Playground 2.	
	$(".container_objects .object").each(function(index){
		$(this).vAlign();												  
		if((index + 1)%2 == 0){
			$(this).addClass('no_margin_right');
		}
	});
	
	// Footer.
	$(".redes_sociales_footer ul li img").hover(function(){
		$(this).animate({opacity: 1}, 300);									   
	}, function () {
		$(this).animate({opacity: .8}, 300);									   
	});
	
	// Join us.
	$("#enviar_formulario_contacto").click(function(){
		$("#text_mensaje span.form_error, #text_mensaje span.form_exito").fadeOut(500);
		xajax_enviarFormaJoinUs(xajax.getFormValues('formulario_join_us'));		
	});
	
	// Find us.
	$("#enviar_formulario_find").click(function(){
		$("#text_mensaje span.form_error, #text_mensaje span.form_exito").fadeOut(500);
		xajax_enviarFormaFindUs(xajax.getFormValues('formulario_find_us'));		
	});		
			
});	