jQuery(document).ready(function(){

    var reload_nav = true;
    var reload_compare = true;
    var page_url = "";
    var cartDeleteText = "";

    function setReload_nav( a ){
        window.reload_nav = a;
    }
    function getReload_nav(  ){
        return window.reload_nav;
    }
    function setReload_compare( a ){
        window.reload_compare = a;
    }
    function getReload_compare(  ){
        return window.reload_compare;
    }

    function setCartDeleteText(a){
        window.cartDeleteText = a;
    }

    setReload_nav(true);
    setReload_compare(true);

    var hash = location.hash.slice(1);

    if(hash!=""){
        handleHashChange();
    }

    jQuery(window).hashchange( function(e) {
        handleHashChange();
    });


    /* prices slider */

    function StartSlider(){
        //SLIDER FILTER
        min_price = 0;
        max_price = parseInt(jQuery("input#price_maximum").val());
        step_val = jQuery("input#step_value").val();
        step_val = parseInt(step_val);
        //var price_slider =
        jQuery("#slider-range-price").slider({
            range: true,
            min: 0,
            max: max_price,
            step: step_val,
            values: [jQuery("#init_price_minimum").val(), jQuery("#init_price_maximum").val()],
            slide: function(event,ui){
                jQuery("input#price_maximum").val(jQuery("#slider-range-price").slider("values", 1));
                jQuery("input#price_minimum").val(jQuery("#slider-range-price").slider("values", 0));
            },
            change: function(event, ui) {
                jQuery("input#price_maximum").val(jQuery("#slider-range-price").slider("values", 1));
                jQuery("input#price_minimum").val(jQuery("#slider-range-price").slider("values", 0));
                //loadFilteredList();

                new_url  = ajaxListURL(jQuery("#price_slider_url").val()) +  "&price=" + jQuery("#slider-range-price").slider("values", 0) +  "," + jQuery("#slider-range-price").slider("values", 1);
                setReload_nav(true);
                setReload_compare( false);
                window.location.hash = hashUrl(new_url);




            }

        });
        jQuery("input#price_maximum").val(jQuery("#slider-range-price").slider("values", 1));
        jQuery("input#price_minimum").val(jQuery("#slider-range-price").slider("values", 0));



    }

    StartSlider();


    jQuery(".pager select, .toolbar select").live('change',function(){
        setReload_nav(true);
        setReload_compare( true);
        window.location.hash = hashUrl(jQuery(this).val());

        //loadAjaxProductsList(,false,false);
        return false
    });
    jQuery(".pager a, .toolbar a").live('click',function(){
        setReload_nav(false);
        setReload_compare( false);
        window.location.hash = hashUrl(jQuery(this).attr("href"));
        //loadAjaxProductsList(,false,false);
        return false
    });

    jQuery("#cart-sidebar a.btn-remove").live('click',function(){
        if(confirm(window.cartDeleteText)){
            setReload_nav(false);
            setReload_compare( false);
            loadAjaxProductsList(jQuery(this).attr("href"), false ,false,true);
        }
        return false;
    });


    jQuery(".block-layered-nav #narrow-by-list a, .block-layered-nav .currently a, .block-layered-nav .actions a").live("click",function(){
        setReload_nav(true);
        setReload_compare( true);

        if(jQuery(this).attr("id")=="price-filter-button"){

            step_val = parseInt(jQuery("input#step_value").val());
            request_price_min = Math.floor(jQuery("#price_minimum").val() / step_val) * step_val;
            request_price_max = Math.ceil(jQuery("#price_maximum").val() / step_val) * step_val;
            new_url  = ajaxListURL(jQuery("#price_slider_url").val()) +  "&price=" + request_price_min+ "," + request_price_max;
            window.location.hash = hashUrl(new_url);
        }else if(jQuery(this).hasClass("category-link")){
       	    //window.location.hash = hashUrl();	 	
        new_url = jQuery(this).attr("href");
        var urlparts= new_url.split('?');
        new_url  = urlparts[0];
        window.location = new_url;
        
        }else{
            window.location.hash = hashUrl(jQuery(this).attr("href"));
        }
        //loadAjaxProductsList(jQuery(this).attr("href"),true,false);
        return false
    });
    jQuery(".col-main .add-to-links a.link-compare").live('click',function(){
        setReload_nav(false);
        setReload_compare( true);
        //window.location.hash = hashUrl(jQuery(this).attr("href"));
        loadAjaxProductsList(jQuery(this).attr("href"),false,true);
        return false
    });

    /*jQuery(".layered-navigation-select").live('change',function(){
        setReload_nav(true);
        setReload_compare( false);
        window.location.hash = hashUrl(jQuery(this).val());
        //loadAjaxProductsList(jQuery(this).val(),true,false);
        return false
    });*/

    jQuery(".block.block-compare a.btn-remove, .block.block-compare .actions a").live('click',function(){
        setReload_nav(false);
        setReload_compare( true);
        //window.location.hash = hashUrl(jQuery(this).attr("href"));
        loadAjaxProductsList(jQuery(this).attr("href"),false,true);
        return false
    });


    jQuery("#price_minimum, #price_maximum").live("keyup", function(event){
        if(event.keyCode == 13){
            jQuery("#price-filter-button").click();
        }
    });


    function handleHashChange(){
        var hash = location.hash.slice(1);

        path = window.location.href;
        path = path.split("#")[0];
        path = path.split("?")[0];
        path = ajaxListURL(path + "?" + hash);

        //alert(  path );

        nv = getReload_nav();
        cm = getReload_compare();
        loadAjaxProductsList(path, nv, cm ,false );
        setReload_nav(true);
        setReload_compare(true);

    }









    function loadAjaxProductsList(e,f,g,h){

        e=ajaxListURL(e);

        jQuery(".col-main").append("<div class=\"products-list-loader\"><div></div></div>");
        if(f){
            jQuery(".block-layered-nav").append("<div class=\"products-list-loader\"><div></div></div>")
        }
        jQuery.get(e,{},function(a,b,c){
            if(b=="error"){
                jQuery(".col-main").html("<p>There was an error making the AJAX request</p>")
            }else{
                var d=jQuery("<div />").html(a);
                jQuery(".col-main").html(d.find('.col-main').html());
                if(f){
                    jQuery(".block-layered-nav").html(d.find('.block-layered-nav').html())
                    StartSlider();
                }
                if(g){
                    jQuery(".block-compare").html(d.find('.block-compare').html())

                }
                if(h){
                    jQuery(".block-cart").html(d.find('.block-cart').html())
                }
                if(typeof(window.ajaxproload)=="function"){
                    ajaxproload()
                }

            }
        })
    }
    function ajaxListURL(a){
        if(a.indexOf("ajax=1")<0){
            if(a.indexOf("?")<0){
                a=a+"?ajax=1"
            }else{
                a=a+"&ajax=1"
            }
        }
        //a = a + "&price_minimum=" + jQuery("#price_minimum").val() + "&price_maximum=" + jQuery("#price_maximum").val() ;
        return a
    }

    function hashUrl(a){

        a.match(/\?(.+)$/);
        var params = RegExp.$1;
        if(params.indexOf("ajax=1")>=0){
            params = params.replace("ajax=1&","");
            params = params.replace("&ajax=1","");
            params = params.replace("ajax=1","");
        }
        // alert(params);
        return params;
    }


    jQuery("#products-list button.btn-cart").live("click", function(){
        loadAjaxProductsList(jQuery(this).attr("rel"), false ,false,true);
        return false;
    });


});

