﻿var aTimerDisp=0, aTimerItems=new Array();

function AddNew(aItem){
	aItem.setParentIndex(aTimerItems.length);
	aTimerItems.length++; 
	aTimerItems[aTimerItems.length-1]=aItem;
}

function AddItem(aItem){
	for(var i=0;i<aTimerItems.length;i++) {
		if(aTimerItems[i]==null){
		 aTimerItems[i] = aItem;
		 return;
		}
	}
	AddNew(aItem);
}

function DropItem(aItem){
	for(var i=0;i<aTimerItems.length;i++){
		if(aTimerItems[i]==aItem) aTimerItems[i]=null;
	}
}

function AddItem(aItem){
	aItem.parentIndex=aTimerItems.length;
	aTimerItems.length++; 
	aTimerItems[aTimerItems.length-1]=aItem;
}

function TimeDispatch(aTime){
	for(var i=0;i<aTimerItems.length;i++)
		aTimerItems[i].next();
		
	if(aTime==undefined) aTime=100;
	if(aTimerDisp!=0) window.clearTimeout(aTimerDisp);
	aTimerDisp=window.setTimeout("TimeDispatch("+ aTime +");", aTime);
}

TTicker = function(aTicks, aAutoStart){
	this.ticks = aTicks;
	this.autoValue = aTicks;
	this.autoStart = !(!aAutoStart);
}

TTicker.prototype.tick = function(){
	this.ticks--;
	var res = (this.ticks <= 0);
	if(res){
		this.ticks = 0;
		if(this.autoStart) this.ticks=this.autoValue;
	}
	return res;
}

TTicker.prototype.setValue = function(aValue){
	this.ticks = aValue;
}


