var tickerId = '';

function wmxTicker()
{
	//Private variables
	var _contentHeight = 0;
	var _contentWidth = 0;
	var _element0 = null;
	var _element1 = null;
	var _element2 = null;
	var _heightOffset = 0;
	var _initialized = false;
	var _tempSpeed = 0;
	var _widthOffset = 0;
	var tempContentS = null;
	var tempContentD = null;
	var tempContentElementS = "span";
	var tempContentElementD = "div";
	var _currentPos = 0;
	var xSpeed = 35;
	var ySpeed = 1.4;
	var xSpeedVerySlow = 150;
	var xSpeedSlow = 100;
	var xSpeedMedium = 35;
	var xSpeedFast = 25;
	var xSpeedVeryFast = 18;
	var ySpeedVerySlow = 3;
	var ySpeedSlow = 2;
	var ySpeedMedium = 1.4;
	var ySpeedFast = 1.375;
	var ySpeedVeryFast = 1.1;
	var xOffset = 200;
	var yOffset = 3;
	var _moveEffect1 = null;
	var _moveEffect2 = null;
	var _timeLeft = xSpeed;
	var _fps = 20 // Defaults to 100 but 20 is good enough for the human eye and avoids unnecessary CPU usage.
	
	//Public properties
	this.content = '';
	this.direction = 2;
	this.height = '';
	this.name = '';	
	this.speed = 1;
	this.width = '';
	this.timer = 0;
	

	this.initialize = function()
	{
		with (document)
		{
			write("<div id=\"" + this.name + "_Ticker\" style=\"position:relative; overflow: hidden;\">");
			write("<div id=\"" + this.name + "_Element0\" style=\"left:0;position:absolute;width:100%;top:0;z-index:3;text-align:center\" onmouseover=\"" + this.name + ".stop()\" onmouseout=\"" + this.name + ".resume()\">");
			write("<div id=\"" + this.name + "_Element1\" style=\"left:0;position:absolute;width:100%;top:0;z-index:2\"></div>");
			write("<div id=\"" + this.name + "_Element2\" style=\"left:0;position:absolute;width:100%;top:0;z-index:1\"></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");
		
		tickerId = _element1.id;
		
		if (this.direction < 3)
		{
			this.content = "<nobr>" + this.content + "</nobr>";
			_widthOffset = 100;
			_heightOffset = 20;
		}
		else
		{
			_heightOffset = 100;
		}
		tempContentS = document.createElement(tempContentElementS);
		tempContentD = document.createElement(tempContentElementD);
		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);
		
		_contentWidth = (tempContentS.offsetWidth > tempContentD.offsetWidth) ? tempContentS.offsetWidth : tempContentD.offsetWidth;
		_contentHeight = (tempContentS.offsetWidth > tempContentD.offsetWidth) ? tempContentS.offsetHeight : tempContentD.offsetHeight;
		
		_element1.element.innerHTML = _element2.element.innerHTML = this.content;
		if (_element1.element.scrollWidth > _contentWidth)
		{
			_contentWidth = _element1.element.scrollWidth;
		}
		_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);
		
		switch (this.direction)
		{
			case 3:
			case 4:
				_contentHeight = _contentHeight - _heightOffset;				
				ticker.style.height = _contentHeight + "px";
				_element2.style.top = _contentHeight + "px";
				break;
			default:
				_element2.style.left = _contentWidth + "px";
		}
		_tempSpeed = this.speed;
		_initialized = true;
	};
	
	// reset on left
	 this.moveResetLR = function(resetTarget)
	{
	$(resetTarget).setStyle({left: -_contentWidth + 'px', top: 0 + 'px'});
	this.moveOnLR(resetTarget);
	};
	
	// reset on right
	this.moveResetRL = function(resetTarget)
	{
	$(resetTarget).setStyle({left: _contentWidth+xOffset + 'px', top: 0 + 'px'});
	this.moveOnRL(resetTarget);
	};
	
	// reset on top
	this.moveResetTB = function(resetTarget)
	{
	$(resetTarget).setStyle({left: 0 + 'px', top: -_contentHeight-yOffset + 'px'});
	this.moveOnTB(resetTarget);
	};
	
	// reset on bottom
	this.moveResetBT = function(resetTarget)
	{
	$(resetTarget).setStyle({left: 0 + 'px', top: _contentHeight+yOffset + 'px'});
	this.moveOnBT(resetTarget);
	};
	
	// resume moveOn left to right
	this.resumeMoveOnLR = function(moveTarget, iDuration)
	{
	_moveEffect1 = new Effect.Move(moveTarget, {x:0, y:0, duration: iDuration, mode: 'absolute', transition:Effect.Transitions.linear, afterFinish:this.moveOffLR.bind(this, moveTarget), fps: _fps, queue: {scope: this.name + '_Ticker'}});
	};
	
		// resume moveOn right to left
	this.resumeMoveOnRL = function(moveTarget, iDuration)
	{
	_moveEffect1 = new Effect.Move(moveTarget, {x:0, y:0, duration: iDuration, mode: 'absolute', transition:Effect.Transitions.linear, afterFinish:this.moveOffRL.bind(this, moveTarget), fps: _fps, queue: {scope: this.name + '_Ticker'}});
	};
	
		// resume moveOn top to bottom
	this.resumeMoveOnTB = function(moveTarget, iDuration)
	{
	_moveEffect1 = new Effect.Move(moveTarget, {x:0, y:0, duration: iDuration, mode: 'absolute', transition:Effect.Transitions.linear, afterFinish:this.moveOffTB.bind(this, moveTarget), fps: _fps, queue: {scope: this.name + '_Ticker'}});
	};
	
		//resume moveOn bottom to top
	this.resumeMoveOnBT = function(moveTarget, iDuration)
	{
	_moveEffect1 = new Effect.Move(moveTarget, {x:0, y:0, duration: iDuration, mode: 'absolute', transition:Effect.Transitions.linear, afterFinish:this.moveOffBT.bind(this, moveTarget), fps: _fps, queue: {scope: this.name + '_Ticker'}});
	};
	
	// resume moveOff left to right
	this.resumeMoveOffLR = function(moveTarget, iDuration)
	{
	_moveEffect2 = new Effect.Move(moveTarget, {x:_contentWidth, y:0, duration: iDuration, mode: 'absolute', transition:Effect.Transitions.linear, afterFinish:this.moveResetLR.bind(this, moveTarget), fps: _fps, queue: {scope: this.name + '_Ticker'}});
	};
	
	// resume moveOff right to left
	this.resumeMoveOffRL = function(moveTarget, iDuration)
	{
	_moveEffect2 = new Effect.Move(moveTarget, {x:-_contentWidth-xOffset, y:0, duration: iDuration, mode: 'absolute', transition:Effect.Transitions.linear, afterFinish:this.moveResetRL.bind(this, moveTarget), fps: _fps, queue: {scope: this.name + '_Ticker'}});
	};
	
	// resume moveOff top to bottom
	this.resumeMoveOffTB = function(moveTarget, iDuration)
	{
	_moveEffect2 = new Effect.Move(moveTarget, {x:0, y:_contentHeight+yOffset, duration: iDuration, mode: 'absolute', transition:Effect.Transitions.linear, afterFinish:this.moveResetTB.bind(this, moveTarget), fps: _fps, queue: {scope: this.name + '_Ticker'}});
	};
	
	// resume moveOff bottom to top
	this.resumeMoveOffBT = function(moveTarget, iDuration)
	{
	_moveEffect2 = new Effect.Move(moveTarget, {x:0, y:-_contentHeight-yOffset, duration: iDuration, mode: 'absolute', transition:Effect.Transitions.linear, afterFinish:this.moveResetBT.bind(this, moveTarget), fps: _fps, queue: {scope: this.name + '_Ticker'}});
	};
	
	// moveOn left to right
	this.moveOnLR = function(moveTarget)
	{
	_moveEffect1 = new Effect.Move(moveTarget, {x:0, y:0, duration: xSpeed, mode: 'absolute', transition:Effect.Transitions.linear, afterFinish:this.moveOffLR.bind(this, moveTarget), fps: _fps, queue: {scope: this.name + '_Ticker'}});
	};
	
	// moveOn right to left
	this.moveOnRL = function(moveTarget)
	{
	_moveEffect1 = new Effect.Move(moveTarget, {x:0, y:0, duration: xSpeed, mode: 'absolute', transition:Effect.Transitions.linear, afterFinish:this.moveOffRL.bind(this, moveTarget), fps: _fps, queue: {scope: this.name + '_Ticker'}});
	};
	
	// moveOn top to bottom
	this.moveOnTB = function(moveTarget)
	{
	_moveEffect1 = new Effect.Move(moveTarget, {x:0, y:_contentHeight+yOffset, duration: ySpeed, transition:Effect.Transitions.linear, afterFinish:this.moveOffTB.bind(this, moveTarget), fps: _fps, queue: {scope: this.name + '_Ticker'}});
	};
	
	// moveOn bottom to top
	this.moveOnBT = function(moveTarget)
	{
	_moveEffect1 = new Effect.Move(moveTarget, {x:0, y:-_contentHeight-yOffset, duration: ySpeed, transition:Effect.Transitions.linear, afterFinish:this.moveOffBT.bind(this, moveTarget), fps: _fps, queue: {scope: this.name + '_Ticker'}});
	};
	
	// moveOff left to right
	this.moveOffLR = function(moveTarget)
	{
	_moveEffect2 = new Effect.Move(moveTarget, {x:_contentWidth, y:0, duration: xSpeed, mode: 'absolute', transition:Effect.Transitions.linear, afterFinish:this.moveResetLR.bind(this, moveTarget), fps: _fps, queue: {scope: this.name + '_Ticker'}});
	};
	
	// moveOff right to left
	this.moveOffRL = function(moveTarget)
	{
	_moveEffect2 = new Effect.Move(moveTarget, {x:-_contentWidth-xOffset, y:0, duration: xSpeed, mode: 'absolute', transition:Effect.Transitions.linear, afterFinish:this.moveResetRL.bind(this, moveTarget), fps: _fps, queue: {scope: this.name + '_Ticker'}});
	};
	
	// moveOff top to bottom
	this.moveOffTB = function(moveTarget)
	{
	_moveEffect2 = new Effect.Move(moveTarget, {x:0, y:_contentHeight+yOffset, duration: ySpeed, transition:Effect.Transitions.linear, afterFinish:this.moveResetTB.bind(this, moveTarget), fps: _fps, queue: {scope: this.name + '_Ticker'}});
	};
	
	// moveOff bottom to top
	this.moveOffBT = function(moveTarget)
	{
	_moveEffect2 = new Effect.Move(moveTarget, {x:0, y:-_contentHeight-yOffset, duration: ySpeed, transition:Effect.Transitions.linear, afterFinish:this.moveResetBT.bind(this, moveTarget), fps: _fps, queue: {scope: this.name + '_Ticker'}});
	};
	
	this.speedSwitch = function()
	{
	switch (this.speed)
		{
			case 1: //very slow
				xSpeed = xSpeedVerySlow;
				ySpeed = ySpeedVerySlow;
				break;
			case 2: //slow	
				xSpeed = xSpeedSlow;
				ySpeed = ySpeedSlow;
				break;
			case 4: //fast			
				xSpeed = xSpeedFast;
				ySpeed = ySpeedFast;		
				break;
			case 5://very fast
				xSpeed = xSpeedVeryFast;
				ySpeed = ySpeedVeryFast;
				break;
			default: //medium
				xSpeed = xSpeedMedium;
				ySpeed = ySpeedMedium;
		}
	};
	this.scroll = function(resume)
	{
		switch (this.direction)
		{
			case 1: //Left to Right
				if(resume) {
					this.speedSwitch();
					this.resumeMoveOffLR(_moveEffect2.element.id, _timeLeft);
					this.resumeMoveOnLR(_moveEffect1.element.id, _timeLeft);
				} else {
					this.speedSwitch();
					this.moveOffLR(_element1.id);
					this.moveResetLR(_element2.id);
				}
					break;
			case 3: //Top to Bottom
				if(resume) {
					this.speedSwitch();
					this.resumeMoveOffTB(_moveEffect2.element.id, _timeLeft);
					this.resumeMoveOnTB(_moveEffect1.element.id, _timeLeft);
				} else {
					this.speedSwitch();
					this.moveOffTB(_element1.id);
					this.moveResetTB(_element2.id);
				}	
				break;
			case 4: //Bottom to Top				
					if(resume) {
					this.speedSwitch();
					this.resumeMoveOffBT(_moveEffect2.element.id, _timeLeft);
					this.resumeMoveOnBT(_moveEffect1.element.id, _timeLeft);
				} else {
					this.speedSwitch();
					this.moveOffBT(_element1.id);
					this.moveResetBT(_element2.id);
				}	
				break;
			default: //Right to Left
				if(resume) {
					this.speedSwitch();
					this.resumeMoveOffRL(_moveEffect2.element.id, _timeLeft);
					this.resumeMoveOnRL(_moveEffect1.element.id, _timeLeft);
				} else {
					this.speedSwitch();
					this.moveOffRL(_element1.id);
					this.moveResetRL(_element2.id);
				}
		}
	};

	this.start = function()
	{
	   if (cmxbrowser.supportsDOM)
		{
		   if (!_initialized)
		   {
			   this.initialize();
			   _initialized = true;
			   if (this.speed != 0)
			   {		    
					this.scroll(false);
			   }
		   }
		   this.speed = _tempSpeed;
		}
	};
	
	this.resume = function()
	{
	this.scroll(true);
	};

	this.stop = function()
	{
		var currentTime=new Date();
		var timeFinish = _moveEffect1.finishOn;
		var totalTime = _moveEffect1.totalTime;
		 _timeLeft = (timeFinish-currentTime)/1000;
		_moveEffect1.cancel();
		_moveEffect2.cancel();
	};		
}


