document.getElementsByTagName('html')[0].className = 'js';

$(document).ready(function() {
	setEvents();
	setMailer(".mailer");
	setMailersContacto();
	creaReelCasos();
	$('#menu-principal li:first-child').addClass("first");
});
$(window).load(function() {    
	$("#bg").show();
	var theWindow = $(window),
    $bg = $("#bg"),
    aspectRatio = $bg.width() / $bg.height();
	function resizeBg() {
    	if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
                    $bg
                        .removeClass()
                        .addClass('bgheight');
                } else {
                    $bg
                        .removeClass()
                        .addClass('bgwidth');
                }
				$("#sidebar").height($(document).height());

        }
        theWindow.resize(function() {
                resizeBg();
        }).trigger("resize");
});

function setNavScroll(){
	var nav_pos = $('#sidebar').offset().top;
	$(window).scroll(function(){
	if ( document.documentElement.scrollTop >= nav_pos  || document.body.scrollTop >= nav_pos)
		{
			$('body').addClass('slideSidebar');
		}
		else $('body').removeClass('slideSidebar');
	}).scroll();
}

function setEvents(){
	$("#nav-noticias li:first-child").addClass("active");
	$("#nav-noticias li a").bind('click', function(event) {
		swapNews($(this).parent().index());		
		cancelEvent(event);
	});
	$("#wrap-bloques h2").hover(function() {
		$(this).addClass("active");
	}, function() {
		$(this).removeClass("active");		
	});
	$("#external-links ul ul, #bolsa-link span, #wrap-bloques .ficha>div, #wrap-noticias").each(function(){
	 	$(this).css("height", $(this).height()+"px");
	 	$(this).hide();
	}); 	
	$("#nav-gallery li").bind('click', function(event) {
		if($(this).hasClass("active"))return;
		var seccion = $(this).parent().attr("rel");
		swapReelCasos($(this).index(),seccion);
	});
	
	$("#external-links a").bind('click', function(event) {
		$(this).parent().find("ul ul").slideToggle();
		cancelEvent(event);
	});
	$("#bolsa-link a").bind('click', function(event) {
		$(this).parent().find("span").slideToggle();
		cancelEvent(event);
	});
	$("#wrap-bloques h2").bind('click', function(event) {
		var item = $(this).parent();
		var wrapper = "#wrap-bloques";
		setCollapsable(item, wrapper);
				cancelEvent(event);
	});
};
function swapNews(n){
	$("#nav-noticias li").removeClass("active");
	$("#nav-noticias li:eq(" + n + ")").addClass("active");	
	$("#content-noticias li:visible").fadeOut(200);
	$("#content-noticias li:eq(" + n + ")").delay(200).fadeIn(400);
	
	
}
function setCollapsable(item, wrapper){	
	var prev = 	$(wrapper + " .visible");
	$(prev).slideUp();
	$(prev).removeClass("visible");
	$(item).find("div:hidden").slideDown(500,function(){
		$("#sidebar").animate({height: $("#trama-bg").height() + 50}, 100);
	});
	$(item).find("div").addClass("visible");
}
function setMailer(itemName){
	$(itemName).each(function(){
		var nombre = $(this).attr("rev");
		var site = $(this).attr("rel");	
		$(this).attr("href", "mailto:"+ nombre + "@" + site);
		$(this).text(nombre + "@" + site);
	});
}

function creaReelCasos(){
	$("#wrap-casos #nav-gallery li:first-child").addClass("active");
	$("#nav-gallery li:eq(3), #nav-downloads li:eq(3)").addClass("end");
	
	var firstImg = $("#imgs-casos img:first-child");
	$("#imgs-casos img").hide();
	$("#imgs-casos").height(firstImg.height());
	firstImg.show();	
	$("#wrap-casos #info-casos div:first-child").show();
	$(".sobre-ibidem #wrap-bloques").hide();
}
function swapReelCasos(n,seccion){
	$("#wrap-bloques:hidden").show();
	$("#nav-gallery li").removeClass("active");
	$("#nav-gallery li:eq(" + n + ")").addClass("active");	
	$("#imgs-casos img:visible").fadeOut(200);
	$("#imgs-casos img:eq(" + n + ")" ).fadeIn(400,function(){	$("#sidebar").height($("#content").height() + 100);});	
	$("#info-casos div:visible").fadeOut(200);

	divMostrar = $("#info-casos div:eq(" + n + ")");
	$(divMostrar).delay(200).fadeIn(400);
	
	if(seccion == "equipo") var timer = setTimeout ( "setLeerMas()", 200 );		

	
}
function setLeerMas(){
	var h = $("p:eq(0)",divMostrar).height() + 62;
	hPrev = $(divMostrar).height();
	hPlegado = h;
	$(divMostrar).height(h);
	$("#sidebar").height($("#content").height() + 100);
	$(".leer-mas a",divMostrar).toggle(function(event) {
		$(divMostrar).animate({height: hPrev},700,function(){
			$("#sidebar").height($("#content").height() + 100);
		});
		$(this).text("-");
		cancelEvent(event);
		
	}, function(event) {
		$(divMostrar).animate({height: hPlegado},700,function(){
			$("#sidebar").height($("#content").height() + 100);			
		})
		$(this).text("+");	
		cancelEvent(event);
	});
	
}



function setMailersContacto(){
	$("#intro-contacto em").each(function() {
		var txt = $(this).text();
		var pos = txt.indexOf("[en]");
		if(pos) $(this).wrapInner("<a/>");
		var nombre = txt.substr(0,pos);
		$("a", this).attr("href", "mailto:" + nombre + "@" + "ibidem.es" );
		$("a", this).text(nombre + "@" + "ibidem.es");
	});	
}
function cancelEvent(e){
		if (e && e.preventDefault)e.preventDefault();
		return false; 		
}




