/** Ready Function
*  This is the main function for the jQuery script. 
*/
$(document).ready(function() {
    // Horizontal Scroller, with Automatic, wrapping scroll
    $("#mycarousel").each(function() {
        var $list = $(this); 
        var numItems = $("li", this).size();
        var itemWidth = $("li.navFirst", this).width() + 5;  // slight hack, the 5 is the right margin I know is there
        var $prev = $("div.carousel-prev").css({ display: "block" });
        var $next = $("div.carousel-next").css({ display: "block" });
        var $clip = $("div.carousel-clip");
        var autoRunning = false;
        
        $list.width((numItems + 1) * itemWidth);

        if ($list.width() < $clip.width()) {
            $next.addClass("carousel-next-disabled");
            $prev.addClass("carousel-prev-disabled");
            autoRunning = false;
        }
        else {
            var defaultPos = itemWidth * -1;
            
            $("li:first", this).before($("li:last", this));
            $list.css({ left: defaultPos + "px" });

            $next.click(function() {
                slide("next", itemWidth * -1, defaultPos);
            }).Disposable();

            $prev.click(function() {
                slide("prev", itemWidth, defaultPos);
            }).Disposable();

            // automatic scrolling
            autoRunning = true;
            var auto = setInterval(function() { autoScroll(); }, 5000);

            $prev.hover(function() {
                clearInterval(auto);
                autoRunning = false;
            }, function() {
                autoRunning = true;
                auto = setInterval(function() { autoScroll(); }, 5000);
            }).Disposable();

            $next.hover(function() {
                clearInterval(auto);
                autoRunning = false;
            }, function() {
                autoRunning = true;
                auto = setInterval(function() { autoScroll(); }, 5000);
            }).Disposable();

            $clip.hover(function() {
                clearInterval(auto);
                autoRunning = false;
            }, function() {
                autoRunning = true;
                auto = setInterval(function() { autoScroll(); }, 5000);
            }).Disposable();

            function autoScroll() {
                if (autoRunning)
                    $next.click();
                return false;
            };

            function slide(direction, increment, defaultPosition) {
                $list.animate({ left: "+=" + increment + "px" }, "normal", function() {
                    if (direction == "next")
                        $("li:last", this).after($("li:first", this));
                    else
                        $("li:first", this).before($("li:last", this));

                    $list.css({ left: defaultPosition + "px" });
                });
            }
        }
    });
});

(function($) {
    $.fn.Disposable = function(cln) {
        return this.each(function() {
            var el = this;
            if (!el.dispose) {
                el.dispose = cleanup;
                $(window).bind("unload", cleanup);
            }
            function cleanup() {
                if (!el) return;
                $(el).unbind();
                $(window).unbind("unload", cleanup);
                el.dispose = null;
                el = null;
            };
        });
    };
})(jQuery);
