try{document.execCommand("BackgroundImageCache", false, true);}catch(err){};


$('document').ready(function(){

	$('.jload').show();

	/* Marcamos la página activa */
	
	$('#mainmenu a[href="'+pagina+'"]').parent().addClass("act");
	
	/* Abrimos enlaces en página nueva validando xHTML Strict */
	
	$('.newpage').live('click',function(e){
		e.preventDefault();
		window.open($(this).attr("href"));
		return false;
	});
	
	/* Subir arriba con plugin */
	
	$.easing.elasout = function(x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	};
	
	$('.up').click(function(e){
		e.preventDefault();
		$.scrollTo(0, 800, {easing:'elasout'});
		return false;
	});
	
	/* Subir arriba "casero" */
	
/*	$('.up').click(function(e){
		e.preventDefault();
		p=$(this).offset();
		var h=$(window).scrollTop();
		var s=$(window).height();
		(h+s>h)?sc(h):"";
		return false;
	});

	function sc(p){
		$('#contentwrap').animate({opacity:1},25,function(){
			scrollTo(0,p);
			p=p-105;
			return (p>0)?sc(p):scrollTo(0,0);
		});
	}
	
	/* Animacion caras en equipo */
    $(".contTeam ul li").mouseenter(function(){
		$(this).stop();
		$(this).animate({marginTop:"0px"});
    }).mouseleave(function(){
     	$(this).stop();
		$(this).animate({marginTop:"35px"});
    });
    
    $(".contTeam ul li").click(function(){
		var txt = $(this).find(".descTeam").html();
		var cont = $(".descPersonTeam");
		var color = $(this).find('.color').html();
		cont.fadeOut('slow', function(){
			cont.html(txt);
			cont.css({'background-color' : color});
		}).fadeIn('slow');
		return false;
    });
    
    // efecto campos formulario
	$("#formcontact input").live('click',function(){if($(this).attr("title")==$(this).val())$(this).val("");});
	$("#formcontact input").live('blur',function(){if(!$(this).val())$(this).val($(this).attr("title"));});
	$("#comment").click(function(){if($(this).attr("title")==$(this).val())$(this).val("");});
	$("#comment").blur(function(){if(!$(this).val())$(this).val($(this).attr("title"));});

	// Efecto mostrar politica privacidad en contacto
	$('.showLopd').live('click',function(){
		$('.lopd').slideToggle('fast', function(){
			return !$.fn.colorbox.resize();
		});
	});

    /* Google maps */
   	function setMap() {
	    var latlng = new google.maps.LatLng(39.56514, 2.624166);
    
	    var myOptions = {
			zoom: 15,
			center: latlng,
			mapTypeControl: false,
			mapTypeId: google.maps.MapTypeId.ROADMAP
	    };
	
		var map = new google.maps.Map(document.getElementById("map"), myOptions);

		var marker = new google.maps.Marker({
			position: latlng, 
			map: map,
			title:"Mandarina Creativos"
		  });
	  }

	if ($('#map').length >0) setMap();
	
	/* Mostrar submenu y buscador */
	/* $('#porfolio, #search').click(function(e){
		e.preventDefault();
		var cont = $(this).next('ul');
		$('#mainmenu ul[id!='+cont.attr("id")+']').slideUp('slow',function(){
			cont.slideToggle('slow');
		});
		return false;
	}); */
	
	/* Mostrar selects buscador */
	$('.fakediv input, .fakediv a').click(function(e){
		e.preventDefault();
		$(this).parent().next('.selectcont').slideToggle();
		return false;
	});
	
	//Esto se podrá quitar???
	$('.selectcont').click(function(){
		return $(this).slideToggle();
	});
	
	/* Mostrar imagenes grandes */
	
	$('.contpic').click(function(){
		contBig = $('.big').eq($('.contpic').index(this));
		projectId = $(this).parents('.project').attr('id').split('-');
		if (contBig.css('display') == 'none') {
			contBig.html('');
			$(this).after('<img src="img/ajax-loader.gif" alt="loading..." id="loader" />');
			$.post('inc/imgLoad.php',{projectId:projectId[1]},function(r){
				src = r.split('|');
				$(src).each(function(i){
					if (src[i] != '') {
						var ima = new Image();
						ima.src = src[i];
						ima.onload = contBig.append(ima);
					}
				});
				
				$('#loader').fadeOut('slow', function(){
					$(this).remove();
					return !contBig.slideDown();
				});
				
	        });
			return false;
		}
		return !contBig.slideUp();
	});
	
	$('.big').click(function(){
		return $(this).slideToggle();
	});
	
	
	/* Imprimir pagina */
	
	$('#imprimir').click(function(){
		window.print();
		return false;
	});

	/* Colorbox para texto legal + texto confidencial */
	
	$('#legal, #confidencial, #creditos').colorbox({speed:1000, maxHeight:470, scrolling:false});
	
	$(".colorbox").colorbox({scrolling:false});
	
	tooltip();
	
	// Validar formulario contacto

	$('.send').live('click', function(){
		var tForm = $('form').eq($('.send').index(this));
		var tReq = tForm.find('.required');
		tReq.removeClass('error');
		$('input[name="accept"]').parent().removeClass('error');
		if ($('input[name="accept"]').is(':checked')) {
		tReq.each(function(){ if ($(this).val()==$(this).attr('title') || $(this).val()=='' || ($(this).attr('name')=='email' && !checkEmail($(this).val()))) $(this).addClass('error'); });
		if (!tReq.hasClass('error')) {tForm.submit();};
		return !$.fn.colorbox.resize();
		} else {
			return !$('input[name="accept"]').parent().addClass('error');
		}
	});
	
	$("img.tooltip").lazyload({
	    effect:"fadeIn"
	});
	
}); /* End DOMReady */

function checkEmail(d){
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(d);
}

/* En tooltip */
this.tooltip = function(){
	/* CONFIG */
		xOffset = 120;
		yOffset = -30;
		if ($('#index, #mndhotel').length>0) {
			xOffset = 100;
			yOffset = -175;
		}
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
	/* END CONFIG */
	$(".tooltip").hover(function(e){ 
		this.t = this.title;
		this.title = "";
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$(".tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
};


function parseTwit(twit) {
	$('document').ready(function(){
		var dtwit = new Date(twit[0].created_at.replace('+0000',''));
		dtwit = dtwit.getDate()+"."+parseInt(dtwit.getMonth()+1)+"."+dtwit.getFullYear().toString().substr(2,2);
		$('.socialTwitter').find('span').html(dtwit);
		$('.socialTwitter').find('a').html(twit[0].text);
	});
}

