/**
 * (c) 2006 k3o Tomek Grzechowski
 * author: Maciej [mco] Świętochowski
 */

function countdownTimer() {
	
	//properties
	this.seconds = 0;
	this.elementId = NaN;
	this.onZero = NaN;
	
	//methods
	this.initialize = Initialize;
	this.start = Start;
	this.getHour = GetHour;
	this.getMinute = GetMinute;
	this.getSecond = GetSecond;
	this.runOnZeroEvent = RunOnZeroEvent;
	
	function Initialize( seconds, elementId ) {
		
		this.seconds = seconds + 1;
		this.elementId = elementId;
	}
	
	function Start( timerName ) {
		
		this.seconds -= 1;
		
		if( this.seconds > 0 ) {
			
			document.getElementById( this.elementId ).innerHTML =
				this.getHour( ) + ':' +
				this.getMinute( ) + ':' +
				this.getSecond( );
			
			setTimeout( timerName + ".start( '" + timerName + "' )", 1000 );
			
		} else {
			
			this.runOnZeroEvent( );
			document.getElementById( this.elementId ).innerHTML = '00:00:00';
		}
	}
	
	function GetHour( ) {
		
		var retval = Math.floor( this.seconds/3600 );
		
		if( retval < 10 ) {
			return '0' + retval;
		} else {
			return retval;
		}
	}
	
	function GetMinute( ) {
		
		var retval = Math.floor( this.seconds/60 )%60;
		
		if( retval < 10 ) {
			return '0' + retval;
		} else {
			return retval;
		}
	}
	
	function GetSecond( ) {
		
		var retval = this.seconds%60;
		
		if( retval < 10 ) {
			return '0' + retval;
		} else {
			return retval;
		}
	}
	
	function RunOnZeroEvent( ) {
		
		if( ( typeof this.onZero ) == 'function' ) {
			
			try {
				
				this.onZero( );
				
			} catch( e ) {
				
				alert( 'merito countdownTimer caught an exception: ' + e );
				
			}
		}
	}
}

