﻿function TickerLink(Url, Title){
	this.Url="";
	if(Url!=null){this.Url=Url;}

	this.Title="";
	if(Title!=null){this.Title=Title;}
	
	this.Char=0;

	this.Next=function(){
		this.Char++;
	}
	this.Reset=function(){
		this.Char=0;
	}
	
	this.ShownTitle=function(endchar){
		var re=this.Title.substring(0,this.Char);
		if(this.Char<this.Title.length){re+=endchar;}
		return re;
	}
	this.ShownEnd=function(){
		return (this.Char>=this.Title.length);
	}
	
	this.ResetChar=function(){
		this.Char=0;
	}
}

function TickerController(Element, FrameDelay, LinkDelay, EndChar){
	this.Display=Element;
	
	Element.TickerController=this;
	if(Element.id==null || Element.id == undefined){
		var newID=null;
		while(newID==null || document.getElementById(newID)!=null){
			newID="id"+Math.random().toString();
		}
		Element.id=newID;
	}
	
	this.ElementID=Element.id;
	
	this.FrameDelay=100;
	if(FrameDelay!=null){this.FrameDelay=FrameDelay;}
	
	this.LinkDelay=1000;
	if(LinkDelay!=null){this.LinkDelay=LinkDelay;}
	
	this.EndChar="_";
	if(EndChar!=null){this.EndChar=EndChar;}
	
	this.CurrentLink=0;
	this.Links=new Array();
	
	this.Timer=null;
	
	this.Update=function(){
		var current=this.Links[this.CurrentLink];
		this.Display.innerHTML=current.ShownTitle(this.EndChar);
		this.Display.href=current.Url;
		if(current.ShownEnd()){
			this.Pause();
		}
		current.Next();
	}
	
	this.Add=function(Url, Title){
		this.Links.push(new TickerLink(Url,Title));
	}
	
	this.Next=function(){
		this.Links[this.CurrentLink].Reset();
		this.CurrentLink++;
		if(this.CurrentLink>this.Links.length-1){
			this.CurrentLink=0;
		}
	}
	
	this.Start=function(){
		if(this.Timer!=null){
			clearInterval(this.Timer);
			this.Timer=null;
		}
		
		this.Update();
		this.Timer=setInterval("document.getElementById(\""+this.ElementID+"\").TickerController.Update();", this.FrameDelay);
	}
	this.Pause=function(){
		if(this.Timer!=null){
			clearInterval(this.Timer);
			this.Timer=null;
		}
		this.Timer=setInterval("document.getElementById(\""+this.ElementID+"\").TickerController.Resume();", this.LinkDelay);
	}
	this.Resume=function(){
		this.Next();
		this.Start();
	}
	this.Stop=function(){
		clearInterval(this.Timer);
		this.Timer=null;
	}
}

