$(document).ready(function() { 
	
    $("ul.sf-menu").superfish({ 
        animation:   {opacity:'show',height:'show'},   // slide-down effect without fade-in 
        dropShadows: true,
        delay: 1200             // 1.2 second delay on mouseout 
    }); 
    $("#el_buscador").click(limpiar);
    $("#boton_buscar").click(buscar);
    $("#sel_ordenar").change(ordenar);
    $("#logo").click(iralhome);

	$.superbox.settings = {
		boxId: "superbox", // Id attribute of the "superbox" element
		boxClasses: "", // Class of the "superbox" element
		overlayOpacity: .8, // Background opaqueness
		boxWidth: "600", // Default width of the box
		boxHeight: "400", // Default height of the box
		loadTxt: "Cargando...", // Loading text
		closeTxt: "Cerrar", // "Close" button text
		prevTxt: "Previous", // "Previous" button text
		nextTxt: "Next" // "Next" button text
	};
	
	$.superbox();
	
	//comienzo slide ofertas
	//To switch directions up/down and left/right just place a "-" in front of the top/left attribute
	//Vertical Sliding
	$('.boxgrid.slidedown').hover(function(){
		$(".cover", this).stop().animate({top:'-260px'},{queue:false,duration:300});
	}, function() {
		$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:300});
	});
	//Horizontal Sliding
	$('.boxgrid.slideright').hover(function(){
		$(".cover", this).stop().animate({left:'325px'},{queue:false,duration:300});
	}, function() {
		$(".cover", this).stop().animate({left:'0px'},{queue:false,duration:300});
	});
	//Diagnal Sliding
	$('.boxgrid.thecombo').hover(function(){
		$(".cover", this).stop().animate({top:'260px', left:'325px'},{queue:false,duration:300});
	}, function() {
		$(".cover", this).stop().animate({top:'0px', left:'0px'},{queue:false,duration:300});
	});
	//Partial Sliding (Only show some of background)
	$('.boxgrid.peek').hover(function(){
		$(".cover", this).stop().animate({top:'90px'},{queue:false,duration:160});
	}, function() {
		$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:160});
	});
	//Full Caption Sliding (Hidden to Visible)
	$('.boxgrid.captionfull').hover(function(){
		$(".cover", this).stop().animate({top:'160px'},{queue:false,duration:160});
	}, function() {
		$(".cover", this).stop().animate({top:'260px'},{queue:false,duration:160});
	});
	//Caption Sliding (Partially Hidden to Visible)
	$('.boxgrid.caption').hover(function(){
		$(".cover", this).stop().animate({top:'70px'},{queue:false,duration:160});
	}, function() {
		$(".cover", this).stop().animate({top:'140px'},{queue:false,duration:160});
	});
	//fin slide ofertas.
});

$(window).load(function(){  
	//initialize after images are loaded  
	$(".img_prod").each(function() {
            //alert("1 er funcion");
           //Get the width of the image
           var width = $(this).width();
           //alert(width+"-"+this.id);
           //Max-width substitution (works for all browsers)
           if (width > 140) {
             $(this).css("width", "140px");
           }
	});
	
	$(".img_prod").css("visibility", "visible");
	$(".logo_marca").css("visibility","visible");
	
	$(".cont_imagen img").each(function() {
            //alert("1 er funcion");
           //Get the width of the image
           var width = $(this).width();
           //alert(width+"-"+this.id);
           //Max-width substitution (works for all browsers)
           if (width > 140) {
             $(this).css("width", "140px");
           }
	});
	
	$(".cont_imagen img").css("visibility", "visible");
 }
);  

var local = "http://www.casasilvia.com/";

function limpiar(){
	$("#el_buscador").val("");
}

