$(function(){
    
    $("#product_list").hide();
    
    var nav_path = $("#product_list").attr("data-tabs");
    
    $("<div></div>").prependTo("#list_place").load(nav_path, function(){

        var start_tab = $("#nav_tabs li:first a").attr("href");

        $("<div id='slider_controls'><div></div><i>loading...</i><a href='#' id='more_left'>More</a> <a href='#' id='more_right'>More</a></div>").insertBefore("#product_list").find("a").add("i").hide();

        load_tab(start_tab);

        $("#nav_tabs li:first a").addClass("on");
        
        $("#nav_tabs a").click(function(){
            
            $("#nav_tabs a").removeClass();
            $(this).addClass("on");
            var this_tab = $(this).attr("href");
            load_tab(this_tab);
            return false;
        
        });
    
    });

    $("#search_again").submit(function(){
        searchLocations();
        return false;
    });

    var load_tab = function(tab) {
        $("#slider_controls").find("div").add("i").show(.1,function(){
        
            $("#product_list").fadeOut(200,function(){

                $(this).empty().load(tab, function(){
                    $("#slider_controls a").unbind();
                    make_slider();
                    $(this).fadeIn(200,function(){
                        $("#slider_controls").find("div").add("i").hide();
                    });
                });

            });
        
        });            
    }
                            
});

function make_slider(){    

    $("#slider_contain").addClass("slider");

/* !SET INITIAL SIZE VARIABLES FOR SLIDING BOXES */
    var accNum = $(".product_item").length;
    var accWidth = accNum / 3;
    var accPages = Math.ceil(accNum / 3);
    var accPaneWidth = 900;
    var accPaneSkips = accPaneWidth * accPages;          
    var accUlWidth = accPaneWidth * accWidth;
    var moreRightStop = accPaneSkips * -1;
    moreRightStop += accPaneWidth;

    $("#slider_pane").width(accUlWidth);


/* !MOVE TO LAST VISITED DOOR */

    var lastItem = $("#product_list").attr("class");

    if (lastItem != "") {

        var lastItemIndex = $("#" + lastItem).prevAll().length;
        var lastItemPage = Math.floor(lastItemIndex / 3);
        var skipToWidth = lastItemPage * accPaneWidth;
    
        var newPos = $("#slider_pane").offset().left;
        newPos -= $("#slider_contain").offset().left;
        newPos -= skipToWidth;
/* Number rounded to compensate for Firefox quirks */
        newPos = Math.round(newPos/10)*10;
        $("#slider_pane").animate({ left: newPos },500);

        if (newPos == moreRightStop) {
            $("#more_right").fadeOut(300);
        }    

        if (skipToWidth == 0) {
            $("#more_left").hide();
        }

    } else {

        $("#more_right").fadeIn(300);
        $("#more_left").hide();

    }

    

    if (accPaneSkips == accPaneWidth) {
        $("#more_right").hide();
    } else if (accPaneSkips == 0) {
        $("#more_right").hide();
    } else {
        $("#more_right").click(function(){
            $("#slider_controls div").show(.1,function(){

                $("#slider_controls a:hidden").fadeIn(200);
                var newPos = $("#slider_pane").offset().left;
                newPos -= $("#slider_contain").offset().left;
                newPos -= accPaneWidth;
    /* Number rounded to compensate for Firefox quirks */
                newPos = Math.round(newPos/10)*10;
                $("#slider_pane").animate({ left: newPos },500,function(){

                    if (newPos == moreRightStop) {

                        $("#more_right").fadeOut(200,function(){
                            $("#slider_controls div").hide();
                        });

                    } else {

                        $("#slider_controls div").hide();
                    
                    }

                });

            });

            return false;
        });
        
        $("#more_left").click(function(){
            $("#slider_controls div").show(.1,function(){

                $("#slider_controls a:hidden").fadeIn(200);
                var newPos = $("#slider_contain > div").offset().left;
                newPos -= $("#slider_contain").offset().left;
                newPos += accPaneWidth;
    /* Number rounded to compensate for Firefox quirks */
                newPos = Math.round(newPos/10)*10;
                $("#slider_contain > div").animate({ left: newPos },500,function(){

                    if (newPos == 0) {
                        $("#more_left").fadeOut(200,function(){
                        
                            $("#slider_controls div").hide();
                        
                        });
                    } else {
                    
                        $("#slider_controls div").hide();
                    
                    }
                });
            
            });

            return false;
        });
    }

}