//Funciones

//Funciones para modificar el layout segun el tamaño del navegador
var animSpeed = 1000;

function resize1024(s){	//Cuando baja a 1024x768

	$("#logo img").dequeue().animate({ //Logo
		width:"146px", height:"50px"
	}, s);
	
	$("#exito").css("top", "458px");
	
	$("header").dequeue().animate({ //Header completo
		top:"35px"
	}, s);
	
	$(".nav-g").dequeue().animate({ //Botones izquierdos grandes
		fontSize:"14px",
		paddingTop:"6px",
		paddingRight:"8px",
		paddingBottom:"6px",
		paddingLeft:"8px"
	}, s);
	
	$("#bgCentral").dequeue().animate({ //Contenedor central de imagenes
		top:"-100px"
	}, s);
	
	/*$(".nav-ch").dequeue().animate({ //Botones izquierdos chicos
		fontSize:"12px",
		paddingTop:"5px",
		paddingRight:"7px",
		paddingBottom:"5px",
		paddingLeft:"7px",
	}, s);*/
	
	$("header nav ul").dequeue().animate({ //Botonera izquierda completa
		marginTop:"34px"
	}, s);
	
	$("#secImg, #home").dequeue().animate({ //Contenedor central de imagenes
		top:"124px",
		left:"470px",
		marginLeft:"-260px"
	}, s);
	
	$("aside").dequeue().animate({
		top:"115px",
		left:"470px",
		marginLeft:"330px"
	}, s);
	
	$("#h3Sup").dequeue().animate({
		marginTop:"50px"
	}, s);
	
	$("#h3Inf").dequeue().animate({
		top:"480px"
	}, s);
	
	$("#secTxt h2").first().dequeue().animate({ //Botonera izquierda completa
		marginTop:"65px",
		fontSize:"18px"
	}, s);
	
		$(".homeBg").css("background-position","center 475px, center -100px");

};

function resize1280(s){	//Cuando sube a mas de 1280x1024

	$("#logo img").dequeue().animate({ //Logo
		width:"202px", height:"72px"
	}, s);
	
	$("#exito").css("top", "525px");
	
	$("header").dequeue().animate({ //Header completo
		top:"100px"
	}, s);
	
	$(".nav-g").dequeue().animate({ //Botones izquierdos grandes
		fontSize:"17px",
		paddingTop:"6px",
		paddingRight:"12px",
		paddingBottom:"6px",
		paddingLeft:"12px"
	}, s);
	
	$("header nav ul").dequeue().animate({ //Botonera izquierda completa
		marginTop:"70px"
	}, s);
	
	$("#secImg, #home").dequeue().animate({ //Contenedor central de imagenes
		top:"245px",
		left:"50%",
		marginLeft:"-280px"
	}, s);
	
	$("#bgCentral").dequeue().animate({ //Contenedor central de imagenes
		top:"0px"
	}, s);

	
	$("aside").dequeue().animate({
		top:"236px",
		left:"50%",
		marginLeft:"310px"
	}, s);
	
	$("#h3Sup").dequeue().animate({
		marginTop:"155px"
	}, s);
	
	$("#h3Inf").dequeue().animate({
		top:"610px"
	}, s);
	
	$("#secTxt h2").first().dequeue().animate({ //Botonera izquierda completa
		marginTop:"128px",
		fontSize:"18px"
	}, s);
	
	$(".homeBg").css("background-position","center 600px, center top");

};

function resize1500(a){
	$("#barra-iz").dequeue().animate({
		width:""+a+"%"
	}, animSpeed);
	
	$("header").dequeue().animate({
		left:""+a+"%"
	}, animSpeed);
	
	$("#bgIz").dequeue().animate({
		left:""+a+"%"
	}, animSpeed);
	
	$("#copy-info").dequeue().animate({
		left:""+a+"%"
	}, animSpeed);		
};

function resize740(s){
	$("#secImg, #home, aside").dequeue().animate({ //Contenedor central de imagenes
		top:"124px",
		left:"50%"
	}, s);
	
	$("#exito").css("top", "458px");
	
	$("#bgCentral").dequeue().animate({ //Contenedor central de imagenes
		top:"-100px"
	}, s);
	
	$("#h3Sup").dequeue().animate({
		marginTop:"50px"
	}, s);
	
	$("#h3Inf").dequeue().animate({
		top:"480px"
	}, s);
	
	$("header").dequeue().animate({ //Header completo
		top:"50px"
	}, s);
	
	$("header nav ul").dequeue().animate({ //Botonera izquierda completa
		marginTop:"22px"
	}, s);
	
	$("#secTxt h2").first().dequeue().animate({ //Botonera izquierda completa
		marginTop:"78px"
	}, s);
	
	$("#logo img").dequeue().animate({ //Logo
		width:"200px", height:"69px"
	}, s);
	
	$(".nav-g").dequeue().animate({ //Botones izquierdos grandes
		fontSize:"17px",
		paddingTop:"6px",
		paddingRight:"12px",
		paddingBottom:"6px",
		paddingLeft:"12px"
	}, s);

	$(".homeBg").css("background-position","center 475px, center -100px");
}

