// JavaScript Document

if(typeof SW_LIB == "undefined") var SW_LIB = new Object();
SW_LIB.TIMERS = new Object();
if(typeof SW_LIB.EVENT_MANAGER == "undefined"){
	alert("events.js file needs to be imported before this file");
}
	
IntervalEvent = {};
IntervalEvent.onStep = "IntervalEvent_onStep";

INTERVAL_LIST = {};
//
INTERVAL_LIST.CURRENT_INTERVALS = {};
INTERVAL_LIST.tmpID = 0;
INTERVAL_LIST.addInterval = function(_interval){
	// generate Timer Id
	var intervalID = this.tmpID.toString();
	this.CURRENT_INTERVALS[intervalID] = _interval;
	this.tmpID++;
	return intervalID;
}
INTERVAL_LIST.fireInterval = function(_timerID){
	this.CURRENT_INTERVALS[_timerID].step();
}

INTERVAL = function(_delay){
	this.delay = (isNaN(_delay)) ? 100 : _delay;
	this.timerID = INTERVAL_LIST.addInterval(this);
	this.n = 0;
	this.active = false;
	this.TIME_OUT = null;
	// Methods
		// Initialize EVENT_MANAGER in this Object
		this.INIT_EVENT_MNG = INIT_EVENT_MNG_CLASS;
		this.INIT_EVENT_MNG();
};
INTERVAL.prototype.start = function(){
	// Start Interval if it hasnt been started
	if(!this.active){
		var action = "INTERVAL_LIST.fireInterval("+this.timerID+")";
		this.TIME_OUT = setTimeout(action,this.delay);
		this.n = -1;
		this.active = true;
	}
}
INTERVAL.prototype.step = function(){
	this.n++;
	this.EVENT_MNG.dispatchEvent(new EVENT(IntervalEvent.onStep, this));
	if(this.active){
		var action = "INTERVAL_LIST.fireInterval("+this.timerID+")";
		this.TIME_OUT = setTimeout(action,this.delay);	
	}
}
INTERVAL.prototype.stop = function(){
	var action = "INTERVAL_LIST.fireInterval("+this.timerID+")";
	clearTimeout(this.TIME_OUT);
	this.n = -1;
	this.active = false;
}
/**/