$(document).ready(function(){
	setEvents();
	setFonts();
	$(".projectGal a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});

	creaDropDown(".list1 ");	
	creaDropDown(".list2 ");	
	creaDropDown(".infoExpo ");
	scrollEvents();
	//controlTopNav();
});

$(window).load(function(){
	var posCabA=$("#cabecera_animada").offset().left;
	var posCabB=$("#cabecera_animada_2").offset().left;
	var ancho=$("#cabecera_animada").width();
	$(document).everyTime(8000, function(i) {
		$('#cabecera_animada').animate({left: '-=455'}, 500); posCabA-=455;
		$('#cabecera_animada_2').animate({left: '-=455'}, 500); posCabB-=455;
		if(((posCabA+ancho)*1)<0) {
			$('#cabecera_animada').animate({left: '+='+(ancho*2)}, 0); posCabA+=ancho*2;
		}
		if(((posCabB+ancho)*1)<0) {
			$('#cabecera_animada_2').animate({left: '+='+(ancho*2)}, 0); posCabB+=ancho*2;
		}
	}, 0);
});

$(window).resize(function(){
	resizeAnclaRelleno();
});


function strpos (haystack, needle, offset) { // Equivalente a strpos de php
	var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
	return i === -1 ? false : i;
}

function resizeAnclaRelleno() {
	// Fijar el tamaņo del div de la derecha, para que permita realizar los scroll correctamente
	var ancho=$(window).width();
	var colLeft=$("#left").width();
	var divCont= 312; //$(".contInfo").width();
	//alert(ancho-colLeft-(divCont*anclaImpar));
	$("#ancla_relleno").width(ancho-colLeft-(divCont*anclaImpar)-70);
}

function scrollEvents(){
	resizeAnclaRelleno();
	$("#navTopHome").click(function (e) {
		anclaActual=0;
		$('html,body').animate({ scrollLeft: 0,scrollTop:0 }, 800);
		if (e && e.preventDefault)
		e.preventDefault(); 
		return false;
	});
	$("#navTopNext").click(function (e) {
		anclaActual++;
		if($('#ancla_'+anclaActual).length) {
			scrollAncla(anclaActual);
			if (e && e.preventDefault)
			e.preventDefault(); 
			return false;
		} else {
			anclaActual--;
			return false;
		}
	});
	$("#navTopPrev").click(function (e) {
		anclaActual--;
		if($('#ancla_'+anclaActual).length) {
			scrollAncla(anclaActual);
			if (e && e.preventDefault)
			e.preventDefault(); 
			return false;
		} else {
			anclaActual++;
			return false;
		}
	});
}

function scrollAncla(ancla) {
	if(ancla>0) {
		var offset = $('#ancla_'+ancla).offset();
		offset=offset.left-290;
		$.scrollTo({top:'0px', left:offset+'px'},800, {axis:'x'});
	} else {
		$('html,body').animate({ scrollLeft: 0,scrollTop:0 }, 800);
	}
}

function setFonts(){
	//bold weight
	Cufon.set('fontFamily', 'bmw_b');
	Cufon.replace('.cssmenu a', {hover: true, fontWeight: "bold"})('.contInfo h3');
	//regular weight
	Cufon.set('fontFamily', 'bmw_r');
	Cufon.replace('#botFilosofia', {hover:true})('#content .introSection h3')('.contInfo h4')('.home h3')('#secondNav a', {hover:true});
	Cufon.set('fontFamily', 'sta');
	//cufon disabled in footer when IE
	if(jQuery.support.leadingWhitespace){
		Cufon.replace('#footer a');
	}
	Cufon.set('fontFamily', 'bmw_l')('.col-home h2')('.contInfo h2');
	Cufon.replace('#left h1');
	
}
function setEvents(){
  $(".toggleInfo").each(function(){
 	$(this).css("height", $(this).height()+"px");
 	$(this).hide();
 }); 	
		

	$(".toggleBot").click(function (e) {
      	var itemAId = $(this).attr("rel");
		$(this).toggleClass("toggleActivo");
	      $("."+ itemAId).stop().slideToggle("normal");
		if (e && e.preventDefault)
		e.preventDefault(); 
		return false;
    });
}

function creaDropDown(elemDiv){
	$(elemDiv + " .infoProject").hide();
	dlActivo = 0;
//	var timerDd = setTimeout(showFirst,1300);
	showFirst(elemDiv);
	var cant = $(elemDiv + " .infoProject").length;
	for(var i= 0; i < cant ; i++){
		itemA = $(elemDiv + "h5")[i];
		//console.log(itemA);
		var itemB = $(elemDiv + "h6")[i];
		//$(elemDiv + "h5").idN = i;
		itemA.idN = i;
		itemB.idN = i;
		$(itemB).mousedown(function(){
			var itemAPrevio = $(elemDiv + " .infoProject")[dlActivo];							  
			$(itemAPrevio).slideUp();
		});
		$(itemA).mousedown(function(){
			$(elemDiv + "h5").removeClass("projectActivo");
			$(this).addClass("projectActivo");									   
			var itemAPrevio = $(elemDiv + " .infoProject")[dlActivo];
			if(this.idN == dlActivo && $(itemAPrevio).is(':visible')){
				$(itemAPrevio).slideUp();
				$(".faq dt").removeClass("activeQ");
			return;
			}			
			$(itemAPrevio).slideUp("fast");

			var itemAActivo = $(elemDiv + " .infoProject")[this.idN];
			$(itemAActivo).slideDown();
			dlActivo = this.idN;
		});
	}
}
function showFirst(elemDiv){
	var itemAActivo = $(elemDiv + " .infoProject")[dlActivo];
	var boton = $(elemDiv + "h5")[0];	
	$(boton).addClass("projectActivo");
	$(itemAActivo).slideDown();	
	
}



function setProjects(){
	$(".infoProject").hide();	
	$(".projectActivo").show();	
	$(".infoProject h6").click(function (e) {
	 	
	 });							  
};

