 /**
  * @name       NewsTicker
  * @version    1.1
  *
  * @param      timeout (interval)
  * @param      speed (speed of the animation)
  * @param      delay (time the text stays on screen)
  * @param      fadeout (time used for fadeout)
  * @param      stopOnHover (stop when mouse is over item)
  *
  * @author     Davy De Pauw (http://www.float.be)
  * @example
  */

(function($) {
    $.fn.ticker = function(options)
    {
        this.each(
            function()
            {
                this.tickerTimeout = 0;

                this.opts = $.extend({}, $.fn.ticker.defaults, options);

                // Get container
                var $cont = $(this);

                // Get elements within container
                var $els = $cont.children();

                this.opts.currItem = 0;

                if ($els.length < 1) {
                    if (window.console && window.console.log)
                        window.console.log('terminating; too few elements: ' + $els.length);
                    return; // don't bother
                }

                // Init ticker
                init(this);

                // Attach reference to this to link
                $a = $('<a href="#" title="" class="next">Next</a>');

                // Append to html
                $(this).parent().append($a);
                
                $a.click(function(e) {
                    $el = $(this).prev().get(0);

                    next($el);

                    e.preventDefault();
                });
            }
        )

        /*
        .hover(

            function()
            {
                pause(this, true);
            },

            function()
            {
                pause(this, false);
            }
        );
        */
    };

    function init(el)
    {
        stop(el);

        // Cache items
		el.items = $('li', el);

        // Current item
		el.opts.currItem = 0;

        // Start
		start(el);
    }

    function tick(el)
    {
        // Don't run if paused
		if(el.pause) return;

		// Pause until animation has finished
		el.pause = true;

        // Get current li element
        $el = $(el.items[el.opts.currItem]);

        var fn = function()
        {
            // Add delay
            $el.delay(el.opts.delay)

            // Fadeout
            .fadeOut(el.opts.fadeout, function() {
                
                $(this).css('left', $(this).width() + 'px').show();

                el.opts.currItem = ++el.opts.currItem % (el.items.size());

                $(el.items[el.opts.currItem]).animate({'left': '0px'}, el.opts.speed, function() {
                    el.pause = false;
                });
            });
        }

        fn();
    }

    function pause(el, state)
    {
        el.pause = state;
    }

    function next(el)
    {
        // Stop current
        stop(el);

        // Skip to next item
        tick(el);
    }

    function start(el)
    {
        // Animate first item
        $(el.items[el.opts.currItem]).animate({'left': '0px'}, el.opts.speed, function() {});

        // Set interval
		el.tickerTimeout = setInterval(function() {tick(el)}, el.opts.delay);

    }
    
    function stop(el)
    {
        clearInterval(el.tickerTimeout);
    }

    $.fn.ticker.defaults = {
        timeout: 3500,
        pause: 2000,
        speed: 1000,
        fadeout: 500,
        delay: 2500,
        stopOnHover: true
    };

}) (jQuery);

