jQuery.fn.accessProducts = function(settings) {
    settings = jQuery.extend({
        scrollSpeed: "slow"
    }, settings);
    return this.each(function(i) {
        aProductSlider.itemWidth = parseInt(jQuery(".item:eq(" + i + ")",".product_slider").css("width")) + parseInt(jQuery(".item:eq(" + i + ")",".product_slider").css("margin-right"));
        aProductSlider.init(settings,this);
        jQuery(".view_all > a", this).click(function() {
            aProductSlider.vAll(settings,this);
            return false;
        });
    });
};
var aProductSlider = {
    itemWidth: 0,
    init: function(s,p) {
        jQuery(".messaging",p).css("display","none");
        itemLength = jQuery(".item",p).length;
        if (jQuery(".view_all",p).width() == null) {
            jQuery(".news_items",p).prepend("<p class='view_all'>" + s.newsHeadline + " [ " + itemLength + " total ]  -  <a href='#'>View All</a></p>");
        }
        newsContainerWidth = itemLength * aProductSlider.itemWidth;
        jQuery(".container",p).css("width",newsContainerWidth + "px");
        jQuery(".next",p).css("display","block");
        animating = false;
		if (itemLength <= 4) { // If items in carousel are <= this number, then don't show arrows
			jQuery(".next",p).css("display","none"); 
		}
        jQuery(".next",p).click(function() {
            if (animating == false) {
                animating = true;
                animateLeft = parseInt(jQuery(".container",p).css("left")) - (aProductSlider.itemWidth * 2); // Controls number of items to scroll per next click
                if (animateLeft + parseInt(jQuery(".container",p).css("width")) > 0) {
                    jQuery(".prev",p).css("display","block");
                    jQuery(".container",p).animate({left: animateLeft}, s.newsSpeed, function() {
                        jQuery(this).css("left",animateLeft);
                        if (parseInt(jQuery(".container",p).css("left")) + parseInt(jQuery(".container",p).css("width")) <= aProductSlider.itemWidth * 4) { // Remove next click when 5 items remain in carousel
                            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(".container",p).css("left")) + (aProductSlider.itemWidth * 2); // Controls number of items to scroll per prev click
                if ((animateLeft + parseInt(jQuery(".container",p).css("width"))) <= parseInt(jQuery(".container",p).css("width"))) {
                    jQuery(".next",p).css("display","block");
                    jQuery(".container",p).animate({left: animateLeft}, s.newsSpeed, function() {
                        jQuery(this).css("left",animateLeft);
						if (parseInt(jQuery(".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("product_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(".container",p).css("left","0px").css("width",aProductSlider.itemWidth * 4 + "px"); // Controls number of items when View All
        } else {
            jQuery(o).text("View All");
            aProductSlider.init(s,p);
        }
    }
};
