/*
		Javascript Timer Object
		By: Jason Powell
		
		Syntax:
		var oTimer = new JTO("13:00:00", 'oTimer');	// to start a timer at 1:00 pm
			   ^			  HH:MM:SS		 ^
			   +-----------------------------+	MUST BE THE SAME!
		
		Properties:
			dStart		= The start time to display
			dDisplay	= The current display time
			imageDir	= The directory to find the digit images
			name		= Name of the object
			extension	= The file extension of the digit images
		
		Methods:
*/

// JTO Class
var JTO = function(sTime, sName, sExt) {
	var aTime = sTime.split(':');
	var dNow = new Date();
	var iHur		= parseInt(aTime[0], 10);
	var iMin		= parseInt(aTime[1], 10);
	var iSec		= parseInt(aTime[2], 10);
	
	// properties
	this.dStart		= new Date();
	this.dDisplay	= new Date(dNow.getYear(), dNow.getMonth(), dNow.getDate(), iHur, iMin, iSec);
	this.imageDir	= 'images/JTO_timer';
	this.name		= sName;
	this.extension	= sExt	||	'.gif';
	
	// methods 
	this.initialize();
}

// Constructor
JTO.prototype.initialize = function() {
	this.render();
	this.timer();
}

// Rendering function
JTO.prototype.render = function() {
	document.write('<div id="' + this.name + '">');
	document.write('<img src="" id="' + this.name + '_h1"><img src="" id="' + this.name + '_h2">');	// write Hour portion
	document.write('<img src="' + this.imageDir + '/y' + this.extension + '">');																// write seperator
	document.write('<img src="" id="' + this.name + '_m1"><img src="" id="' + this.name + '_m2">');	// write minute portion
	document.write('</div>\r\n');
}

// Timer function
JTO.prototype.timer = function() {
//timer = function(eTimer) {
	var dNow = new Date();
	var iDiff = dNow.getTime() - this.dStart.getTime();
	var dTimer = new Date(this.dDisplay.getTime() + iDiff)
	var sExt = this.extension;
	
	var sHur = dTimer.getHours() + "";
	var sMin = dTimer.getMinutes() + "";
	if(parseInt(sHur) <= 9) sHur = "0" + sHur;
	if(parseInt(sMin) <= 9) sMin = "0" + sMin;
	var h1 = sHur.substring(0,1);
	var h2 = sHur.substring(1,2);
	var m1 = sMin.substring(0,1);
	var m2 = sMin.substring(1,2);
	document.getElementById(this.name + '_h1').src = this.imageDir + "/" + h1 + sExt;
	document.getElementById(this.name + '_h2').src = this.imageDir + "/" + h2 + sExt;
	document.getElementById(this.name + '_m1').src = this.imageDir + "/" + m1 + sExt;
	document.getElementById(this.name + '_m2').src = this.imageDir + "/" + m2 + sExt;
	setTimeout(this.name + ".timer()", 60000);	// 60 seconds (not very accurate)
}