// JavaScript Document

/* C. global scope object */
var globalScope = new Array();

function Timer(endHour,endMin,endSec){
	this.endHour=endHour;
	this.endMin=endMin;
	this.endSec=endSec;
	//this.timer = setInterval( this.updateTimer, 999 );
	
	/* An extra unique identifier for the current object */
	this.uniqueId = 'a123';

	if( document.all ){/* make a reference to the current object and save in the global scope array to use later to correct the scope.*/
		globalScope[ this.uniqueId ] = this;
		setInterval( 'ieIntervalHandler("' + this.uniqueId + '","updateTimer")', 999 );
	}
	else{/* Mozilla */
		this.timer = setInterval ( function( that ) { that.updateTimer(); }, 999, this );
	}
	
	/* Special IE setInterval handler function with scope corrected */
	function ieIntervalHandler( id, strFunc ){/* correct the scope then make the call */
		var scope = globalScope[id];
		eval( "scope." + strFunc + "()" );
	}
}

Timer.prototype.updateTimer = function()
{
  var currentTime = new Date ();

  var currentHours = currentTime.getHours ( );
  var currentMins = currentTime.getMinutes ( );
  var currentSecs = currentTime.getSeconds ( );
  
  var hoursLeft = ( ((this.endMin!==00)&&(this.endMin<currentMins)) ? this.endHour-currentHours-1 : this.endHour-currentHours);
  //this.endHour-currentHours;
  
  //if(currentMins > minsLeft){ minsLeft = 60+this.endMin-currentMins ;
  //}else{ minsLeft = this.endMin-currentMins; }
  var minsLeft = ( currentMins>this.endMin ? this.endMin-currentMins+59 : this.endMin-currentMins);
  minsLeft = ( minsLeft < 10 ? "0" : "" ) + minsLeft;
  //this.minsLeft = this.endMin-currentMins;
  
  var secsLeft = this.endSec-currentSecs;
  // Pad the minutes and seconds with leading zeros, if required
  //this.minsLeft = ( this.minsLeft < 10 ? "0" : "" ) + this.minsLeft;
  secsLeft = ( secsLeft < 10 ? "0" : "" ) + secsLeft;

  // Compose the string for display
  this.timeLeft = hoursLeft + ":" + minsLeft + ":" + secsLeft;

  // Update the time display
  document.getElementById("timer").firstChild.nodeValue = this.timeLeft;
}

