/* BEGIN: UTILS */
function check(e){
   if(typeof e == "string"){elm = getElm(e);}else{elm = e;}
   if(elm == null || typeof elm != "object") throw Error("Element "+e+" does not exist. It is type "+typeof elm);
   return elm;
}
function getElm(e,f){
   if (f == null) f = self;
   return f.document.getElementById(e);
}
function createE(t,id){
   var elm = document.createElement(t);
   if (id != null && id != "") {
      elm.id = id;
   }
   return elm;
}
function createElm(t,id,p){
   if (p == null){
      p = document.body;
   }
   p = check(p);
   var elm = document.createElement(t);
   if (id != null && id != "") {
      elm.id = id;
   }
   p.appendChild(elm);
   return elm;
}
function getE(id){
   var elm = document.getElementById(id);
   return elm;
}
function writeTo(e,c){
   e = check(e);
   if(c == null){
      c = " ";
   }
   var tNode = document.createTextNode(c);
   e.appendChild(tNode);
}
/* END: UTILS */

/* begin:controller */
var theHeight;
var lines = 0;
function cycle(t) {
	t.textObj.nodeValue = " ";
	lines = 0;
	theHeight = t.placeHolder.offsetHeight;
	t.linkObj.href= t.items[t.currentIndex].url;
	window.tickerTypeInterval = setInterval(function(){typewrite(t);},t.speed);
}
function resetCycle(t){
	if( window.tickerCycleDelay ){
		clearTimeout(window.tickerCycleDelay);
	}
	if( window.tickerTypeInterval ){
		clearInterval(window.tickerTypeInterval);
	}
	t.textObj.nodeValue = " ... resetting ticker, please wait.";
}
function nextCycle(t){
	t.typeIndex = 0;
	t.currentIndex = (t.currentIndex < t.items.length-1) ? t.currentIndex+1 : 0;
	window.tickerCycleDelay = setTimeout(function(){cycle(t); },t.delay);
}
function typewrite(t){
	var tmpLabel = t.items[t.currentIndex].label;
	t.typeIndex++;
	var isDone = (t.typeIndex == tmpLabel.length) ? true : false;
	
	t.textObj.nodeValue = tmpLabel.substr(0,t.typeIndex) + ((isDone) ? "" : "_");
	
	if( theHeight != t.placeHolder.offsetHeight && t.slide ){
		theHeight = t.placeHolder.offsetHeight;
		doSlide(t,lines);
		lines++;
	}
	if( isDone ) {
		clearInterval(window.tickerTypeInterval);
		nextCycle(t);	
	}
}
function doSlide(t,l){
	var top = ( ( l == 1 ) ? 0.6 : l )* t.slideUnit - ( ( l > 1 ) ? 0.6 : 0 );
	t.placeHolder.style.top = "-" + top + "em";
}
/* end:controller */

/* begin:model */
Ticker.prototype.show = function(){
	this.linkObj = createE("A","linkObj");
	this.textObj = document.createTextNode("test");
	this.linkObj.appendChild(this.textObj);
	this.placeHolder.appendChild(this.linkObj);	
	cycle(this);
}
Ticker.prototype.add = function(str,url){
	var tmpObj = new TickerItem(str,url);
	this.items.push(tmpObj);
}
Ticker.prototype.clear = function(){
	resetCycle(this);
	this.items = new Array();
	this.currentIndex = 0;
	this.typeIndex = 0;
}
Ticker.prototype.start = function(){
	cycle(this);
}
function Ticker(id,delay,speed,slide,slideUnit){
	this.id = id;
	this.items = new Array();
	this.placeHolder = document.getElementById(id);
	
	this.delay = delay;
	this.speed = speed;
	this.linkObj;
	this.slide = slide;
	this.slideUnit = slideUnit;
	
	this.currentIndex = 0;
	this.typeIndex = 0;
}

function TickerItem(label,url){
	this.label = label;
	this.url = url
}
/* end:model */

