var ANCHO_STAGE;
var ALTO_STAGE;
var posicionActual = 0;
var pausa = false;
var imagePath = "archivo/";
var FULL_IMG = true;
var TIEMPO_SLIDE = 1500;
var TIEMPO_FADE = 250;
var imgBackground;
var auxiliar;
var pausa = false;
var btnActivoInterna;
var btnActivoFooter;
var arregloTnsProyecto;
var offContenedorTns = 0;
var grupos;
var grupoActual = 1;
var statusNavegacion;
var accesoDirectoLaMaldita = false;
var clickUsuario = true;
var isIE;
var urlVideo;
var typeVideo;

var acum=0;


$(document).ready(function(){
	isIE = $.browser.msie;
	precargarImagenes();
	//inicializar();
});

function precargarImagenes(){
	//alert(proyecto.length);
	precargarFoto(acum);
}

function precargarFoto(num){
	$("#loader").remove();
	$("body").append("<img id='loader' src='"+imagePath+proyecto[acum].bg+"'>");
	$("#preLoading").css({"background-image":"url(archivos/loading"+loadingPath+"/loading_0"+Math.floor(Math.random()*9)+".png)"});
	$("#preLoading").css({display:"block"});
	$("#loader").load(function(){
			acum++;
			if (acum<proyecto.length){
				precargarFoto(acum);
			}else{
				$("#preLoading").css({display:"none"});
				inicializar();
			}
			
	});
}

function inicializar(){
	$(window).resize(resize);
	initEventosMouse();
	leerUrl();
	//initHistory();
}

function initEventosMouse(){
	/*$("#login input").focus(function(){
		if (this.value == this.defaultValue){  
            this.value = '';  
        } 
	});
	
	$("#login input").blur(function(){
		if (this.value == ""){
			this.value = this.defaultValue;
		}
	});*/
	
	$("#flechaIzq").click(function(){
		clickUsuario = true;
		retroceder();
	});
	
	$("#flechaDer").click(function(){
		clickUsuario = true;
		avanzar();
	});
	
	$("#btnEntrar").click(function(){
		clickEntrar();
	});
	
	$("#iconoGrilla").click(function(){
		mostrarTodosLosProyectos();
	});
	
	$("#iconoFull").click(function(){
		cerrarSiEstaAbierta(true);
	});
	
	$("#iconoLaMaldita, #logo").click(function(){
		subir(proyectoLaMaldita);
	});
	
	$("#todosLosProyectos .flechaIzq").click(function(){
		verTnsProyectoIzquierda();
	});
	
	$("#todosLosProyectos .flechaDer").click(function(){
		verTnsProyectoDerecha();
	});
	
	$("#todosLosProyectos .cerrar").click(function(){
		cerrarTodosLosProyectos(true);
	});
	
	$("#todosLosProyectos .menu a:first").addClass("btnActivoTodosLosProyectos");
	$("#todosLosProyectos .menu a").click(function(){
					$("#todosLosProyectos .menu a").removeClass("btnActivoTodosLosProyectos");
					$(this).addClass("btnActivoTodosLosProyectos");	
					
	});
	
	/*$("#todosLosProyectos .contenedorThumbs div").mouseover(function(){
		$(this).find("a").css({"visibility":"visible"});
	});
	
	$("#todosLosProyectos .contenedorThumbs div").mouseout(function(){
		$(this).find("a").css({"visibility":"hidden"});
	});*/
	
	arregloTnsProyectos = $(".contenedorThumbs > div");
	//acomodarTnsProyectos(arregloTnsProyectos);
	
	btnActivoFooter = document.getElementById("iconoFull");
	$(btnActivoFooter).css({"background-position":"bottom"});
	
	$("#iconosFooter a").mouseover(function(){
		if (btnActivoFooter != this)
		$(this).css({"background-position":"bottom"});
	});
	
	$("#iconosFooter a").mouseout(function(){
		if (btnActivoFooter != this)
		$(this).css({"background-position":"top"});
	});
	
	$("#iconosFooter a").click(function(){
		btnActivoFooter = this;
		$("#iconosFooter a").css({"background-position":"top"});
		$(this).css({"background-position":"bottom"});
	});

}

