function ensenar_todo(){
	if ((jQuery("#bloque_precios").css("display")=="none")){
		ensenar_todo_mostrar();
	}else{
		ensenar_todo_esconder();
	}	
}

function ensenar_todo_mostrar(){
		jQuery("#bloque_categorias").fadeIn();
		jQuery("#bloque_fabricantes").fadeIn();
		jQuery("#bloque_precios").fadeIn();
		jQuery("#boton_buscar").fadeIn();
                jQuery(".links_categorias").css("background","url(images/flecha_arriba.png) no-repeat right 3px");
                jQuery(".links_fabricantes").css("background","url(images/flecha_arriba.png) no-repeat right 3px");
                jQuery(".links_precios").css("background","url(images/flecha_arriba.png) no-repeat right 3px");
		jQuery(".close_advanced").fadeIn();
}

function ensenar_todo_esconder(){
		jQuery("#bloque_categorias").fadeOut();
		jQuery("#bloque_fabricantes").fadeOut();
		jQuery("#bloque_precios").fadeOut();
		jQuery("#boton_buscar").fadeOut();
jQuery(".links_categorias").css("background","url(images/flecha_abajo.png) no-repeat right 3px");
                jQuery(".links_fabricantes").css("background","url(images/flecha_abajo.png) no-repeat right 3px");
                jQuery(".links_precios").css("background","url(images/flecha_abajo.png) no-repeat right 3px");
	jQuery(".close_advanced").fadeOut();

}

function riAjaxSearchUpdateResult(result){
	jQuery('#advanced_program').fadeIn();
	jQuery('#advanced_program').html(result);
}

function buscar_todo(){
	jQuery('#advanced_program').fadeIn();
	jQuery('#advanced_program').html(result);
}

function cargar_final(){
		jQuery("#precios").fadeIn();
		jQuery("#fabricantes").fadeIn();
		jQuery("#advanced_program").fadeIn();
		//jQuery("#boton_buscar").fadeIn();
		//jQuery("#boton_success").css("background","url(images/cerrar.png) no-repeat");
		jQuery("#boton_success").css("width","21px");
		jQuery("#boton_success").css("height","21px");
		jQuery("#boton_success").css("border","none");
}

