$(document).ready(function(){
    
    $("#product_list").before("<div id='slider_controls'><a href='#' id='more_left'>More</a> <a href='#' id='more_right'>More</a></div>");

    $("#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_left").hide();

    }

    

    if (accPaneSkips == accPaneWidth) {
        $("#more_right").hide();
    } else if (accPaneSkips == 0) {
        $("#more_right").hide();
    } else {
        $("#more_right").click(function(){

            $("#slider_controls a:hidden").fadeIn(300);
            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);
            if (newPos == moreRightStop) {
                $(this).fadeOut(300);
            }

            return false;
        });
        
        $("#more_left").click(function(){
            $("#slider_controls a:hidden").fadeIn(300);
            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);
            if (newPos == 0) {
                $(this).fadeOut(300);
            }
            return false;
        });
    }

});

