/*
 *
 * Copyright (c) 2006-2008 Sam Collett (http://www.texotela.co.uk)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Version 2.0.2
 * Demo: http://www.texotela.co.uk/code/jquery/newsticker/
 *
 * $LastChangedDate$
 * $Rev$
 *
 */

(function($) {
/*
 * A basic news ticker.
 *
 * @name     newsticker (or newsTicker)
 * @param    delay      Delay (in milliseconds) between iterations. Default 4 seconds (4000ms)
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @example  $("#news").newsticker(); // or $("#news").newsTicker(5000);
 *
 */
$.fn.newsTicker = $.fn.newsticker = function(delay, id)
{
        delay = delay || 4000;
        initTicker = function(el)
        {
                $.newsticker.clear(el);
                el.items = $("li", el);
                // hide all items (except first one)
                el.items.not(":eq(0)").hide().end();


                //Генерим пейджер
                var pages = $("#pages");
                pages.innerHTML="";
                var inpages='';
                inpages = "<a onfocus='blur()' href='javascript:void(0)' id='prev-tick'><img src='/js/a_prev.png' class='png' alt='prev' width='17' height='16' /></a>";
                for (var index = 0; index < el.items.size(); index++) {
                        inpages =inpages + "<a onfocus='blur()' href='javascript:void(0)' id='tick-" + index + "'>" + "<img src='/js/a_" + (index + 1) + ".png' alt='prev' class='png' width='17' height='16' />" + "</a>";
                }
                inpages = inpages + "<a onfocus='blur()' href='javascript:void(0)' id='next-tick'><img src='/js/a_next.png' alt='prev' class='png' width='17' height='16' /></a>";                //pages.append("<a href='javascript:void(0)' id='resume'>=</a>");
                document.getElementById("pages").innerHTML = inpages;
                //Назначаем события на клик
                pages.find("a").click(function() { randomTick(el, this) });
                pages.find("#prev-tick").unbind("click").click(function() { prevTick(el) });
                pages.find("#next-tick").unbind("click").click(function() { nextTick(el) });
                pages.find("#resume").unbind("click").click(function() { $.newsticker.resume(el) });


                // current item
                el.currentitem = 0;
                startTicker(el);
        };
        startTicker = function(el)
        {
                el.tickfn = setInterval(function() { doTick(el) }, delay)
        };
        doTick = function(el)
        {
                // don't run if paused
                if(el.pause) return;
                // pause until animation has finished
                $.newsticker.pause(el);
                // hide current item
                $(el.items[el.currentitem]).fadeOut("slow",
                        function()
                        {
                                $(this).hide();
                                // move to next item and show
                                el.currentitem = ++el.currentitem % (el.items.size());
                                $(el.items[el.currentitem]).fadeIn("slow",
                                        function()
                                        {
                                                $.newsticker.resume(el);
                                        }
                                );
                        }
                );
        };
        prevTick = function(el) {
                // pause until animation has finished
                $.newsticker.pause(el);
                // hide current item
                $(el.items[el.currentitem]).fadeOut("slow",
                        function()
                        {
                                $(this).hide();
                                // move to next item and show
                                el.currentitem = (el.currentitem + el.items.size() - 1) % (el.items.size());
                                $(el.items[el.currentitem]).fadeIn("slow");
                        }
                );
        };
        nextTick = function(el) {
                // pause until animation has finished
                $.newsticker.pause(el);
                // hide current item
                $(el.items[el.currentitem]).fadeOut("slow",
                        function()
                        {
                                $(this).hide();
                                // move to next item and show
                                el.currentitem = ++el.currentitem % (el.items.size());
                                $(el.items[el.currentitem]).fadeIn("slow");
                        }
                );
        };
        randomTick  = function(el, elem) {
                // pause until animation has finished
                $.newsticker.pause(el);
                // hide current item
                $(el.items[el.currentitem]).fadeOut("slow",
                        function()
                        {
                                $(this).hide();
                                // move to next item and show
                                el.currentitem = $(elem).attr("id").replace("tick-", "");
                                $(el.items[el.currentitem]).fadeIn("slow");
                        }
                );
        };
        this.each(
                function()
                {
                        if(this.nodeName.toLowerCase()!= "ul") return;
                        initTicker(this);
                }
        )
        .addClass("newsticker")
        .hover(
                function()
                {
                        // pause if hovered over
                        $.newsticker.pause(this);
                },
                function()
                {
                        // resume when not hovered over
                        $.newsticker.resume(this);
                }
        );
        return this;
};


$.newsticker = $.newsTicker =
{
        pause: function(el)
        {
                (el.jquery ? el[0] : el).pause = true;
        },
        resume: function(el)
        {
                (el.jquery ? el[0] : el).pause = false;
        },
        clear: function(el)
        {
                el = (el.jquery ? el[0] : el);
                clearInterval(el.tickfn);
                el.tickfn = null;
                el.items = null;
                el.currentItem = null;
        }
}

})(jQuery);