//todas las acciones que hay que hacer cuando se abre.
$(document).ready(function(){
						   
	$(".elementoListado[rel='lista']").colorbox({slideshow:true,slideshowAuto:false});
	
	$('#irIzquierda').bind('mousedown', function() {
 		timerIzda=setInterval(irIzda, 2);
	});
	
	$('#irIzquierda').bind('mouseup', function() {
 		clearInterval(timerIzda);
	});	
	
	$('#irDerecha').bind('mousedown', function() {
 		timerDere=setInterval(irDere, 2);
	});
	
	$('#irDerecha').bind('mouseup', function() {
 		clearInterval(timerDere);
	});
	
	$('.fotoListado').mouseover(function() {
		  $(this).stop();
		  $(this).animate({
			padding: '0'
		  }, 200
		  );
		  
		  $(this).find('span').stop();
		  $(this).find('span').animate({
			height: '170',
			width: '170'
		  }, 200
		  );		  
		  
		  $(this).find('img').stop();
		  $(this).find('img').animate({
			height: '160',
			width: '160'
		  }, 200
		  );		  
		  
		  
	});
	
	$('.fotoListado').mouseout(function() {
		  $(this).stop();
		  
		  $(this).animate({
			padding: '17'
		  }, 200
		  );
		  
		  $(this).find('span').stop();
		  $(this).find('span').animate({
			height: '136',
			width: '136'
		  }, 200
		  );		  
		  
		  $(this).find('img').stop();
		  $(this).find('img').animate({
			height: '130',
			width: '130'
		  }, 200
		  );
		  
	});	
	
	
	$('.fotoListado').each(function() {
	
		  $(this).stop();
		  
		  $(this).animate({
			padding: '17'
		  }, 0
		  );
		  
		  $(this).find('img').stop();
		  $(this).find('img').animate({
			height: '130',
			width: '130'
		  }, 0
		  );	
	
	});
						 
});

function validarEmail(mail) {
	var exr = /^[0-9a-z_\-\.]+@[0-9a-z\-\.]+\.[a-z]{2,4}$/i;
	return exr.test(mail);
}


function irDere(){
	
	pos=$(".listaFotos").position();
	
	limite=$(".listaFotos").width()*-1;
	
	//alert (pos.left);
	if (pos.left>limite){
		$(".listaFotos").css("left",pos.left-3)	
	}else{
		clearInterval(timerIzda);
	}	
	
}

function irIzda(){
	
	pos=$(".listaFotos").position();
	//alert (pos.left);
	if (pos.left<0){
		$(".listaFotos").css("left",pos.left+3)	
	}else{
		clearInterval(timerIzda);
	}
	
}


function validarContacto(){
	
	error=false;
	
	if ($("#nombre").val()==""){
		error=true;
	}else{
	}
	
	if ($("#email").val()=="" || !validarEmail($("#email").val()) ){
		error=true;
	}else{
	}
	
	if ($("#mensaje").val()==""){
		error=true;
	}else{
	}	
	
	if (error){
		
		$.colorbox({html:"<div style='height:200px; width:300px;'><div style='padding:10px; padding-top:25px; font-size:20px; color:#7d7d7d; text-align:center;'>Alguno de los campos obligatorios no est&aacute; relleno o es incorrecto.</div></div>"});
		
	}else{
		
		$("#error").hide();
	
			$.ajax({
			   type: "POST",
			   url: "/includes/guardarContacto.php",
			   data: "nombre="+$("#nombre").val()+"&empresa="+$("#empresa").val()+"&email="+$("#email").val()+"&mensaje="+$("#mensaje").val(),
			   success: function(recep){
				   
				   recep=recep.split("[[");
				   msg=recep[0];
				   
				   if (msg=="OK"){
					   
						$.colorbox({html:"<div style='height:200px; width:300px;'><div style='padding:10px; padding-top:25px; font-size:25px; text-align:center;'>Su petici&oacute;n se ha enviado correctamente.</div></div>"});	
				   }else{
					
						$.colorbox({html:"<div style='height:200px; width:300px;'><div style='padding:10px; padding-top:25px; font-size:25px; text-align:center;'>No se ha podido enviar su petici&oacute;n, por favor, int&eacute;ntelo de nuevo en unos minutos.</div></div>"});	
					
				   }
			   }
			});		
	
	}
	
}

function expandirNoticia(idNot){
	
	//cierro todo.
	$(".contenidoNoticia").addClass("contraido");
	$(".imagenBoton").removeClass("activo");
	$(".contenidoNoticia").slideUp();	
	
	if ($("#contenidoNoticia_"+idNot).hasClass("contraido")){
		
		$("#contenidoNoticia_"+idNot).removeClass("contraido");
		$("#imagenBoton_"+idNot).addClass("activo");
		$("#contenidoNoticia_"+idNot).slideDown();
		
	}else{
		
		$("#contenidoNoticia_"+idNot).addClass("contraido");
		$("#imagenBoton_"+idNot).removeClass("activo");
		$("#contenidoNoticia_"+idNot).slideUp();		
		
	}
}


function verColeccion(idColeccion){
	
	//desmarco todos los botones.
	$(".botonColeccion").removeClass("activo");
	
	//activo el que corresponde
	$("#botonColeccion_"+idColeccion).addClass("activo");
	
	//Cargo el contenido.
	$("#contenedorColecciones").load("/includes/privada_coleccion.php", { 'idColeccion': idColeccion } );
	
	
}

function verPrenda(idPrenda){
	
	$.colorbox({href:'/includes/privada_ventanaPrenda.php?idPrenda='+idPrenda});
	
}

function mostrarLogin(){
	$.colorbox({href:'/includes/privada_login.php'});
}
function mostrarLoginTemporada(idColeccion){
	$.colorbox({href:'/includes/privada_login_temporada.php?idcoleccion='+idColeccion});
}

function logar(){
	
	$.ajax({
	   type: "POST",
	   url: "/includes/logon.php",
	   data: "usuario="+$("#usuario").val()+"&pass="+$("#pass").val(),
	   success: function(msg){
		   
		 if (msg=="KO"){
			$("#error").html("Nombre de usuario o contrase&ntilde;a incorrectos");
		 }else{
			window.location.href = msg;
		 }
		 
	   }
	 });
	
}

function logarTemporada(idcoleccion){
	
	$.ajax({
	   type: "POST",
	   url: "/includes/logon_temporada.php",
	   data: "pass="+$("#pass").val()+"&idcoleccion="+idcoleccion,
	   success: function(msg){
		 if (msg=="KO"){
			$("#error").html("Contrase&ntilde;a incorrecta");
		 }else{
			window.location.href = msg;
		 }
		 
	   }
	 });
	
}