function winResize(s){
	
	if($(window).width() > 1100 && $(window).height() < 740) {
		resize740(s);
	} else if($(window).width() < 1100){
		resize1024(s);
	} else {
		resize1280(s);
	};
	
	if($(window).width() > 1500){
		resize1500(15)
	} else {
		resize1500(3)
	};
	
};

//Mas Info, funcion completa. Llamada en script y Ajax, dentro de "contFadein()" ----------------------
var altoInfo;
var altoDif;
var infoMargin;

function resetInfo() {//Oculta todos los info y vuelve la imagen a su lugar
	$(".p-info").stop(true).fadeTo("fast", 0, function(){ $(".p-info").hide(); });
	$("#secImg").dequeue().animate({
		marginTop:"0px"
	}, 500);
	
	$(".masInfo").removeClass("desplegado");
}

function masInfo(){//Funcion general que muestra y oculta la info, ademas de mover la ventana y demás.
	var $pInfo = $(".p-info");
	var indxInf = 0;
	$pInfo.fadeTo(0,0).hide();
	
	$(".masInfo").click(function(){	
		
		indxInf = $("#listImg li .masInfo").index(this);
		
		if( !($(this).hasClass("desplegado")) ) {
			
			$pInfo.eq(indxInf).stop(true).show().fadeTo("fast", 1);
			altoInfo = $pInfo.eq(indxInf).height();//Calcula el tamaño de la ventana en cuestion, con el numero de index
				
			if ($(window).width() < 1100 || $(window).height() < 680){//Se fija cuanto tiene que moverse
				infoMargin = 579;
			} else {
				infoMargin = 700;
			}
			
			if ( $(window).height() < altoInfo + infoMargin) {
				altoDif = (altoInfo + infoMargin + 20) - $(window).height();
				$("#secImg").dequeue().animate({
					marginTop:"-"+altoDif+"px"
				}, 500)
			};
			
			$(this).addClass("desplegado");//Indica que ya fue cliqueado				
		} else {
			resetInfo();			
		};					
	});
};

//Funciones para la GALERIA -----------------------------------------------------------------------------------
function fadeImg(imgIndex, $l, $gSpeed) {//Funcion para que se valla una imagen
	$("#listImg li").eq(imgIndex).css({"z-index":"400"}).stop(true).animate($l,$gSpeed).fadeOut($gSpeed).dequeue();
}

function newImg(imgIndex) { //Funcion para que aparesca una nueva imagen
	$("#listImg li").eq(imgIndex).css({"z-index":"200","margin":"0px"}).stop(true).show().fadeTo(0,1)
}

function pagerColor(imgIndex){	
		$("#pager li a").css("background-image","url(img/disc-cel.png)");
		$("#pager li a").eq(imgIndex).css("background-image","url(img/disc-neg.png)");
}

//Funcion para poner los circulitos del pager
function initPager(){
	
	var $cantLi = $("#listImg li").size();
	
	for( i=1 ; i <= $cantLi; i++) {
		$("#pager").append('<li><a href="JavaScript:void(0);">&nbsp;</a></li>')
	}
		
}

