/*
 * Marquee.js
 * $Author: Trigosoft (info@trigosoft.net)$
 * $Date: 2008-10-08$
 *
 * This object provides an easy way to display a marquee into a webpage.
 *
 */

function Marquee(id) {
	this.Initialize(id);
}

Marquee.prototype.Initialize = function(id) {
	/* "Public properties */
	this.Width = '300px'; // width of the marquee
	this.Speed = '3'; // speed of the slidding text
	this.PauseOnMouseOver = true; // the text will stop slidding if mouse gets over the marquee container
	this.Content = ''; // html content of the marquee
	
	/* Private properties */
	this._id = id;
	this._contentHTMLElement = null; // contains a reference to the DOM object representing the marquee container
	this._interval = null; // contains a reference to the interval of the marquee so that we can control it
}

Marquee.prototype.Start = function() {
	this._interval = setInterval(this._id + '.scrollContent()', 20);
}

Marquee.prototype.Pause = function() {
	clearInterval(this._interval);
}

Marquee.prototype.Render = function(parent) {
	// we do not need to persist the container
	var container = document.createElement('div');
	container.setAttribute('id', this._id + '_container');
	container.style.width = this.Width;
	container.style.position = 'relative';
	container.style.overflow = 'hidden';
	
	this._contentHTMLElement = document.createElement('div');
	this._contentHTMLElement.setAttribute('id', this._id);
	
	this._contentHTMLElement.style.position = 'relative';
	this._contentHTMLElement.style.whiteSpace = 'nowrap';
	this._contentHTMLElement.style.display = 'inline'; // so that the contentHTMLElement width is extending with the content that will not wrap
	
	// Initialise just outside the container
	this._contentHTMLElement.style.left = parseInt(this.Width) + 'px';
	this._contentHTMLElement.innerHTML = this.Content;
	
	if (this.PauseOnMouseOver) {
		this._contentHTMLElement.setAttribute('onmouseover', this._id + '.Pause()');
		this._contentHTMLElement.setAttribute('onmouseout', this._id + '.Start()');
	}
	
	container.appendChild(this._contentHTMLElement);
	
	parent.appendChild(container);
}

// "private" methods
Marquee.prototype.scrollContent = function() {
	if (parseInt(this._contentHTMLElement.style.left) > (parseInt(this.Width) * -1)) {
		this._contentHTMLElement.style.left = (parseInt(this._contentHTMLElement.style.left) - this.Speed) + 'px';
	}
	else {
		this._contentHTMLElement.style.left = parseInt(this.Width) + 'px';
	}
}
