﻿// Countdown MM:SS Object
// Usage:
//	myCounter = new Counter(300,'counter_tag');
//	myCounter.start('window.close()')
// -----------------------------------------------
// Constractor:
// time: the time in seconds for the countdown
// id: the HTML element to write the remaining time to (in format of MM:SS)  
function count(time,id){
	this.element = document.getElementById(id);
	this.time = time;
	this.timer = null;
}
// Start counter:
// run_on_end: a javascript command or function to invoke when the conter ends.
count.prototype.start = function(run_on_end){
	var obj = this;
	
	cycle = function(){				
		var min = Math.floor(obj.time/60);
		var sec = obj.time%60;
		if (min < 10) min = "0"+""+min ;
		if (sec < 10) sec = "0"+""+sec ;
		//alert(min+":"+sec);
		obj.element.innerHTML = min+":"+sec;
		if (obj.time == 0) {
			clearTimeout(obj.timer);
			if (run_on_end) eval(run_on_end+";");
			else {
				window.opener = self;
				window.close();
			}
		}
		else
			obj.time--;
			obj.timer = setTimeout(cycle,990);
	}
	cycle()
}


