/*
 *
 * Copyright (c) 2006/2007 Sam Collett (http://www.texotela.co.uk)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * Version 2.0
 * Demo: http://www.texotela.co.uk/code/jquery/newsticker/
 *
 * $LastChangedDate: 2007-05-29 11:31:36 +0100 (Tue, 29 May 2007) $
 * $Rev: 2005 $
 *
 */
(function($) {
$.fn.newsTicker = $.fn.newsticker = function(delay)
{
    delay = delay || 4000;
    initTicker = function(el)
    {
        stopTicker(el);
        el.items = $("li", el);
        // hide all items (except first one)
        el.items.not(":eq(0)").hide().end();
        el.currentitem = 0;
        startTicker(el);
    };
    startTicker = function(el)
    {
        el.tickfn = setInterval(function() { doTick(el) }, delay)
    };
    stopTicker = function(el)
    {
        clearInterval(el.tickfn);
    };
    pauseTicker = function(el)
    {
        el.pause = true;
    };
    resumeTicker = function(el)
    {
        el.pause = false;
    };
    doTick = function(el)
    {
        if(el.pause) return;
        el.pause = true;
        $(el.items[el.currentitem]).fadeOut("normal",
            function()
            {
                $(this).hide();
                el.currentitem = ++el.currentitem % (el.items.size());
                $(el.items[el.currentitem]).fadeIn("normal",
                    function()
                    {
                        el.pause = false;
                    }
                );
            }
        );
    };
    this.each(
        function()
        {
            if(this.nodeName.toLowerCase()!= "ul") return;
            initTicker(this);
        }
    )
    .addClass("newsticker")
    .hover(
        function()
        {
            pauseTicker(this);
        },
        function()
        {
            resumeTicker(this);
        }
    );
    return this;
};

})(jQuery);
