var _tickerId = '';

function wmxXmlScroller()
{
	//Private variables
	var _contentHeight = 0;
	var _contentWidth = 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;
	var _xmlTicker = null;
	var _xmlElement0 = null;
	var _xmlElement1 = null;
	var _xmlElement2 = null;
	
	//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(typeof($(this.name + '_Container')) != 'undefined')
			{
				var scrollerContainer = $(this.name + "_Container");
				_xmlTicker = new Element('div', {'id': this.name + '_Ticker'});
				_xmlElement0 = new Element('div', {'id': this.name + '_Element0'});
				_xmlElement1 = new Element('div', {'id': this.name + '_Element1', 'class': 'xmlScroller'});
				_xmlElement2 = new Element('div', {'id': this.name + '_Element2', 'class': 'xmlScroller'});
				
				_xmlTicker.setStyle({
					position: 'relative',
					overflow: 'hidden'
				});
				_xmlElement0.setStyle({
					position: 'absolute',
					top: '0',
					left: '0',
					width: '100%',
					zIndex: '3'
				});
				
				Element.observe(_xmlElement0, 'mouseover', this.stop.bind(this));
				Element.observe(_xmlElement0, 'mouseout', this.start.bind(this));
				
				if ((this.direction == ScrollerDirections.HorizontalRightToLeft) ||
					 (this.direction == ScrollerDirections.HorizontalLeftToRight))
				{
					_xmlElement1.setStyle({
						position: 'absolute',
						top: '0',
						left: '0',
						zIndex: '2'
					});
					_xmlElement2.setStyle({
						position: 'absolute',
						top: '0',
						left: '0',
						zIndex: '1'
					});
				}
				else
				{
					_xmlElement1.setStyle({
						display: 'block',
						position: 'relative',
						top: '0',
						width: '100%',
						zIndex: '2'
					});
					_xmlElement2.setStyle({
						display: 'block',
						position: 'relative',
						width: '100%',
						zIndex: '1'
					});
				}
				
				Element.insert(scrollerContainer, _xmlTicker);
				Element.insert(_xmlTicker, _xmlElement0);
				Element.insert(_xmlElement0, _xmlElement1);
				Element.insert(_xmlElement0, _xmlElement2);
			}
		}
		
		if ((this.direction == ScrollerDirections.HorizontalRightToLeft) ||
			 (this.direction == ScrollerDirections.HorizontalLeftToRight))
		{
			this.content = "<nobr>" + this.content + "</nobr>";
		}
		
		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";
		}
		
		var tempFontSize = _xmlElement1.getStyle('fontSize');
		var tempFontWeight = _xmlElement1.getStyle('fontWeight');

		document.body.appendChild(tempContentS);
		document.body.appendChild(tempContentD);
		
		if(tempFontSize != '')
		{
			$(tempContentS).setStyle({'fontSize': tempFontSize});
			$(tempContentD).setStyle({'fontSize': tempFontSize});
		}
		if(tempFontWeight != '')
		{
			$(tempContentS).setStyle({'fontWeight': tempFontWeight});
			$(tempContentD).setStyle({'fontWeight': tempFontWeight});
		}
		
		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;
		
		document.body.removeChild(tempContentS);
		document.body.removeChild(tempContentD);
		
		_xmlElement1.innerHTML = _xmlElement2.innerHTML = this.content;
		
		var element1Width = _xmlElement1.getWidth();
		var element2Width = _xmlElement2.getWidth();
		var element1Height = _xmlElement1.getHeight();
		var element2Height = _xmlElement2.getHeight();
		
		_contentWidth = (element1Width > _contentWidth) ? element1Width : _contentWidth;
		_contentWidth = (element2Width > _contentWidth) ? element2Width : _contentWidth;
		_contentHeight = (element1Height > _contentHeight) ? element1Height : _contentHeight;
		_contentHeight = (element2Height > _contentHeight) ? element2Height : _contentHeight;
		
		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%";
		
		_tempSpeed = this.speed;
		
		_initialized = true;
		
		this.height = _contentHeight;
				
		if ((this.direction == ScrollerDirections.HorizontalRightToLeft) ||
			 (this.direction == ScrollerDirections.HorizontalLeftToRight))
		{
			_xmlElement2.setStyle({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;
				_xmlElement0.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;
				_xmlElement0.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;
				_xmlElement0.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;
				_xmlElement0.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);
	};		
	
}
