(function($) {

    $.fn.slider = function(options) {

        var default_options = {
            prevButton:         "prevSliderButton",
            nextButton:         "nextSliderButton",
            timerSpeed:         5000,
            animationSpeed:     1000,
            animate:            false
        };

        var options = $.extend(default_options, options);

        var obj = $(this);
        var s = $("li", obj).length;
        var w = $("li", obj).width();
        var h = $("li", obj).height();

        var ts = s-1;
        var t = 0;

        var timer = null;
        
        $("ul", obj).css('width', s*w);

        $(obj).live('mouseover', function() {
            stopSliding();
        });
        $(obj).live('mouseout', function() {
            if (options.animate == true) {
                timer = setTimeout(sliding, options.timerSpeed);
            }
        });
        $("a#" + options.prevButton).live('click', function() {
           animateItPrev();
           return false;
        });
        $("a#" + options.nextButton).live('click', function() {
           animateItNext();
           return false;
        });

        function animateItPrev() {
            var ot = t;
            if (ot > 0) {
                t = t-1;
            } else {
                t = ts;
            }
            animateIt();
        }
        function animateItNext() {
            var ot = t;
            if (ot < ts) {
                t = t+1;
            } else {
                t = 0;
            }
            animateIt();
        }

        function animateIt(p)
        {
            p = (t*w*-1);
            $("ul",obj).animate({marginLeft: p}, options.animationSpeed);
        }

        function initSliding() {
            if (options.animate == true) {
                timer = setTimeout(sliding, options.timerSpeed);
            }
        }

        function sliding() {
            animateItNext();
            if (options.animate == true) {
                timer = setTimeout(sliding, options.timerSpeed);
            }
        }

        function stopSliding()
        {
            clearTimeout(timer);
        }

        initSliding();
    };
    
})(jQuery);
