
/**
 * Object class : timer
 * Author : Stuart Perkins
 */

function timer(sd,dir,bx) {
  if( arguments.length ) {
  	this.months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
  	this.init(sd,dir,bx);
	}
}

timer.prototype.init = function (sd,dir,bx) {
  this.startDate = sd;
  this.ImageFolder = dir;
  this.container = bx;
  this.container_title = 'bxt';
  this.output = '';
};

timer.prototype.run = function () {
  this.getNowDate();
  this.calculateDifference();
  this.calculateDisplay();
  this.display();
};

timer.prototype.getNowDate = function () {
	T = new Date();
	var y = T.getFullYear();
	var m = T.getMonth();
	var d = T.getDate();
	var h = T.getHours();
	var n = T.getMinutes();
	var s = T.getSeconds();
	this.currentDate = d +" " +this.months[m] +" " +y +" " +h +":" +n +":" +s;
};

timer.prototype.calculateDifference = function () {
	this.DateDifference = parseFloat((Date.parse(this.currentDate) - Date.parse(this.startDate))/1000);
	if( this.DateDifference < 0 ){
		this.DateDifference = this.DateDifference * -1;
		document.getElementById(this.container_title).innerHTML = 'D&Eacute;';
	}
};

timer.prototype.calculateDisplay = function () {
	var d = Math.floor(this.DateDifference /86400);
	var h = Math.floor((this.DateDifference - (d *86400 )) /3600);
	var m = Math.floor((this.DateDifference - (d *86400 ) - (h *3600 )) /60 );
	var s = Math.floor((this.DateDifference - (d *86400 ) - (h *3600 ) - (m *60)));
	this.buildOuput(d,h,m,s);
};

timer.prototype.buildOuput = function (D,H,M,S) {
	var FN = Array('d','h','m','s');
	var d = ''+D;
	var h = ''+H;
	var m = ''+M;
	var s = ''+S;
	var out = '';
	for ( var f = 0, P = ''; f < FN.length; f++) {
		eval('if(parseInt('+ FN[f] +') < 10) '+ FN[f] +' = "0" + '+ FN[f] +';');
		for ( var i = 0, j = 1; i < 2; i++, j++) {
			var N = (FN[f]).toUpperCase() +''+ j;
			eval('P = '+ FN[f] +'.substr('+ i +','+ j +');');
			eval('out += "<img id=\'CD'+ N +'\' src=\''+ this.ImageFolder +'/'+ P +'.gif\' alt=\'\'>";');
			if (j == 2 && f < FN.length - 1) eval('out += "<img src=\''+ this.ImageFolder +'/colon.gif\' alt=\'\'>";');
		}
	}
	this.output = out + "<span id='box_sub' style='display:block;'><img src='"+ this.ImageFolder +"/subs.gif' alt=''></span>";
};

timer.prototype.display = function () {
	document.getElementById(this.container).innerHTML = this.output;
}