function startGal() {
	var imgIndex = 0;
	var listSize = $("#listImg li").length - 1;
	var $ford = $("#galFord");
	var $back = $("#galBack");
	var $left = {"margin-left":"100px"};
	var $right = {"margin-left":"-100px"};
	var $gSpeed = 400;
	
	initPager();
	pagerColor(imgIndex);
	
	$("#listImg li").not(":eq("+imgIndex+")").fadeTo(0,0).hide();
	$ford.fadeTo(0,0.5); $back.fadeTo(0,0.5);
	$("#galFord, #galBack").hover(function(){
		
		$("#galFord, #galBack").fadeTo("fast", 0.5);
		$(this).stop(true).fadeTo("fast",1).addClass("galHov");
	
	}, function(){	
	
		$(this).stop(true).fadeTo("fast",0.5).removeClass("galHov");
		
		setTimeout(function(){ 
			if ( !($ford.hasClass("galHov") || $back.hasClass("galHov")) ){
			
				$("#galFord, #galBack").fadeTo(1000, 0);
			
			};
		}, 1000);

	});
	
	$back.click(function(){
		
		resetInfo();
		
		fadeImg(imgIndex, $left, $gSpeed);
		
		if(imgIndex > 0){
			imgIndex--;
		} else {
			imgIndex = listSize;
		};
		
		newImg(imgIndex);
		pagerColor(imgIndex);
		
	});
	
	$ford.click(function(){
		
		resetInfo();
		
		fadeImg(imgIndex, $right, $gSpeed);//La funcion para desaparecer la Imagen. Se ejecuta ANTES de renovar el index, para que sepa cual tiene que irse
		
		if(imgIndex < listSize){//Calculamos el index
			imgIndex++;
		} else {
			imgIndex=0;
		};
		
		newImg(imgIndex); //Aparece la ueva imagen, se ejecuta DESPUES de renovar el index, así sabe cual tiene que aparecer.
		pagerColor(imgIndex);
		
	});
	
	$("#pager li a").click(function(){
		
		resetInfo();
		
		var $auxG = imgIndex;
		
		imgIndex = $("#pager li a").index(this);
		
		if ($auxG < imgIndex) {
			fadeImg($auxG, $right, $gSpeed);//La funcion para desaparecer la Imagen. Se ejecuta ANTES de renovar el index, para que sepa cual tiene que irse
		} else {
			fadeImg($auxG, $left, $gSpeed);//La funcion para desaparecer la Imagen. Se ejecuta ANTES de renovar el index, para que sepa cual tiene que irse
		};
		
		newImg(imgIndex); //Aparece la ueva imagen, se ejecuta DESPUES de renovar el index, así sabe cual tiene que aparecer.
		pagerColor(imgIndex);
	
	});
	
};

//Funcion para la animación de los thumbnails

function animThumbs(i,zI){
	$("aside li").eq(i).css("left","-70px")
					   .css("z-index",zI)
					   .fadeIn(400)
					   .animate({left:"0px"}, 400).dequeue();
}

function showThumbs(){
	$("aside li").hide();
	
	var cantThumbs = $("aside ul li").length;
	var i = 0;
	var zI = 250;

	var interval = setInterval(
                function() { 
                        animThumbs(i,zI);
                        i++;
                        zI--;
                        if( i >= cantThumbs) {clearInterval(interval)};
                } , 200);
	
}

//Funcion para inicializar galeria del home
function hGalClick(a, $hSpeed, $homeTipo, $hGal, $hIndx){//Las funciones que se ejecutan al hacer click en los botones
	
	$(a).stop(true).animate({"margin-left":"10px"}, $hSpeed/2);
	
	$homeTipo.not(":eq("+$hIndx+")").stop(true).delay($hSpeed*2).animate({"margin-left":"0px"}, $hSpeed);
	
	$hGal.eq($hIndx).css({"z-index":"200","margin-top":"0px"}).stop(true).show().fadeTo($hSpeed/3, 1);
	
	$hGal.not(":eq("+$hIndx+")").css("z-index","400").stop(true).animate({"margin-top":"120px"}, $hSpeed).fadeOut($hSpeed).dequeue();

}

function hGalStart(){
	
	var $hSpeed = 400;
	
	var $hGal = $("#homeGal li");
	
	$hGal.not(":eq(0)").hide();
		
	var $hIndx = 0;
	var $homeTipo = $("#homeTipo li a");
	
	$homeTipo.eq(0).css("margin-left","10px");
	
	$homeTipo.click(function(){
		
		$hIndx = $homeTipo.index(this);

		hGalClick(this, $hSpeed, $homeTipo, $hGal, $hIndx);
		
		clearInterval(hIntvl);
			
	});
	
	$homeTipo.hover(function(){
		$(this).stop(true).animate({"margin-left":"10px"}, $hSpeed/2);		
	}, function(){
		$homeTipo.not(":eq("+$hIndx+")").stop(true).animate({"margin-left":"0px"}, $hSpeed/2);
	});

	var hIntvl = setInterval(function(){
		
		if($hIndx < 2){
			$hIndx++;
		} else {
			$hIndx = 0;
		};
		
		hGalClick($homeTipo.eq($hIndx), $hSpeed, $homeTipo, $hGal, $hIndx);
	
	}, 4000);
		
};
	
//Rollovers de la botonera principal
	
function izHover(){	
	
	var $izA = $("#navIz ul a");
	
	$izA.not(".izClick").hover(function(){
		
		$(this).stop(true).animate({backgroundPosition:"0 -90px", "color":"#FFF"}, "fast");
		
		$(this).click(function(){ 
			$izA.not(this).removeClass("izClick").delay(500).animate({backgroundPosition:"0 0", "color":"#1079A1"}, "slow");
			$(this).addClass("izClick"); 
		});
		
	}, function(){
		
		if(!($(this).hasClass("izClick"))) {
		
			$(this).animate({backgroundPosition:"0 0", "color":"#1079A1"}, "slow");
		
		};
		
	});
	
	$("#logo").click(function(){
		$izA.removeClass("izClick").delay(500).animate({backgroundPosition:"0 0", "color":"#1079A1"}, "slow");
	})
	
};

