/*
 *
 * 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/
 *
 * jQueryLastChangedDatejQuery
 * jQueryRevjQuery
 *
 */

(function(jQuery) {
/*
 * 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  jQuery("#news").newsticker(); // or jQuery("#news").newsTicker(5000);
 *
 */

jQuery.fn.newsTicker = jQuery.fn.newsticker = function(delay)
{
	delay = delay || 4000;
	initTicker = function(el)
	{
		stopTicker(el);
		el.items = jQuery("li", el);
		// hide all items (except first one)
		el.items.not(":eq(0)").hide().end();
		//el.items.hide().end();
		
		// current item
		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)
	{
		// don't run if paused
		if(el.pause) return;
		// pause until animation has finished
		el.pause = true;
		// hide current item
		jQuery(el.items[el.currentitem]).fadeOut("slow",
			function()
			{
				jQuery(this).hide();
				// move to next item and show
				el.currentitem = ++el.currentitem % (el.items.size());
				
				var textElem = jQuery(el.items[el.currentitem]);
				
				textElem.cChar = 1;
				doChars = function(textElem){
					if(textElem.pause) return;
					pauseTicker(el);
										
					if(textElem.cChar <= textElem.text().length){
						if(textElem.cChar == 1){
							textElem.css('display', 'block');
							textElem.allText = textElem.text();
						}
						var tickerText = textElem.allText.substring(0,textElem.cChar);
						if(textElem.cChar == textElem.allText.length){
							textElem.children('a').text(tickerText);
						}else{
							textElem.children('a').text(tickerText + '_');
						}
						textElem.cChar = textElem.cChar + 1;
						
						tickerTimeout = setTimeout(function() { doChars(textElem) }, '150');
					}else{
						textElem.cChar = 1;	
						resumeTicker(el);
					}
					textElem.hover(
						function(){
							textElem.children('a').text(textElem.allText);
							textElem.pause = true;
							pauseTicker(el);
						},
						function(){
							textElem.pause = false;
							resumeTicker(el);
						}
					);
				};
				doChars(textElem);
			}
		);
	};
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase()!= "ul") return;
			initTicker(this);
		}
	)
	.addClass("newsticker")
	jQuery("#newsLatest li a").hover(
		function()
		{
			// pause if hovered over
			pauseTicker(this);
		},
		function()
		{
			// resume when not hovered over
			resumeTicker(this);
		}
	);
	return this;
};

})(jQuery);
