jQuery.fn.accessSlide = function(settings) {
    settings = jQuery.extend({
        defaultHeadline: "Accessory Categories",
        defaultSpeed: "normal"
    }, settings);
    return this.each(function(i) {
        aAccessorySlider.itemWidth = parseInt(jQuery(".accessory-category:eq(" + i + ")",".accessory-slider").css("width")) + parseInt(jQuery(".accessory-category:eq(" + i + ")",".accessory-slider").css("margin-right"));
        aAccessorySlider.init(settings,this);
        jQuery(".accessory-view-all > a", this).click(function() {
            aAccessorySlider.vAll(settings,this);
            return false;
        });
    });
};
var aAccessorySlider = {
    itemWidth: 0,
    init: function(s, p) {
        jQuery(".messaging", p).css("display", "none");
        itemLength = jQuery(".accessory-category", p).length;
        if (jQuery(".accessory-view-all", p).width() == null) {
            jQuery(".accessory-categories", p).prepend("<p class='accessory-view-all'>" + s.defaultHeadline + " [ " + itemLength + " total ] &nbsp;-&nbsp; <a href='#'>View All</a></p>");
        }
        accessoryContainerWidth = itemLength * aAccessorySlider.itemWidth;
        jQuery(".accessory-container", p).css("width", accessoryContainerWidth + "px");
        jQuery(".next", p).css("display", "block");
        animating = false;
        jQuery(".next", p).click(function() {
            if (animating == false) {
                animating = true;
                animateLeft = parseInt(jQuery(".accessory-container", p).css("left")) - (aAccessorySlider.itemWidth * 4);
                if (animateLeft + parseInt(jQuery(".accessory-container", p).css("width")) > 0) {
                    jQuery(".prev", p).css("display", "block");
                    jQuery(".accessory-container", p).animate({ left: animateLeft }, s.defaultSpeed, function() {
                        jQuery(this).css("left", animateLeft);
                        if (parseInt(jQuery(".accessory-container", p).css("left")) + parseInt(jQuery(".accessory-container", p).css("width")) <= aAccessorySlider.itemWidth * 4) {
                            jQuery(".next", p).css("display", "none");
                        }
                        animating = false;
                    });
                } else {
                    animating = false;
                }
            }
            return false;
        });
        jQuery(".prev", p).click(function() {
            if (animating == false) {
                animating = true;
                animateLeft = parseInt(jQuery(".accessory-container", p).css("left")) + (aAccessorySlider.itemWidth * 4);
                if ((animateLeft + parseInt(jQuery(".accessory-container", p).css("width"))) <= parseInt(jQuery(".accessory-container", p).css("width"))) {
                    jQuery(".next", p).css("display", "block");
                    jQuery(".accessory-container", p).animate({ left: animateLeft }, s.defaultSpeed, function() {
                        jQuery(this).css("left", animateLeft);
                        if (parseInt(jQuery(".accessory-container", p).css("left")) == 0) {
                            jQuery(".prev", p).css("display", "none");
                        }
                        animating = false;
                    });
                } else {
                    animating = false;
                }
            }
            return false;
        });
    },
    vAll: function(s, p) {
        var o = p;
        while (p) {
            p = p.parentNode;
            if (jQuery(p).attr("class") != undefined && jQuery(p).attr("class").indexOf("accessory-slider") != -1) {
                break;
            }
        }
        
        if (jQuery(o).text().indexOf("View All") != -1) {
            jQuery(".next", p).css("display", "none");
            jQuery(".prev", p).css("display", "none");
            jQuery(o).text("View Less");
            jQuery(".accessory-container", p).css("left", "0px").css("width", aAccessorySlider.itemWidth * 4 + "px");
            jQuery(".accessory-category").css("margin-bottom", "10px");
        } else {
            jQuery(o).text("View All");
            aAccessorySlider.init(s, p);
        }        
    }
};