function ordenar(){
	var cat = $("#cat").val();
	var marca = $("#marca").val();
	var page = $("#page").val();
	var o = this.value;
	/*var url = "http://"+window.location.host+"/acceder/articulos/"+cat;*/
	var url = "http://"+window.location.host+"/articulos/"+cat;
	if(marca!="") url+="/marca/"+marca;
	url+="/orden/"+o;
	if(page!="") url+= "/pagina/"+page;
	window.location=url;
}

function recargar(){
	window.location.reload();
}

function iralhome(){
	window.location="http://"+window.location.host+"/home";
}

function obtenerArticulos(){
	var stringid = this.id;
	var aux = stringid.split("_");
	var catv = aux[0];
	var idv = aux[1];
	var pagina = "lib/obtenerArticulosFront.php";
	x = $("#frame_resultados");
	$.ajax({
	           async:true,
	           type: "POST",
	           dataType: "html",
	           contentType: "application/x-www-form-urlencoded",
	           url:pagina,
	           data:"id="+idv+"&cat="+catv+"&marca="+marca,
	           success:mensaje,
	           timeout:4000
	         }); 
	return false;
}

function buscar(){
 if($("#el_buscador").val().length<2) {
 	alert('Su búsqueda debe tener al menos cuatro caracteres.');
 	return false;
 }
 if( ($("#el_buscador").val()!='') && ($("#el_buscador").val()!='Busque su producto')) 
 		$("#form_buscador").submit();
 	else 
 		return false;
}

function elegirComparar(esto){
	if(esto.checked==true){
		var url="accion=guardar";
	}
	else
	{
		var url="accion=eliminar";
	}
	
	var url = url+"&id="+esto.value;
	var pagina = local+"lib/sesiones_comparar.php";
	
	
	$.ajax({
	           async:true,
	           type: "POST",
	           dataType: "html",
	           contentType: "application/x-www-form-urlencoded",
	           url:pagina,
	           data:url,
	           timeout:4000
	        }); 
	         
	return false;
}

//Verifica que existan mas de un articulo seleccionado.
function validarComparar(){
	var pagina = local+"lib/validar_comparar.php";
	$.ajax({
	           async:true,
	           type: "POST",
	           dataType: "html",
	           contentType: "application/x-www-form-urlencoded",
	           url:pagina,
	           success:resultadoComparar,
	           timeout:4000
	        }); 
   return false;	
}

function resultadoComparar(a){
	if(a>=2){
		window.location = local+"comparar";
	}
	else
	{
		alert('Debe elegir mas de un artículo para comparar');
	}
}

//Al entrar a una categoría se resetean las comparaciones.
function limpiarSesionComparar(){
	var pagina = local+"lib/limpiar_sesion.php";
	$.ajax({
	           async:true,
	           dataType: "html",
	           cache:false,
	           contentType: "application/x-www-form-urlencoded",
	           url:pagina,
	           timeout:4000
	        }); 
   return false;	
}

function similares(id){
	var x = $("#sel_simil").val();
	window.location = local+"ver-similares/"+id+"/"+x;
}

function prueba(a){
	alert(a);
}

function validaAvanzada(){
	if( ($("#palabra").val()=="") &&
		($("#selmenuweb").val()==0) &&
		($("#selmarcas").val()==0) ){
			alert("Debe completar algún criterio de búsqueda");
		}
		else
		{
			document.form_busq_av.submit();
		}
}

function validaEmail(){
	var msg="";
	if($("#nombre").val()==""){
		msg+="- Su nombre está incompleto\n";
		$("#nombre").focus();
	}
	if($("#telefono").val()==""){
		msg+="- Su teléfono está incompleto\n";
		$("#telefono").focus();
	}
	if($("#email").val()==""){
		msg+="- Su email está incompleto\n";
		$("#email").focus();
	}else
	{
		if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('email').value)){
			msg+="- El email es incorrecto\n";
			$("#email").focus();
		}
	}
	if($("#localidad").val()==""){
		msg+="- Su localidad está incompleta\n";
		$("#localidad").focus();
	}
	if($("#mensaje").val()==""){
		msg+="- Debe incluir un mensaje\n";
		$("#mensaje").focus();
	}
	if(msg!=""){
		alert("Han ocurrido los siguientes errores:\n"+msg);
		return false;
	}else
	{
		return true;
	}
}

