/**
 *  Marquee control
 * 
 *  @author      Viknesk Kumarr Muniandy
 *  @updated     24-01-2009
 *
 *  @param jQuery     containerEl   Marquee container element
 *  @param jQuery     contentEl     Marquee content element
 *  @param integer    interval      Scrolling interval in ms
 *  @param integer    deltaY        Scrolling distance in px
 *  @param integer    timer         Timer id
 */

/**
 *  Marquee contructor
 *
 *  @param  string|element container Container for the marquee
 *  @param  hash options Options: interval - scrolling interval in ms, deltaY - amount to scroll in px
 *
 *  @return object New Marquee object
 */
function Marquee (container, options)
{
	var $this = this;
	this.interval = (options && options.interval) || this.interval;
	this.deltaY = (options && options.deltaY) || this.deltaY;
	var containerEl = this.containerEl = $(container || '.marquee');
	var contentEl = this.contentEl = this.containerEl.wrapInner('<div />').children().eq(0);
	
	if (!containerEl.length) return;
	
	// If content block too short, do nothing
	if (containerEl.height() > contentEl.outerHeight({margin:true})) return;

	// Duplicate block to create appearance of smooth transition during scrolling
	containerEl.css ({'overflow':'hidden'}).append (contentEl.clone());

	// Set up hover behaviour
	containerEl.hover (function () { $this.stopScroll() }, function () { $this.startScroll() });

	// Start scrolling
	this.startScroll ();
	
	return this;
}
Marquee.prototype.interval = 100;
Marquee.prototype.deltaY = 1;
Marquee.prototype.scroll = function (containerEl, contentEl) {
		var top = containerEl.scrollTop() + this.deltaY;
		if (top > contentEl.height()) top -= contentEl.outerHeight({margin:true});
		containerEl.scrollTop (top);
};
Marquee.prototype.startScroll = function () {
	var $this = this;
	$this.timer = setInterval (function(){ $this.scroll($this.containerEl, $this.contentEl); }, $this.interval);
};
Marquee.prototype.stopScroll = function () { clearInterval (this.timer); this.timer = -1; }