//Acordeon de Que hacemos
function qhAcor(){
	
	var $qhWrap = $(".qhWrapper");
	var $qhH = $(".qhH2");
	
	var qhIndx = 0;
	var hThis = new Array();
	
	for(i=0;i<3;i++){//Con eso saco el alto de cada elemento, para poder volverlo a la normalidad. Lo guardo en un array.
		hThis[i] = $qhWrap.eq(i).height() + 48; //Hay que sumarle una pequeña cantidad supongo que resultante de los margenes y demas.
	}
	
	$qhH.not(":eq("+qhIndx+")").css("margin-top","5px")
	$(".qhWrapper").not(":eq("+qhIndx+")").css("height","0px");
	
	$qhH.click(function(){
		
		qhIndx = $qhH.index(this);
		$qhWrap.not(":eq("+qhIndx+")").animate({"height":"0px"}, 500);
		$qhWrap.eq(qhIndx).animate({"height":""+hThis[qhIndx]+"+px"}, 400);
		
	});
};

//Fancybox

function fancyOn(){
	$(".fancy").fancybox({
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'overlayColor'	: 	'#000',
		'padding'		: 	0
	});
}

//Fondos
function selctBg(){
	
	var navPort = $("#navIz li a:eq(0), #navIz li a:eq(1), #navIz li a:eq(2), .navPort");
	var navText = $("#navIz li a:eq(3), #navIz li a:eq(4), #navIz li a:eq(6)");

	
	$("#bgLec").hide().css("z-index", "-5");
	$("#bgCentral").hide();
	
	$("#logo").click(function(){
		setTimeout(function(){
			$("#bg li").eq(1).fadeOut("normal");
		}, 700);
		$("#bg li").eq(3).hide();
	})
	
	navPort.click(function(){
		$("#bg li").eq(1).fadeIn("fast");
		$("#bg li").eq(3).hide();
	})
	
	navText.click(function(){
		setTimeout(function(){
			$("#bg li").eq(3).fadeIn("fast");	
		}, 500);
	})
}

//Validación de formulario

//Contacto, enviar el formulario sin refrescar
function form(){
	
$("#exito").hide();
function exito() {
	$("#exito").fadeIn("fast");
	setTimeout(function(){
		$("#exito").fadeOut(2000);
	}, 3000)
}
	
$("#f-simple").validate({ 
      event: "blur",
      rules: { 
         nombre: { 
            required: true,
            minlength: 3 
         }, 
         mail: { 
            required: true, 
            email: true 
         }, 
         mensaje: { 
            required: true
         }  
      }, 
      messages: { 
         nombre: { 
            required: "&iexcl;Necesitamos tu nombre!",
            minlength: "&iexcl;Tienen que ser al menos 3 letras!" 
         }, 
         mail: { 
            required: "&iexcl;Necesitamos saber tu e-mail!", 
            email: "Tiene que ser un e-mail v&aacute;lido" 
         },
         mensaje: { 
            required: "Requerido" 
         } 
      },
      
	debug: true,
	errorElement: "div",
	errorContainer: $(".error"),
	submitHandler: function(form){
		var $nombre = $("#nombre").val();
		var $mail = $("#mail").val();
		var $asunto = $("#asunto").val();
		var $mensaje = $("#mensaje").val();
			
		var dataString = 'nombre='+ $nombre + '&mail=' + $mail + '&asunto=' + $asunto + '&mensaje=' + $mensaje;	

		$.ajax({
		  type: "POST",
		  url: "../enviar.php",
		  data: dataString,
		  error:function(){
		  	exito();
		  	//alert("Hubo un error al enviar su solicitud. Por favor, utilice el e-mail o intente mas tarde. Gracias.");
		  },
		  success: function() { 
		  	exito();
		  	//$("#exito").fadeIn("fast");
			/*$('#formulario').html("<div id='message'></div>");
			$('#message').html("<h2>Mensaje Enviado!</h2>")
			.append("<p>Nos pondremos en contacto pronto.</p>")
			.hide()
			.fadeIn(500, function() {
			$('#message').append("<img id='checkmark' src='images/check.png' />");
							   }); //cierra fadein
			*/}
		});	//cierra ajax			
		return false; // return false para que no recargue la pagina cuando se envia el mensaje
	}
}); 

};






