/**
 * @author amosaad
 */

// get the browser name
//var browserName=navigator.appName; 
// set isIe true if the browser is IE
//var isIe = false;
//if (browserName=="Microsoft Internet Explorer"){
//	isIe = true;
//}

/**
 * 
 * @param {Object} text
 * @param {Object} elementId
 * @param {Object} scrollamount
 * @param {Object} scrolldelay
 * @param {Object} direction
 * @param {Object} behavior
 */
function Ticker(elementId, interval, delay, direction){
	// get the browser name
	var browserName=navigator.appName; 
	// set isIe true if the browser is IE
	var isIe = false;
	if (browserName=="Microsoft Internet Explorer"){
		isIe = true;
	}
	this.tickerItems = new Array();
	this.currentItem = -1;
	this.currentChar = 0;
	this.cursorText = "...";
	this.timerItems = null;
	this.element = null;
	this.anchor = null;
	this.resetHref = true;
	
	// this variable to tell if the stop was due to user event or not
	// if it's true, then don't the currentChr to 0 as it's 
//	this.doNotReset = false;
	
//	this.elementId = elementId;
	if (elementId != null){
		// get the container element
		this.element = document.getElementById(elementId);
	}
	
	
	this.parse = function(divId){
		this.tickerItems = new Array();
		if (divId == null){
			return;
		}
		var e = document.getElementById(divId);

		if (e != null){
			var ul = e.getElementsByTagName("ul")[0];
			var items = ul.getElementsByTagName("li");
			for (var i=0; i<items.length; i++){
				var anchors = items[i].getElementsByTagName("a");
				var item = new Object();
				if (isIe){
					item.text = items[i].innerText;
				}else{
					item.text = items[i].textContent;
				}
				if (anchors){
					item.href = anchors[0];
				}
				this.tickerItems.push(item);
			}
			e.innerHTML = "";
		}
	}
	
	this.setText = function (elementId, text, href){
//		alert(_this)
		if (elementId != null) {
			// get the container element
			element = document.getElementById(elementId);
			if (text != null && this.element != null){
				if (href){
					if (! this.anchor){
						this.anchor = document.createElement("a");
						element.appendChild(this.anchor);
						var _this = this;
						this.anchor.onmouseover = function(){
							clearInterval(_this.timerChars)
							_this.timerChars = null;	
							clearInterval(_this.timerItems);
							_this.timerItems = null;
						}
						this.anchor.onmouseout = function(){
							clearInterval(_this.timerChars)
							_this.timerChars = null;
							clearInterval(_this.timerItems);
							_this.timerItems = null;
						
							_this.timerChars = setInterval(_this.handler_char(), interval);
						}
					}
					if (this.resetHref == true){
						this.anchor.href = href;
						this.resetHref = false;
					}
					
					if (isIe){
						this.anchor.innerText = text;
					}else{
						this.anchor.textContent = text;
					}
					
				}else{
					if (isIe){
						element.innerText = text;
					}else{
						element.textContent = text;
					}
				}
			}
		}
	}

	this.step = function (){
		// reset the current character
		this.currentChar = 0;
		// move to the next item
		this.currentItem ++;
		// adjust if current Index is bigger that the items count
		this.currentItem %= this.tickerItems.length;
		
		// as the item is changed, tell js to change the href
		this.resetHref = true;
		
		// clear the interval on the items to avoid conflict while the characters timer 
		// is working
		clearInterval(this.timerItems);
		// set the interval for the characters timer
		this.timerChars = setInterval(this.handler_char(), interval);
	}
	this.charStep = function (){

    if (this.tickerItems.length == 0) return;		

		// get the item to work on from the items array
		var item = this.tickerItems[this.currentItem];

		// if the item is linkable, then it has href
		if (item.href){
			// call the set text passing to it the href
			this.setText(elementId, item.text.substring(0, this.currentChar++) + this.cursorText, item.href);
		}else{
			// call the set text WITHOUT passing to it an href, it handels that
			this.setText(elementId, item.text.substring(0, this.currentChar++) + this.cursorText);
		}
		// if the characters are all typed, then need to stop the timer and start the items
		// timer again to switch to next items ....
		if (this.currentChar >= this.tickerItems[this.currentItem].text.length ){
			// clear the characters timer
			clearInterval(this.timerChars);
			// start the items timer
			this.timerItems = setInterval(this.handler(), delay);
//			// tell the step function to reset
//			this.doNotReset = false;
		}
	}
	this.handler = function(){
		var _this = this;
		return function(){
			_this.step();
		}
		
	}
	this.handler_char = function(_this){
		var _this = this;
		return function(){
			_this.charStep();
		}
	}
	this.active = 0;
	/**
	 * Starts the current timer
	 */
	this.start =  function (){
		if (this.active){
			// throw an exception, should be IllegalStateException
			throw "IllegalStateException: Ticker already started ...";
			return false;
		}
		// activation flag
		this.active = 1;
		// set the timer interval
		this.timerItems = setInterval(this.handler(), delay);
	}
	this.stop =  function (){
		if (!this.active){
			// throw an exception, should be IllegalStateException
			throw "IllegalStateException: Ticker already stopped ...";
		}
		// activation flag
		this.active = 0;
		clearInterval(this.timerChars);
		clearInterval(this.timerItems);
	}

	this.parse(elementId);
}