function leerUrl(){
	if(document.location.href.indexOf("#/")>=0){
		for (i=0 ; i<proyecto.length ; i++){
			if(document.location.href.indexOf(proyecto[i].nombre)>=0 && i!=0){
				posicionActual = i;
				proyectoInicial(i);
				break;
			}
		}
	}else{
		proyectoInicial(posicionActual);
	}
}

function initHistory(){
	$.history.init(callback);
    $("a[@rel='history']").click(function(){
		$.history.load(this.href.replace(/^.*#/, ''));
		return false;
    });
}

function callback(hash){
	if (clickUsuario != true){
		openHistory(hash);
	}else{
		clickUsuario = false;
	}
}

/*
*************************************************************************
Todos los proyectos
*************************************************************************
*/

function acomodarTnsProyectos(a){
	$(".contenedorThumbs").css({"left":"0px"});
	$("#todosLosProyectos .contenedorThumbs div").css({"display":"none"});
	var posx = 0;
	var posy = 0;
	var contador = 0;
	grupoActual = 1;
	grupos = parseInt(a.length/9);
	grupos=((a.length%9)>0)?grupos+1:grupos;
	
	for (var k=0;k<a.length;k++){
		posx = k*($(window).width()-($(window).width()/2 - 450));
		for (var i=0;i<3;i++){
			for (var j=0;j<3;j++){
				$(a[contador]).css({"left":(j*185)+posx, "top":i*128, "display":"block"});
				//$(a[contador]).css({"top":i*128});
				//$(a[contador]).fadeIn("normal");
				//$(a[contador]).css({"display":"block"});
				contador++;
			}
		}
		
	}
	managerFlechitasTns();
}

function verTnsProyectoDerecha(){
	if (pausa == false){
		if (grupoActual<grupos){
			pausa = true;
			$(".contenedorThumbs").animate({"left": "-="+($(window).width()-($(window).width()/2 - 450))+"px"}, TIEMPO_SLIDE, 
																							function(){
																								grupoActual = grupoActual+1;
																								managerFlechitasTns();
																								pausa = false;
																							});
		}
	}
}

function verTnsProyectoIzquierda(){
	if (pausa == false){
		if (grupoActual>=grupos && grupos>1){
			pausa = true;
			$(".contenedorThumbs").animate({"left": "+="+($(window).width()-($(window).width()/2 - 450))+"px"}, TIEMPO_SLIDE, 
																							function(){
																								grupoActual = grupoActual-1;
																								managerFlechitasTns();
																								pausa = false;
																							});
		}
	}
}

function managerFlechitasTns(){
	if (grupoActual==1 && grupos==1){
		$("#todosLosProyectos .flechaIzq, #todosLosProyectos .flechaDer").css({"display":"none"});
		//$("#todosLosProyectos .flechaDer").css({"display":"none"});
	}
	
	if (grupoActual==1 && grupos>1){
		$("#todosLosProyectos .flechaIzq").css({"display":"none"});
		$("#todosLosProyectos .flechaDer").css({"display":"block"});
	}
	
	if (grupoActual==grupos && grupos>1){
		$("#todosLosProyectos .flechaIzq").css({"display":"block"});
		$("#todosLosProyectos .flechaDer").css({"display":"none"});
	}
	
}

function mostrarTodosLosProyectos(){
	if (statusNavegacion != "todosLosProyectos"){
		if (statusNavegacion == "interna"){
			$("#menuInterna").css({"display":"none"});
			$("#contenedorInterna").css({"display":"none"});
			//$("#menuInterna").fadeOut("normal");
			//$("#contenedorInterna").fadeOut("normal", function(){
			$("#interna").html("");
			$("body").css({"overflow":"hidden"});
			$("#interna").css({"width":"10px"});
			
			
			statusNavegacion = "todosLosProyectos";
			//$("#infoProyecto, #navegacionFlechas").fadeOut("normal");
			$("#infoProyecto, #navegacionFlechas").css({"display":"none"});
			$("#todosLosProyectos").fadeIn(TIEMPO_FADE);
			$("body").css({"overflow":"auto"});
			$("#todosLosProyectos").css({"width":$("#todosLosProyectos .contenedorThumbs").width()});
			//acomodarTnsProyectos(arregloTnsProyectos);
			$("#todosLosProyectos .menu a").removeClass("btnActivoTodosLosProyectos");
			$("#todosLosProyectos .menu a:first").addClass("btnActivoTodosLosProyectos");
			
		}else{
			cerrarSiEstaAbierta(false);
			statusNavegacion = "todosLosProyectos";
			//$("#infoProyecto, #navegacionFlechas").fadeOut("normal");
			$("#infoProyecto, #navegacionFlechas").css({"display":"none"});
			$("#todosLosProyectos").fadeIn(TIEMPO_FADE);
			$("body").css({"overflow":"auto"});
			$("#todosLosProyectos").css({"width":$("#todosLosProyectos .contenedorThumbs").width()});
			//acomodarTnsProyectos(arregloTnsProyectos);
			$("#todosLosProyectos .menu a").removeClass("btnActivoTodosLosProyectos");
			$("#todosLosProyectos .menu a:first").addClass("btnActivoTodosLosProyectos");
		}
	}
}




/*
*************************************************************************
*/

/*
*************************************************************************
COMUNES
*************************************************************************
*/

function resize(){
	var ANCHO_STAGE = $(window).width();
	var ALTO_STAGE = $(window).height();
	if (FULL_IMG){
		var ANCHO_IMG = $(".divImagen img").width();
		var ALTO_IMG = $(".divImagen img").height();
		var stageRatio = ANCHO_STAGE / ALTO_STAGE;
		var imgRatio = ANCHO_IMG / ALTO_IMG;
		if (stageRatio > imgRatio){
			var newHeight = (ANCHO_STAGE / ANCHO_IMG) * ALTO_IMG;
			var newWidth = ANCHO_STAGE;
		}else{
			var newHeight = ALTO_STAGE;
			var newWidth = (ALTO_STAGE / ALTO_IMG) * ANCHO_IMG;
		}
		var newTop = 0 - ((newHeight - ALTO_STAGE) / 2);
		var newLeft =  0 - ((newWidth - ANCHO_STAGE) / 2);
		$(".divImagen img").css({"width":newWidth , "height":newHeight, "top":newTop , "left":newLeft});
	}
	//acomodarTnsProyectos(arregloTnsProyectos);
}



function proyectoInicial(num){
	$("body").append("<div id='main' class='divImagen' style='left:0px; top:0px'><img id='foto' src='"+imagePath+proyecto[num].bg+"'></div>");
	$("#infoProyecto h1").html(filtrarTexto(proyecto[num].titulo));
	var txtH2 = "";
	/*if (proyecto[num].categoria != ""){
		txtH2 = txtH2 + "<span class='categoria'>"+filtrarTexto(proyecto[num].categoria)+". </span>";
	}
	txtH2 = txtH2 + proyecto[num].bajada;*/
	txtH2 = proyecto[posicionActual].bajada;
	$("#infoProyecto h2").html(txtH2);
	$("#foto").css({display:"none"});
	//$("#preLoading").css({display:"block"});
	$("#foto").load(function(){
			//$("#preLoading").css({display:"none"});
			resize();
			$("#foto").fadeIn(TIEMPO_SLIDE, function (){
				if (proyecto[posicionActual].titulo+proyecto[posicionActual].categoria+proyecto[posicionActual].bajada != "")
				$("#infoProyecto").fadeIn(TIEMPO_FADE);
				$("#navegacionFlechas").fadeIn(TIEMPO_FADE);
			});
	});
}

function filtrarTexto(t){
	return t;
}

/*
*************************************************************************
*/

/*
*************************************************************************
Navegacion
*************************************************************************
*/

function avanzar(){
	if (!pausa){
		posicionActual++;
		if (posicionActual==proyecto.length){
			posicionActual = 0;
		}
		sentido = "avanzar";
		
		cambiarInfoProyecto();
	}
}

function retroceder(){
	if (!pausa){
		posicionActual--;
		if (posicionActual==-1){
			posicionActual = proyecto.length-1;
		}
		sentido = "retroceder";
		
		cambiarInfoProyecto();
	}
}

function subir(p){
	if (statusNavegacion == "interna"){

			clickUsuario = true;
			btnActivoFooter = $("#iconoLaMaldita");
			$("#iconosFooter a").css({"background-position":"top"});
			$("#iconoLaMaldita").css({"background-position":"bottom"});
			accesoDirectoLaMaldita = true;
			for (i=0;i<proyecto.length;i++){
				if (proyecto[i].nombre == p){
					posicionActual = i;
				}
			}
			sentido = "subir";
			cambiarInfoProyecto();
		
		$("#menuInterna").css({"display":"none"});
			$("#contenedorInterna").css({"display":"none"});
			//$("#menuInterna").fadeOut("normal");
			//$("#contenedorInterna").fadeOut("normal", function(){
			$("#interna").html("");
			$("body").css({"overflow":"hidden"});
			$("#interna").css({"width":"10px"});

	}else{
		if (accesoDirectoLaMaldita == false && pausa == false){
			
			clickUsuario = true;
			btnActivoFooter = $("#iconoLaMaldita");
			$("#iconosFooter a").css({"background-position":"top"});
			$("#iconoLaMaldita").css({"background-position":"bottom"});
			
			
			accesoDirectoLaMaldita = true;
			cerrarSiEstaAbierta(false);
			for (i=0;i<proyecto.length;i++){
				if (proyecto[i].nombre == p){
					posicionActual = i;
				}
			}
			sentido = "subir";
			cambiarInfoProyecto();
		}
	}
}

function openHistory(hash){
	if (hash != ""){
		for (i=0 ; i<proyecto.length ; i++){
			if (proyecto[i].nombre == hash){
				
				if (i>posicionActual){
					posicionActual=i;
					sentido = "avanzar";
					cambiarInfoProyecto();
				}
				if (i<posicionActual){
					posicionActual=i;
					sentido = "retroceder";
					cambiarInfoProyecto();
				}
				cerrarSiEstaAbierta(true);
				break;
			}
		}
	}
}

function clickEntrar(){
	statusNavegacion = "interna";
	
	if (accesoDirectoLaMaldita==false){
		btnActivoFooter = this;
		$("#iconosFooter a").css({"background-position":"top"});
	}
	$("body").css({"scrollX":"0px"});
	$("#interna").css({"display":"block", "width":"90000px"});
	$("#infoProyecto").css({"display":"none"});
	$("#navegacionFlechas").css({"display":"none"});
	//$("#infoProyecto, #navegacionFlechas").fadeOut("normal", function(){
		var target;
		if (isIE==true){
			//target="-ie";
			target="";
		}else{
			target="";
		}
		$("#preLoading").css({"background-image":"url(archivos/loading"+loadingPath+"/loading_0"+Math.floor(Math.random()*9)+".png)"});
		$("#preLoading").css({"display":"block"});
		$("#interna").load(baseContenidos + "contenidos?lang=" + language, {numProyecto:proyecto[posicionActual]['id']}, function(){																					 
			$("body").css({"overflow":"auto"});
		});
	//});
}

function clickCerrarInterna(forzarIconoFull){
	statusNavegacion = "";
	accesoDirectoLaMaldita = false;
	
	$("#menuInterna").css({"display":"none"});
	$("#contenedorInterna").css({"display":"none"});
	//$("#menuInterna").fadeOut("normal");
	//$("#contenedorInterna").fadeOut("normal", function(){
		$("#interna").html("");
		$("body").css({"overflow":"hidden"});
		$("#interna").css({"width":"10px"});
		
		if (forzarIconoFull==true){
			$(btnActivoFooter).css({"background-position":"top"});
			btnActivoFooter = document.getElementById("iconoFull");
			$(btnActivoFooter).css({"background-position":"bottom"});
			$("#infoProyecto, #navegacionFlechas").fadeIn(TIEMPO_FADE);
		}
	//});
	clearTimeout(timeout);
}

function clickMenuInterna(e){
	var o = document.getElementById(e);
	var destino = $(o).offset();
	var posx = $("#interna").offset();
	$("body").scrollTo(destino.left-posx.left+"px", TIEMPO_SLIDE);
}

function cerrarSiEstaAbierta(b){
	switch (statusNavegacion){
		case "interna":
			clickCerrarInterna(b);
			break;
		case "todosLosProyectos":
			cerrarTodosLosProyectos(b);
			break;
	}
}

function clicTn(num){
	clickUsuario = true;
	
	cerrarSiEstaAbierta(false);
	
	$(btnActivoFooter).css({"background-position":"top"});
	btnActivoFooter = document.getElementById("iconoFull");
	$(btnActivoFooter).css({"background-position":"bottom"});
	
	$("#navegacionFlechas").fadeIn(TIEMPO_FADE);
	
	cambiarAddress(posicionActual);
	if (num>posicionActual){
		posicionActual=num;
		sentido = "avanzar";
		cambiarInfoProyecto();
		accesoDirectoLaMaldita = true;
		
	}
	if (num<posicionActual){
		posicionActual=num;
		sentido = "retroceder";
		cambiarInfoProyecto();
		accesoDirectoLaMaldita = true;
	}
}

function cerrarTodosLosProyectos(forzarIconoFull){
	statusNavegacion = "";
	
	if (forzarIconoFull==true){
		$(btnActivoFooter).css({"background-position":"top"});
		btnActivoFooter = document.getElementById("iconoFull");
		$(btnActivoFooter).css({"background-position":"bottom"});
		$("#navegacionFlechas").fadeIn(TIEMPO_FADE);
		
		if (proyecto[posicionActual].titulo+proyecto[posicionActual].categoria+proyecto[posicionActual].bajada != "")
		$("#infoProyecto").fadeIn(TIEMPO_FADE);
	}
	$("#todosLosProyectos").css({"display":"none", "width":9000});
	clickFiltro("todos");
	//$("#todosLosProyectos").fadeOut("normal");
}

function clickFiltro(f){
	if (f == 'todos'){
		arregloTnsProyectos = $(".contenedorThumbs div");
	}else{
		arregloTnsProyectos = $(".contenedorThumbs").find("."+f);
	}
	$("body").scrollTo("0px", 200);
	//acomodarTnsProyectos(arregloTnsProyectos);
	$("#todosLosProyectos .contenedorThumbs div").css({"display":"none"});
	$(arregloTnsProyectos).css({"display":"block"});
}

function cambiarAddress(pos){
	if(location.href.match(/\/en/)){
		location.href = "../../en/#/"+proyecto[pos].nombre;
	} else {
		location.href = "../#/"+proyecto[pos].nombre;
	}
}



/*
*************************************************************************
*/

/*
*************************************************************************
Interna
*************************************************************************
*/

function acomodarTextos(){
	if ($("#menuInterna a")[1] != undefined)
	$("#menuInterna a:first").addClass("btnActivoInterna");
	
	$("#menuInterna a").click(function(){
		$("#menuInterna a").removeClass("btnActivoInterna");
		$(this).addClass("btnActivoInterna");
	});
	
		var arregloTextos = null;
		arregloTextos = $(".modulo > .texto");
		for (i=0 ; i<arregloTextos.length ; i++){
			$(arregloTextos[i]).find("hr:last").after("<table border='0' cellspacing='0' cellpadding='0'><tr><td></td></tr></table>");
			var arregloParrafos = null;
			arregloParrafos = $(arregloTextos[i]).find("p,h3, ul");
			for (j=0 ; j<arregloParrafos.length ; j++){
				$(arregloTextos[i]).find("td:last").append($(arregloParrafos[j]));
				$(arregloParrafos[j]).css({"visibility":"visible"});
				var pos = $(arregloParrafos[j]).position();
				if ((pos.top + $(arregloParrafos[j]).height()) > $(".modulo").height()){
					$(arregloTextos[i]).find("tr:last").append("<td></td>");
					$(arregloTextos[i]).find("td:last").append($(arregloParrafos[j]));
				}
			}
		}
		var off = $("#contenedorInterna").offset();
		var agregado = $(window).width() - off.left - $("#contenedorInterna div:last").width();
		$("#interna").css({"width":$("#contenedorInterna").width()+agregado+"px"});
		$("#contenedorInterna").css({"display":"none", "visibility":"visible"});
		//$("#contenedorInterna").css({"visibility":"visible"});
		$("#preLoading").css({"display":"none"});
		$("#contenedorInterna, #menuInterna").fadeIn(250);
	}
	
/*
*************************************************************************
*/
