/*
 *
 * Copyright (c) 2006/2007 Sam Collett (http://www.texotela.co.uk)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 */
 
(function($) {
/*
 * A basic news ticker. Version 2
 *
 * @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,controls)
{
	delay = delay || 6000;
	controls = controls || null;
	initTicker = function(el)
	{
		stopTicker(el);
		el.items = $("li", el);
		el.ticks = 0;
		// hide all items (except first one)
		el.items.not(":eq(0)").hide().end();
		// current item
		el.currentitem = 0;
		startTicker(el);
		controls.css({display: "block"});
		controls.children(".number_box").children("a").filter(":eq(0)").addClass("selected");
	}
	startTicker = function(el)
	{
		el.tickfn = setInterval(function() { doTick(el) }, delay)
	}
	stopTicker = function(el)
	{
		clearInterval(el.tickfn);
	}
	toggleTicker = function(el)
	{
		if (el.pause)
		{
			resumeTicker(el);
		} else {
			pauseTicker(el);
		}
	}
	pauseTicker = function(el)
	{
		el.pause = true;
		//if (controls != null)
		//{
			controls.children(".ticker_pause").css({display:'none'});
			controls.children(".ticker_resume").css({display:'inline'});
		//}
	}
	resumeTicker = function(el)
	{
		el.pause = false;
		//if (controls != null)
		//{
			controls.children(".ticker_resume").css({display:'none'});
			controls.children(".ticker_pause").css({display:'inline'});
		//}
	}
	nextItem = function(el)
	{
		el.pause = true;
		changeItem(el,el.currentitem + 2);
	}
	prevItem = function(el)
	{
		el.pause = true;
		changeItem(el,el.currentitem );
	}
	changeItem = function(el,dest)
	{
		pauseTicker(el);
		//hide current item
		$(el.items[el.currentitem]).hide();
		el.currentitem = (dest < 1) ? el.items.size() - 1 : ((dest - 1) % (el.items.size()));
		$(el.items[el.currentitem]).show();
		controls.children(".number_box").children("a").filter(":eq("+(el.currentitem)+")").addClass("selected");
		controls.children(".number_box").children("a").not(":eq("+(el.currentitem)+")").removeClass("selected");
	}
	doTick = function(el)
	{
		if (el.ticks > 100) {
			stopTicker(el);
			return;
		}
		el.ticks++;
		// don't run if paused
		if(el.pause) return;
		// pause until animation has finished
		el.pause = true;
		// hide current item
		$(el.items[el.currentitem]).fadeOut("fast",
			function()
			{
				$(this).hide();
				// move to next item and show
				el.currentitem = ++el.currentitem % (el.items.size());
				controls.children(".number_box").children("a").filter(":eq("+(el.currentitem)+")").addClass("selected");
				controls.children(".number_box").children("a").not(":eq("+(el.currentitem)+")").removeClass("selected");
				$(el.items[el.currentitem]).fadeIn("fast",
					function()
					{
						el.pause = false;
					}
				);
			}
		);
	}
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase()!= "ul") return;
			initTicker(this);
		}
	)
	.addClass("newsticker")
	return this;
}

})(jQuery);
