var scrollable_tools_timer__;

/**
 * @license 
 * jQuery Tools 1.2.5 / Scrollable Autoscroll
 * 
 * NO COPYRIGHTS OR LICENSES. DO WHAT YOU LIKE.
 * 
 * http://flowplayer.org/tools/scrollable/autoscroll.html
 *
 * Since: September 2009
 * Date:    Wed Sep 22 06:02:10 2010 +0000 
 */
(function($) {

    var t = $.tools.scrollable;

    t.autoscroll = {

        conf: {
            autoplay: true,
            interval: 3000,
            autopause: true,
            dynamicInterval: false,
            dynamicIntervalID: ""
        }
    };

    // jQuery plugin implementation
    $.fn.autoscroll = function(conf) {

        if (typeof conf == 'number') {
            conf = { interval: conf };
        }

        var opts = $.extend({}, t.autoscroll.conf, conf), ret;

        this.each(function() {

            var api = $(this).data("scrollable");
            if (api) { ret = api; }

            // interval stuff
            var stopped = true;

            api.play = function() {

                stopped = false;

                // timer 
                api.setTimer();

            };

            api.setTimer = function() {
                scrollable_tools_timer__ = clearInterval(scrollable_tools_timer__);
            
                if (opts.dynamicInterval) {
                    var index_ = 0;
                    if (api.getIndex() != api.getSize() && api.getIndex() >= 0)
                        index_ = api.getIndex();

                    var intervalo = parseInt(api.getItems().eq(index_).children(opts.dynamicIntervalID).html());
                    
                    scrollable_tools_timer__ = setInterval(function() {
                        api.next(); api.setTimer()
                    }, intervalo);
                } else {
                    scrollable_tools_timer__ = setInterval(function() {
                        api.next();
                    }, opts.interval);
                }


            };

            api.pause = function() {
                scrollable_tools_timer__ = clearInterval(scrollable_tools_timer__);
            };

            // when stopped - mouseover won't restart 
            api.stop = function() {
                api.pause();
                stopped = true;
                scrollable_tools_timer__ = clearInterval(scrollable_tools_timer__);
            };

            /* when mouse enters, autoscroll stops */
            if (opts.autopause) {
                api.getRoot().add(api.getNaviButtons()).hover(api.pause, api.play);
            }

            if (opts.autoplay) {
                api.play();
            }

        });

        return opts.api ? ret : this;

    };

})(jQuery);		

