var _tickerId = '';

function wmxXmlScroller()
{
	//Private variables
	var _contentHeight = 0;
	var _contentWidth = 0;
	var _element0 = null;
	var _element1 = null;
	var _element2 = null;
	var _heightOffset = 0;
	var _widthOffset = 0;
	var _initialized = false;
	var ScrollerDirections = {HorizontalRightToLeft:0, HorizontalLeftToRight:1, VerticalTopToBottom:2, VerticalBottomToTop:3, None:4};
	var ScrollerSpeeds = {Stopped:0, ReallySlow:1, Slow:2, Medium:3, Fast:4, Fastest:5};
	var _tempSpeed = ScrollerSpeeds.Stopped;
	var tempContentS = null;
	var tempContentD = null;
	var tempContentElementS = "span";
	var tempContentElementD = "div";
	var _currentPos = 0;
	
	//Public properties
	this.content = '';
	this.direction = ScrollerDirections.HorizontalRightToLeft;
	this.height = '';
	this.name = '';	
	this.speed = ScrollerSpeeds.ReallySlow;
	this.width = '';
	this.timer = 0;

	this.initialize = function()
	{
		with (document)
		{
			if ((this.direction == ScrollerDirections.HorizontalRightToLeft) ||
				 (this.direction == ScrollerDirections.HorizontalLeftToRight))
			{
				write("<div id=\"" + this.name + "_Ticker\" style=\"position:relative; overflow: hidden;\">");
				write("<div id=\"" + this.name + "_Element0\" style=\"left:0;position:absolute;top:0;width:100%;z-index:3\" onmouseover=\"" + this.name + ".stop()\" onmouseout=\"" + this.name + ".start()\">");
				write("<div id=\"" + this.name + "_Element1\" style=\"left:0;position:absolute;top:0;width:100%;z-index:2\"></div>");
				write("<div id=\"" + this.name + "_Element2\" style=\"left:0;position:absolute;top:0;width:100%;z-index:1\"></div>");
				write("</div>");
				write("</div>");
			}
			else
			{
				write("<div id=\"" + this.name + "_Ticker\" style=\"position:relative; overflow: hidden;\">");
				write("<div id=\"" + this.name + "_Element0\" style=\"left:0;position:absolute;top:0;width:100%;z-index:3\" onmouseover=\"" + this.name + ".stop()\" onmouseout=\"" + this.name + ".start()\">");
				write("<div id=\"" + this.name + "_Element1\" style=\"position:relative;width:100%;z-index:2;display:block;top:0\"></div>");
				write("<div id=\"" + this.name + "_Element2\" style=\"position:relative;width:100%;z-index:1;display:block\"></div>");
				write("</div>");
				write("</div>");
			}
		}
		
		_element0 = new cmxElement();
		_element0.setId(this.name + "_Element0");
		_element1 = new cmxElement();
		_element1.setId(this.name + "_Element1");
		_element2 = new cmxElement();
		_element2.setId(this.name + "_Element2");
		
		if ((this.direction == ScrollerDirections.HorizontalRightToLeft) ||
			 (this.direction == ScrollerDirections.HorizontalLeftToRight))
		{
			this.content = "<nobr>" + this.content + "</nobr>";
			_widthOffset = 100;
		}
		else
		{
			_heightOffset = 100;
		}
		
		tempContentS = new Element('span');
		tempContentD = new Element('div');
		tempContentS.innerHTML = tempContentD.innerHTML = this.content;
		tempContentS.style.visibility = "hidden";
		tempContentD.style.visibility = "hidden";	
		
		if (this.width != '')
		{
			tempContentS.style.width = this.width + "px";
			tempContentD.style.width = this.width + "px";
		}
		if (this.height != '')
		{
			tempContentS.style.height = this.height + "px";
			tempContentD.style.height = this.height + "px";
		}
		
		document.body.appendChild(tempContentS);
		document.body.appendChild(tempContentD);
		
		var tempContentS_height = $(tempContentS).getHeight();
		var tempContentS_width = $(tempContentS).getWidth();
		var tempContentD_height = $(tempContentD).getHeight();
		var tempContentD_width = $(tempContentD).getWidth();
		
		if (tempContentS.scrollHeight > tempContentS_height)
		{
			tempContentS_height = tempContentS.scrollHeight;
		}
		
		if (tempContentD.scrollHeight > tempContentD_height)
		{
			tempContentD_height = tempContentD.scrollHeight;
		}
		
		_contentWidth = (tempContentS_width > tempContentD_width) ? tempContentS_width : tempContentD_width;
		_contentHeight = (tempContentS_height > tempContentD_height) ? tempContentS_height : tempContentD_height;
				
		//_contentHeight = _contentHeight + _heightOffset;
		//_contentWidth = _contentWidth + _widthOffset;
		storedWidth = _contentWidth;
		storedOffsetWidth = _contentWidth;
		
		ticker = new cmxElement();
		ticker.setId(this.name + "_Ticker");
		ticker.style.height = (this.height != '' ? this.height : parseInt(_contentHeight)) + "px";
		ticker.style.width = this.width != '' ? this.width + "px" : "100%";
		
		document.body.removeChild(tempContentS);
		document.body.removeChild(tempContentD);
		
		_element1.element.innerHTML = _element2.element.innerHTML = this.content;
		
		_tempSpeed = this.speed;
		
		_initialized = true;
		
		this.height = $(_element1.id).getHeight();
		
		if ((this.direction == ScrollerDirections.HorizontalRightToLeft) ||
			 (this.direction == ScrollerDirections.HorizontalLeftToRight))
		{
			_element2.style.left = _contentWidth + "px";
		}
		else
		{
			ticker.style.height = (this.height != '') ? this.height + "px" : "100%";
		}
	};
	
	this.scroll = function()
	{
		switch (this.direction)
		{
			case ScrollerDirections.HorizontalLeftToRight: //Left to Right
				if (cmxbrowser.ie == false) _currentPos = (_contentWidth + this.speed) % _actualWidth;
				if (cmxbrowser.ie == true) _currentPos = (_contentWidth + this.speed) % _actualWidth;
				_element0.style.left = _currentPos - _contentWidth + "px";
				break;
				
			case ScrollerDirections.VerticalTopToBottom: //Top to Bottom					
				if (cmxbrowser.ie == false) _currentPos = (_currentPos - (this.speed / 4)) % _contentHeight;
				if (cmxbrowser.ie == true) _currentPos = (_currentPos - (this.speed / 4)) % _contentHeight;
				_element0.style.top = _currentPos + "px";
				break;
				
			case ScrollerDirections.VerticalBottomToTop: //Bottom to Top
				if (cmxbrowser.ie == false) _currentPos = (_currentPos + (this.speed / 4)) % _contentHeight;
				if (cmxbrowser.ie == true) _currentPos = (_currentPos + (this.speed / 4)) % _contentHeight;
				_element0.style.top = _currentPos - _contentHeight + "px";
				break;
				
			default: //ScrollerDirections.HorizontalRightToLeft ~ Right to Left
				if (cmxbrowser.ie == false) _currentPos = (_currentPos - this.speed) % _contentWidth;
				if (cmxbrowser.ie == true) _currentPos = (_currentPos - this.speed) % _contentWidth;
				_element0.style.left = _currentPos + "px";
		}
	};

	this.start = function()
	{
	   if (cmxbrowser.supportsDOM)
		{
		   if (!_initialized)
		   {
			   this.initialize();
			   _initialized = true;
		   }
			
		   this.speed = _tempSpeed;
		   
		   if (this.speed > ScrollerSpeeds.Stopped)
		   {		    
		      this.timer = setInterval(this.name + ".scroll();", 30);
		   }
		}
	};

	this.stop = function()
	{
		_tempSpeed = this.speed;
		this.speed = ScrollerSpeeds.Stopped;
		window.clearInterval(this.timer);
	};		
	
}
