/**
 * @author pit
 */

/**
 * 
 * @param {Array} newsArray
 * @param {Node} parentElement
 * @param {Number} tickerWidth
 * @param {Number} tickerHeight
 */
function NewsTicker(newsArray, parentElement, tickerWidth, tickerHeight)
{
	/* ticker animation step (in pixels) */
	this.animationStep = 2;
	
	/* DOM elemnent containing the currently shown news title */
	this.newsElement = null;
	this.emptyElement = null;
	
	/* array containing the news titles */
	this.newsArray = newsArray;
	
	/* index of the news currently shown */
	this.currentNewsIndex = 0;
	
	/* ID of the animation returned by setInterval() */
	this.animationInterval = null;
	
	/* news click handler */
	this.newsClickHandler = null;
	
	/* call to the DOM initialization method */
	this.init(parentElement, tickerWidth, tickerHeight);
}

NewsTicker.cssClass = 'news_ticker';

/**
 * Initializes the component DOM element, and appends it to the parentElement
 * @param {Node} parentElement
 * @param {Number} tickerHeight
 */
NewsTicker.prototype.init = function(parentElement, tickerWidth, tickerHeight)
{
	var mainElement = document.createElement('div');
	mainElement.className = NewsTicker.cssClass;
	mainElement.style.width = tickerWidth;
	mainElement.style.height = tickerHeight;
	mainElement.style.overflow = 'hidden';
	mainElement.style.position = 'relative';
	
	var fakeElement = document.createElement('div');
	mainElement.appendChild(fakeElement);
	this.emptyElement = fakeElement;
	
	var textElement = document.createElement('div');
	textElement.style.position = 'absolute';
	textElement.style.whiteSpace = 'nowrap';

	this.newsElement = textElement;
	
	mainElement.appendChild(textElement);
	
	parentElement.appendChild(mainElement);
	
	textElement.innerHTML = this.newsArray[0];
}
/**
 * Animates the news ticker from right to left.
 * 
 * If animation is ended, then calls showNextNews() to show
 * the next available news.
 */
NewsTicker.prototype.animate = function()
{
	var textLeft = parseInt(this.newsElement.offsetLeft);
	var textWidth = parseInt(this.newsElement.offsetWidth);
	
	if(textLeft + textWidth > this.emptyElement.offsetWidth)
	{
		var newLeft = textLeft - this.animationStep;
		
		this.newsElement.style.left = newLeft + 'px';
	}
	else
	{
		this.stopAnimation();
		
		var self = this;
		
		setTimeout(
			function()
			{
				self.showNextNews();
			},
			2000
		);
	}
}

/**
 * Starts the news ticker
 */
NewsTicker.prototype.start = function()
{
	var self = this;
	
	setTimeout(
		function()
		{
			self.startAnimation();
		},
		2000
	);
}

/**
 * shows the next available news
 */
NewsTicker.prototype.showNextNews = function()
{
	this.currentNewsIndex = (this.currentNewsIndex + 1) % this.newsArray.length;
	
	this.newsElement.style.left = '0px';
	
	this.newsElement.innerHTML = this.newsArray[this.currentNewsIndex];
	
	var self = this; 
	
	setTimeout(
		function()
		{
			self.startAnimation();
		},
		2000
	);
}

/**
 * Starts the news ticker animation
 */
NewsTicker.prototype.startAnimation = function()
{
	if(this.animationInterval == null)
	{
		var self = this;
		
		this.animationInterval = setInterval(
			function()
			{
				self.animate();
			},
			100
		);
	}
}

/**
 * Stops the news ticker animation
 */
NewsTicker.prototype.stopAnimation = function()
{
	if(this.animationInterval != null)
	{
		clearInterval(this.animationInterval);
		
		this.animationInterval = null;
	}
}
/**
 * Sets the click handler f
 * 
 * @param {Function} clickHandler
 */
NewsTicker.prototype.setNewsClickHandler = function(clickHandler)
{
	this.newsClickHandler = clickHandler;
	
	if(clickHandler != null)
	{
		var self = this;
		
		this.newsElement.onclick = function()
		{
			self.newsClickHandler(self.currentNewsIndex);
		}
	}
	else
	{
		this.newsElement.onclick = undefined;
	}
}

/**
 * Sets the number of pixels per single animation step
 * 
 * @param {Number} stepPixels
 */
NewsTicker.prototype.setAnimationStepPixels = function(stepPixels)
{
	if(stepPixels < 0)
		stepPixels = 1;
		
	this.animationStep = stepPixels;
}
