/**
 * @author Pawel Malisak 'stormfly'
 * @author Andrzej Sobel <andrzej.sobel@pronet.com.pl>
 */

function MovingText(sText, iSpeed)
{
	this.eText = document.getElementById(sText);
	
	this.iSpeed = iSpeed;
	
	this.bBorders = false;
	
	this.iLengthMoved = 0;
	
	this.iLengthLimit = 0;
	
	this.run = function(iType, iStart, iLength, iLengthShow)
	{
		this.iType = iType;
		this.iStart = iStart;
		this.iLength = iLength;
		this.iLengthShow = iLengthShow;
		
		this.bum();
	}

	this.setLimit = function(iLimit)
	{
		this.iLengthLimit = iLimit;
	}

	this.setBorder = function(bBorder)
	{
		this.bBorders = bBorder;
	}
	
	this.type = function(iType)
	{
		this.iType = iType;
	}
	
	this.stop = function()
	{
		this.iSpeed = 0;
	}
	
	this.start = function(iSpeed)
	{
		this.iSpeed = iSpeed;
		this.bum();
	}
	
	this.bum = function()
	{
		var oSelf = this;
		
		switch(this.iType)
		{
		case 1:
			this.fromRightToLeft();
			break;
		case 2:
			this.fromLeftToRight();
			break;
		case 3:
			this.fromBottomToTop();
			break;
		case 4:
			this.fromTopToBottom();
			break;
		default:
			throw new Error('Error. Param iType has bad value.');
		}
		
		if(this.iSpeed) {
			setTimeout(function() {oSelf.bum()}, 1000 / this.iSpeed);
		}
	}

	this.checkLimit = function()
	{
		if(this.iLengthLimit && this.iLengthMoved >= this.iLengthLimit){
			this.stop();
			this.iLengthMoved = 0;
		}
	}
	
	this.fromRightToLeft = function()
	{
		this.eText.style.left = this.iStart + "px";
		
		if(this.bBorders){
			if(this.iStart > -this.iLength + this.iLengthShow) {
				this.iStart = this.iStart - 1;
				this.iLengthMoved++;
			}
		}else{
			if(this.iStart > -this.iLength) {
				this.iStart = this.iStart - 1;
				this.iLengthMoved++;
			} else {
				this.iStart = this.iLengthShow;
			}
		}
		
		this.checkLimit();
	}
	
	this.fromLeftToRight = function()
	{
		this.eText.style.left = this.iStart + "px";
		
		if(this.bBorders){
			if(this.iStart < 0) {
				this.iStart = this.iStart + 1;
				this.iLengthMoved++;
			}
		}else{
			if(this.iStart < this.iLength) {
				this.iStart = this.iStart + 1;
				this.iLengthMoved++;
			} else {
				this.iStart = this.iLengthShow;
			}
		}
		
		this.checkLimit();
	}
	
	this.fromBottomToTop = function()
	{
		this.eText.style.top = this.iStart + "px";
		
		if(this.bBorders){
			if(this.iStart > -this.iLength + this.iLengthShow) {
				this.iStart = this.iStart - 1;
				this.iLengthMoved++;
			}
		}else{
			if(this.iStart > -this.iLength) {
				this.iStart = this.iStart - 1;
				this.iLengthMoved++;
			} else {
				this.iStart = this.iLengthShow;
			}
		}
		
		this.checkLimit();
	}
	
	this.fromTopToBottom = function()
	{
		this.eText.style.top = this.iStart + "px";

		if(this.bBorders){
			if(this.iStart < 0) {
				this.iStart = this.iStart + 1;
				this.iLengthMoved++;
			}
		}else{
			if(this.iStart < this.iLength) {
				this.iStart = this.iStart + 1;
				this.iLengthMoved++;
			} else {
				this.iStart = this.iLengthShow;
			}
		}
		
		this.checkLimit();
	}
}