function validaCreditos(){
	var msg="";
	if($("#nombre").val()==""){
		msg+="- Su nombre está incompleto\n";
		$("#nombre").focus();
	}
	if($("#apellido").val()==""){
		msg+="- Su apellido está incompleto\n";
		$("#apellido").focus();
	}
	if($("#dni").val()==""){
		msg+="- Su dni está incompleto\n";
		$("#dni").focus();
	}
	if($("#cuit").val()==""){
		msg+="- Su cuit/cuil está incompleto\n";
		$("#cuit").focus();
	}
	if($("#localidad").val()==""){
		msg+="- Su localidad está incompleta\n";
		$("#localidad").focus();
	}
	if($("#domicilio").val()==""){
		msg+="- Su domicilio está incompleto\n";
		$("#domicilio").focus();
	}
	if($("#telefono").val()==""){
		msg+="- Su telefono está incompleto\n";
		$("#telefono").focus();
	}
	if($("#email").val()==""){
		msg+="- Su email está incompleto\n";
		$("#email").focus();
	}
	else
	{
		if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('email').value)){
			msg+="- El email es incorrecto\n";
			$("#email").focus();
		}
		else
		{
			if($("#confirmaemail").val()!=$("#email").val()){
				msg+="- El email y su confirmación no coinciden.";
				$("#confirmaemail").focus();
			}
		}
	}
	
	if(msg!=""){
		alert("Han ocurrido los siguientes errores:\n"+msg);
		return false;
	}
	else
	{
		return true;
	}
}

function validaEmail2(){
	var msg="";
	if($("#nombre").val()==""){
		msg+="- Su nombre está incompleto\n";
		$("#nombre").focus();
	}
	if($("#documento").val()==""){
		msg+="- Su documento está incompleto\n";
		$("#documento").focus();
	}
	if($("#direccion").val()==""){
		msg+="- Su dirección está incompleta\n";
		$("#direccion").focus();
	}
	if($("#localidad").val()==""){
		msg+="- Su localidad está incompleta\n";
		$("#localidad").focus();
	}
	if($("#cp").val()==""){
		msg+="- Su Código Postal está incompleto\n";
		$("#cp").focus();
	}
	if($("#provincia").val()==""){
		msg+="- Su provincia está incompleta\n";
		$("#provincia").focus();
	}
	if($("#pais").val()==""){
		msg+="- Su pais está incompleto\n";
		$("#pais").focus();
	}
	if($("#telefono").val()==""){
		msg+="- Su teléfono está incompleto\n";
		$("#telefono").focus();
	}
	if($("#email").val()==""){
		msg+="- Su email está incompleto\n";
		$("#email").focus();
	}else
	{
		if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('email').value)){
			msg+="- El email es incorrecto\n";
			$("#email").focus();
		}
	}
	if(msg!=""){
		alert("Han ocurrido los siguientes errores:\n"+msg);
		return false;
	}else{
		return true;
	}
}
function validaEmail4(){
	var msg="";
	if($("#nombre").val()==""){
		msg+="- Su nombre está incompleto\n";
		$("#nombre").focus();
	}
	if($("#email").val()==""){
		msg+="- Su email está incompleto\n";
		$("#email").focus();
	}
	if($("#consulta").val()==0){
		msg+="- Consulta no realizada\n";
		$("#consulta").focus();
	}else
	{
		if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('email').value)){
			msg+="- El email es incorrecto\n";
			$("#email").focus();
		}
	}
	if(msg!=""){
		alert("Han ocurrido los siguientes errores:\n"+msg);
		return false;
	}else{
		return true;
	}
}
