/*
 *
 * 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$
 * $Rev$
 *
 */
 
(function($) {

/* this is little hackish, defining the element variable in the closure scope just 
so we can use our public methods as in a jQuery plugin */
var global_el;

/*
 * A basic news ticker.
 *
 * @name     newsticker (or newsTicker)
 * @param    cfg associative array for key-value pairs. expected fields: delay, step_forward_obj, step_backward_obj
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @example  $("#news").newsticker(); // or $("#news").newsTicker(5000);
 *
 */
$.fn.newsTicker = $.fn.newsticker = function(cfg)
{
	var defaults = {
		delay: 4000
	}

	// Extend our default options with those provided.#
	var cfg = $.extend(defaults, cfg);
	delay = cfg.delay;

	
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase()!= "ul") return;
			global_el = this;
			$.fn.newsticker.initTicker(global_el);
			//initTicker(this);
		}
	)
	.addClass("newsticker")
	.hover(
		function()
		{
			// pause if hovered over
			$.fn.newsticker.pauseTicker();
		},
		function()
		{
			// resume when not hovered over
			$.fn.newsticker.resumeTicker();
		}
	);

	//return this;
	return global_el;
};

$.fn.newsticker.initTicker = function(el) 
{
	global_el = el;
	$.fn.newsticker.stopTicker(global_el);
	//stopTicker(el);
	global_el.items = $("li", global_el);
	// hide all items (except first one)
	global_el.items.not(":eq(0)").hide().end();
	// current item
	global_el.currentitem = 0;
	$.fn.newsticker.startTicker(global_el);
	//startTicker(el);
};


/* don't like making all of these methods public in a way that requires the 
element to be put into the broadest scope in this closure. I will not be able
to instantiate more than one newsticker at a time this way. */

$.fn.newsticker.startTicker = function()
{
	global_el.tickfn = setInterval(function() { $.fn.newsticker.doTick({direction:'forward'}) }, delay)
};

$.fn.newsticker.stopTicker = function()
{
	clearInterval(global_el.tickfn);
};

$.fn.newsticker.pauseTicker = function()
{
	global_el.pause = true;
};

$.fn.newsticker.resumeTicker = function()
{
	global_el.pause = false;
};

$.fn.newsticker.stepForward = function()
{
	$.fn.newsticker.doTick({direction:'forward', speed:'fast'});
	$.fn.newsticker.stopTicker();
};

$.fn.newsticker.stepBackward = function()
{
	$.fn.newsticker.doTick({direction:'backward', speed:'fast'});
	$.fn.newsticker.stopTicker();
};

$.fn.newsticker.doTick = function(cfg)
{
	var defaults = {
		delay: 4000,
		direction: 'forward',
		speed: 'slow'
	}

	// Extend our default options with those provided.#
	var cfg = $.extend(defaults, cfg);
	

	//alert('do tick, direction = '+direction);
	// don't run if paused
	if(global_el.pause) return;
	// pause until animation has finished
	global_el.pause = true;
	// hide current item
	$(global_el.items[global_el.currentitem]).fadeOut(cfg.speed,
		function()
		{
			$(this).hide();
			// move to next item and show
			global_el.currentitem = (cfg.direction == 'backward') ? global_el.currentitem - 1 : global_el.currentitem + 1;
			global_el.currentitem = (global_el.currentitem > global_el.items.size()-1) ? 0 : global_el.currentitem;
			global_el.currentitem = (global_el.currentitem < 0) ? global_el.items.size() - 1 : global_el.currentitem;
			
			$(global_el.items[global_el.currentitem]).fadeIn(cfg.speed,
				function()
				{
					global_el.pause = false;
				}
			);
		}
	);
};

})(jQuery);