function cargar_categorias(){
	  var categoria_activa;
	  categoria_activa=jQuery("#categoria_activa").attr("value");  
	  jQuery.ajax({
		url:	"ajax/cargar_categorias.php",
		type: "post",
		data: "categoria="+categoria_activa,
		beforeSend:  function() {
		jQuery("#categorias").html("<center><img alt=\"Cargando Productos....\" src=\"images/calculando.gif\" /></center>");
		},
			success: function(responseText, statusText){
			jQuery('#categorias').html(responseText);
			//jQuery("#categorias").css("display","block");      
			//jQuery("#categorias").fadeIn();  
			if (jQuery("#primera_vez").attr("value")=="1"){
				//ensenar_todo_mostrar();
			}
			$(".input_categor").unbind("click");
					$(".input_categor").click(function(evento){ 
						success_advanced("","","","3");
					});
										
		}});  
}
function cargar_fabricantes(datos){
jQuery.ajax({
url:	"ajax/cargar_fabricantes.php",
        type: "post",
        data: datos,
        beforeSend:  function() {
          jQuery('#fabricantes').html("<center><img alt=\"Cargando Productos....\" src=\"images/calculando.gif\" /></center>");
        },
        success: function(responseText, statusText){
                jQuery('#fabricantes').html(responseText);
               
                $(".fabricantes").unbind("click");
                $(".fabricantes").click(function(evento){
                     
                                success_advanced("","","","2");
                });
        }
});  
}
function cargar_precios(datos){
jQuery.ajax({
url:	"ajax/cargar_precios.php",
type: "post",
data: datos,
beforeSend:  function() {
        jQuery('#precios').html("<center><img alt=\"Cargando Productos....\" src=\"images/calculando.gif\" /></center>");
       
},
success: function(responseText, statusText){
        jQuery('#precios').html(responseText);
        //cargar_final();
        $(".precios_generales").click(function(evento){
                        success_advanced("","","","4");
        });

        if (jQuery("#primera_vez").attr("value")=="1"){
                success_advanced();
                ensenar_todo();
        }else{

        }
        jQuery("#primera_vez").attr("value","1");

}
});  
}
function success_advanced(disp_order,quepagina,maximo,resultado,conjava,empezar){
    var fabricant;
    var categories;
    var pfrom;
    var pto;
    var ordenar;
    var precios;
    var categoria_activa;
    categoria_activa=jQuery("#categoria_activa").attr("value");
    fabricant="";
    categories="";
    ordenar="";
    precios="";
    if (!disp_order){
        disp_order="";
    }
    if (!quepagina){
        quepagina="";
    }
    if (!maximo){
        maximo="";
    }
    if (!resultado){
        resultado="1";
    }
    if (!conjava){
        conjava="1";
    }

    if (!empezar){
        empezar="Abierto";
    }

    $(".fabricantes").each(function(){
        if ($(this).attr('checked')){
                fabricant = fabricant + "&fabricantes_"+$(this).attr('value')+"="+$(this).attr('value');

        }
    }) ;
    $(".input_categor").each(function(){
        if ($(this).attr('checked')){
                categories = categories + "&categories_"+$(this).attr('value')+"="+$(this).attr('value');
        }
    }) ;

    $(".precios_generales").each(function(){
        if ($(this).attr('checked')){
                precios = precios + "&price_range="+$(this).attr('value');
        }
    }) ;

    if ((disp_order=="")||(typeof(disp_order)=="undefined")){
                            if ($(".disp_order").val()!=""){
                                    ordenar ="&disp_order="+$(".disp_order").val()+"&disp_order_adicional="+$(".disp_order").val();
                            }else{
                                    ordenar = "&disp_order=1&disp_order_adicional=1";
                            }
    }else{
                            ordenar = "&disp_order="+disp_order+"&disp_order_adicional="+disp_order;
    }

    if (typeof(quepagina)=="undefined"){
       quepagina = "";
    }else{
    }
    pfrom=jQuery("input[name=pfrom]").attr("value");
    pto=jQuery("input[name=pto]").attr("value");
    if (resultado==1){
            jQuery.ajax({
            url:	"ajax/advanced_result.php",
            type: "post",
                //data: "cPath=$cPath&"+precios+"&"+fabricant +  categories + "&" + "pfrom="+pfrom + "&pto="+pto+ordenar+quepagina,
                data: "cPath="+categoria_activa+"&"+precios+"&"+fabricant +  categories + "&" +ordenar+quepagina,
            beforeSend:  function() {
                //jQuery('#advanced_result').html("Calculando");
                jQuery("#indexCategories").remove();
                jQuery('#humo').html('<p style="text-align:center;margin-top:500px"><img src="images/calculando.gif" /></p>');
                jQuery("#humo").css("display","block");
                },
            success: function(responseText, statusText){

            jQuery("#productListing").remove();
            jQuery("#indexCategories").remove();
            //jQuery('#advanced_result').html(responseText.tratarResponseText());
            jQuery('#advanced_result').html(responseText);
            /*Como esto se ejecuta en la pagina principal, es el jscript_btn.js, pero para que me funcione
             En ajax he realizado lo mismo que es jscript_btn.js en jquery...*/
            jQuery("#humo").css("display","none");
             if (empezar=="Abierto"){
                ensenar_todo_mostrar();
             }
              if (conjava==1){
                $(".btn").each(function(){
                       
                       if ($(this).find('i').length>0){
                          //alert($(this).find('i').length);
                       }else{
                           // alert($(this).find('i').length);
                            $(this).html('<i></i><span><i></i><span></span>'+$(this).attr('id')+'</span>');
                       }

                });
              }



                jQuery("#disp-order-sorter").attr("onchange","");



                $("#disp-order-sorter").change(function(evento){
                        success_advanced($(this).val());
                });

                $("#productsListingListingTopLinks > a").each(function(){
                        $(this).click(function(evento){
                                success_advanced("",$(this).attr("href").substr($(this).attr("href").indexOf("&page="),$(this).attr("href").length));
                                return false;
                        });
                });

                        $("#productsListingListingBottomLinks > a").each(function(){
                        $(this).click(function(evento){
                                success_advanced("",$(this).attr("href").substr($(this).attr("href").indexOf("&page="),$(this).attr("href").length));
                                return false;
                        });
                });
                function smartColumns() {
                if ($("ul.column")){
                        $("ul.column").css({ 'marginRight' : "0px"});
                        $("ul.column").css({ 'marginLeft' : "0px"});
                        var colWrap = $("ul.column").width();
                        var colNum = Math.floor(colWrap /  $("ul.column li").width());
                        var restoCol = Math.floor(colWrap %  $("ul.column li").width());
                        var anchoCol = Math.floor(restoCol / (colNum-1));
                        var colActual=1;
                        $("ul.column li").each(function(){

                        if (colActual==colNum){
                                $(this).css({ 'marginRight' : "0px"});
                                colActual=0;
                        }else{
                                $(this).css({ 'marginRight' : anchoCol});

                        }
                        colActual++;

                        }) ;
                }

                if ($("#crossSell ul.column")){
                        $("#crossSell ul.column").css({ 'marginRight' : "0px"});
                        $("#crossSell ul.column").css({ 'marginLeft' : "0px"});

                        var colWrap = $("#crossSell ul.column").width();
                        var colNum = Math.floor(colWrap /  $("#crossSell ul.column li").width());
                        var restoCol = Math.floor(colWrap %  $("#crossSell ul.column li").width());
                        var anchoCol = Math.floor(restoCol / (colNum-1));
                        var colActual=1;
                        $("#crossSell ul.column li").each(function(){
                        if (colActual==colNum){
                                $(this).css({ 'marginRight' : "0px"});
                                colActual=0;
                        }else{
                                $(this).css({ 'marginRight' : anchoCol});
                        }
                        colActual++;
                        }) ;
                }
                if ($("#featuredProducts ul.column")){
                    $("#featuredProducts ul.column").css({ 'marginRight' : "0px"});
                    $("#featuredProducts ul.column").css({ 'marginLeft' : "0px"});
                    var colWrap = $("#featuredProducts ul.column").width();
                    var colNum = Math.floor(colWrap /  $("#featuredProducts ul.column li").width());
                    var restoCol = Math.floor(colWrap %  $("#featuredProducts ul.column li").width());
                    var anchoCol = Math.floor(restoCol / (colNum-1));
                    var colActual=1;
                    $("#featuredProducts ul.column li").each(function(){
                    if (colActual==colNum){
                            $(this).css({ 'marginRight' : "0px"});
                            colActual=0;
                    }else{
                            $(this).css({ 'marginRight' : anchoCol});
                    }
                    colActual++;
                    }) ;
                }
                if ($("#whatsNew ul.column")){
                    $("#whatsNew ul.column").css({ 'marginRight' : "0px"});
                    $("#whatsNew ul.column").css({ 'marginLeft' : "0px"});
                    var colWrap = $("#whatsNew ul.column").width();
                    var colNum = Math.floor(colWrap /  $("#whatsNew ul.column li").width());
                    var restoCol = Math.floor(colWrap %  $("#whatsNew ul.column li").width());
                    var anchoCol = Math.floor(restoCol / (colNum-1));
                    var colActual=1;
                    $("#whatsNew ul.column li").each(function(){
                    if (colActual==colNum){
                            $(this).css({ 'marginRight' : "0px"});
                            colActual=0;
                    }else{
                            $(this).css({ 'marginRight' : anchoCol});
                    }
                    colActual++;
                    }) ;
                }
                if ($("#specialsDefault ul.column")){
                    $("#specialsDefault ul.column").css({ 'marginRight' : "0px"});
                    $("#specialsDefault ul.column").css({ 'marginLeft' : "0px"});
                    var colWrap = $("#specialsDefault ul.column").width();
                    var colNum = Math.floor(colWrap /  $("#specialsDefault ul.column li").width());
                    var restoCol = Math.floor(colWrap %  $("#specialsDefault ul.column li").width());
                    var anchoCol = Math.floor(restoCol / (colNum-1));
                    var colActual=1;
                    $("#specialsDefault ul.column li").each(function(){
                    if (colActual==colNum){
                            $(this).css({ 'marginRight' : "0px"});
                            colActual=0;
                    }else{
                            $(this).css({ 'marginRight' : anchoCol});
                    }
                    colActual++;
                    }) ;
                }
                //Also purchased
                if ($("#alsoPurchased ul.column")){
                    $("#alsoPurchased ul.column").css({ 'marginRight' : "0px"});
                    $("#alsoPurchased ul.column").css({ 'marginLeft' : "0px"});
                    var colWrap = $("#alsoPurchased ul.column").width();
                    var colNum = Math.floor(colWrap /  $("#alsoPurchased ul.column li").width());
                    var restoCol = Math.floor(colWrap %  $("#alsoPurchased ul.column li").width());
                    var anchoCol = Math.floor(restoCol / (colNum-1));
                    var colActual=1;
                    $("#alsoPurchased ul.column li").each(function(){
                    if (colActual==colNum){
                            $(this).css({ 'marginRight' : "0px"});
                            colActual=0;
                    }else{
                            $(this).css({ 'marginRight' : anchoCol});
                    }
                    colActual++;
                    }) ;
                }
                }
                smartColumns();
                if ($("ul.column")||$("#featuredProducts ul.column")||$("#specialsDefault ul.column")||$("#crossSell ul.column")||$("#alsoPurchased ul.column")){
                    $(window).resize(function () {
                    smartColumns();
                    });
                }
         


            return false;

            }
            });
    }else if (resultado=="2"){
        cargar_precios("cPath="+categoria_activa+"&"+precios+"&"+fabricant +  categories + "&" +ordenar+quepagina);
        valor1="";
        $(".fabricantes").each(function(){
                        if ($(this).attr('checked')){
                                jQuery("#fabricantes_texto").attr("value",$(this).parent().parent().find("span").text());
                                valor1 = jQuery("#fabricantes_texto").attr("value")+","+valor1;
                        }
        }) ;
        if (valor1==""){
                valor1="Todos";
        }else{
                valor1=valor1.slice(0,-1);
        }
        jQuery("#fabricantes_texto").attr("value","Fabricantes:"+valor1);
    }else if (resultado=="3"){
        cargar_fabricantes("cPath="+categoria_activa+"&"+precios+"&"+fabricant +  categories + "&" +ordenar+quepagina);
        cargar_precios("cPath="+categoria_activa+"&"+precios+"&"+fabricant +  categories + "&" +ordenar+quepagina);
       
            valor1="";
            $(".input_categor").each(function(){
                            if ($(this).attr('checked')){
                                    //valor1=$(this).parent().parent().find("span").text();
                                    jQuery("#categorias_texto").attr("value",$(this).parent().parent().find("span").text());
                                    valor1 = jQuery("#categorias_texto").attr("value")+","+valor1;
                                    //alert(valor);
                            }
            }) ;
            if (valor1==""){
                    valor1="Todas";
                    }else{
                    valor1=valor1.slice(0,-1);
            }
            jQuery("#categorias_texto").attr("value","Categorias:"+valor1+"; ");
    }else if (resultado=="4"){
            valor1="";
            $(".precios_generales").each(function(){
                if ($(this).attr('checked')){
                                        jQuery("#precios_texto").attr("value",$(this).parent().parent().find("span").text());
                                        valor1 = jQuery("#precios_texto").attr("value")+","+valor1;
                }
                }) ;
            if (valor1==""){
                    valor1="Todos";
                    }else{
                    valor1=valor1.slice(0,-1);
            }
            jQuery("#precios_texto").attr("value","Precios:"+valor1);
            success_advanced("","","","");
            }



    }
  

