/**
 * A Playlist is a logical sequence of events that are played back on an interval cycle
 * author: Alex Vigdor     copyright 2007 ABCNews Internet Ventures
 **/
var allPlaylistsOnPage = new Object();

function pausePL(){
	for(id in allPlaylistsOnPage){
		allPlaylistsOnPage[id].pause();
	}
}
function resumePL(){
	for(id in allPlaylistsOnPage){
		allPlaylistsOnPage[id].resume();
	}
}

/**
 * A playlist item represents one step in the playlist.  It should have a handler
 * function that is executed when the item is on deck, another when it is activated,
 * and a third when it is deactivated 
 **/
function PlaylistItem(id){
	this.id = id;
	this.ondeck;
	this.activate;
	this.deactivate;
}

function Playlist(id,lifecycle){
	this.id = id;
	var old = allPlaylistsOnPage[this.id];
	this.paused=false;
	
	allPlaylistsOnPage[this.id] = this;
	this.playing=false;	
	this.preload=false;
	this.interval=1000;
	this.showLength =3;
	this.timer = null;
	this.items = new Array();
	this.tags = new Array();
	this.currentItem = null;
	this.pointer = 0;
	this.inits = new Array();
	this.inited=false;
	this.destroys = new Array();
	this.destroyed=false;
}

Playlist.prototype.addInit = function(initFunc){
	this.inits.push(initFunc);
}

Playlist.prototype.addDestroy = function(destroyFunc){
	this.destroys.push(destroyFunc);
}

Playlist.prototype.destroy = function(){
	if(!this.destroyed){
		this.destroyed=true;
		this.pause();
		var len = this.destroys.length;
		for(var i=0;i<len;i++){
			this.destroys[i]();
		}
		var len = this.items.length;
		var item;
		for(var i=0;i<len;i++){
			item = this.items[i];
			if(typeof item.destroy == 'function'){
				item.destroy();
				this.items[i]=null;
			}
		}
	}
}

Playlist.prototype.next = function(){
	if(this.timer!=null){
		window.clearTimeout(this.timer);
		this.timer=null;
	}
	this.autoForward(arguments);
	return false;
}

Playlist.prototype.back = function(){
	if(this.timer!=null){
		window.clearTimeout(this.timer);
		this.timer=null;
	}
		if(this.pointer-2 < 0){
			this.pointer=this.items.length-2;
		}
		else{
			this.pointer -=2;
		}

	this.autoForward(arguments);
	return false;
}


Playlist.prototype.gotoItem = function(id){
	var len = this.items.length;
	for(var i=0;i<len;i++){
		if(this.items[i].id==id){
			if(this.currentItem==null || this.currentItem.id!=id){
				this.pointer = i;
				if(this.timer!=null){
					window.clearTimeout(this.timer);
					this.timer=null;
				}
				var args = new Array();
				for(var x=1;x<arguments.length;x++){
					args.push(arguments[x]);
				}
				this.autoForward(args);
			}
			return true;
		}
	}
	return false;
}

Playlist.prototype.play = function(){
	if(!this.playing){
		if(this.items.length > 0){
			if(!this.paused){
				this.playing=true;
			}
			this.autoForward(arguments);
		}
	}
}	

Playlist.prototype.resume = function(){
	if(!this.playing && this.paused){
		this.playing=true;
		this.paused=false;
		var pl = this;
		this.timer = window.setTimeout(function(){pl.autoForward();pl=null;},this.interval);
	}
}

Playlist.prototype.pause = function(){
	if(this.playing){
		this.playing=false;
		this.paused=true;
		if(this.timer!=null){
			window.clearTimeout(this.timer);
			this.timer=null;
		}
	}
}

Playlist.prototype.setInterval = function(millis){
	this.interval=millis;
}
Playlist.prototype.setLength = function(leng){
	this.showLength=leng;
}

Playlist.prototype.autoForward = function(args){

	if(this.items.length > 0){
		
		if(this.playing){
			var pl = this;
			this.timer = window.setTimeout(function(){pl.autoForward();pl=null},this.interval);
		}
		else{
			this.timer = null;
		}
		if(this.pointer==(this.items.length-1)){
			this.pointer=0;
		}
		else{
			this.pointer++;
		}
		//now prefetch the next item in the playlist
		document.getElementById(this.id).innerHTML = this.tags[this.pointer];
		var next = this.pointer;
		for (k=1; k<this.showLength;k++)
		{
			next ++;
			if(next  == (this.tags.length)){
				
				next=0;
				if(this.pointer==0)
				{
					next = 1
				}
			}
			document.getElementById(this.id).innerHTML += this.tags[next];
		}
		
	}
}

Playlist.prototype.addItem = function(item,tag){
	this.items[this.items.length] = item;
	this.tags[this.tags.length] = tag;
}
