/*
	Author:		Robert Hashemian (http://www.hashemian.com/)
	Modified by:	Munsifali Rashid (http://www.munit.co.uk/)
	Modified by:	Mike Audleman (http://www.wolfiesden.com/) 23 Sep 2010
*/


function countdown(obj) {
	// Object setup variables
	this.obj				= obj;
	this.Div				= this.obj + "Clock";
	this.BackColor			= "white";
	this.ForeColor			= "black";
	this.TargetDate			= "12/31/2020 5:00 AM";
	this.DisplayFormat		= "%%W%% Weeks, %%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
	this.CountActive		= true;
	this.FinishMessage		= null; 				// Anything but null will display the string
	this.CountStepper		= -1;					// count interval
	this.SetTimeOutPeriod	= (Math.abs(this.CountStepper)-1)*1000 + 990;
	this.LeadingZero		= true;					// true to add 0's to single digits, false to block leading zeros

	this.DisplayStr			= "";
	
	// Function Connections
	this.Calcage			= cd_Calcage;
	this.CountBack			= cd_CountBack;
	this.Setup				= cd_Setup;
	this.CountDone			= cd_CountDone;					// Function called when Count is done
	this.CounterThrobber	= cd_CounterThrobber;			// Function called with each throb of the counter
	this.CalcageInt			= cd_CalcageInt;				// Calculates the variable but returns numeric instead of string
}

// Dummy function to call on countdown done
function cd_CountDone() {
}

// Dummy function to call on each timer update
function cd_CounterThrobber(subDays,subHrs,subMinutes,subSecs,subWeeks) {
}

// Calculate the age as an integer
function cd_CalcageInt(secs, num1, num2) {
	s = ((Math.floor(secs/num1))%num2);
	return s;
}

function cd_Calcage(secs, num1, num2){
	s = Math.abs(((Math.floor(secs/num1))%num2)).toString();
	if (this.LeadingZero && s.length < 2)
		s = "0" + s;
		return (s);
}

function cd_CountBack() {
	// MKA - added local calc of actual time
		var dthen	= new Date(this.TargetDate);
		var dnow	= new Date();
		ddiff		= new Date(dthen-dnow);
		secs		= Math.floor(ddiff.valueOf()/1000);

	// MKA - Added sign inversion for count up timers
	if (this.CountStepper > 0) {
		secs = Math.abs(secs);
	}

	// MKA - Added end of counter done message and call to function
	if (secs < 0 && this.CountStepper <= 0) {
		if (this.FinishMessage != null) {
			document.getElementById(this.Div).innerHTML = this.FinishMessage;
			this.CountDone();
			return;
		}
	}

	this.DisplayStr = this.DisplayFormat;
	// MKA - Added Weeks variable to string
	if (this.DisplayStr.indexOf('%%W%%') >0){
		this.DisplayStr = this.DisplayStr.replace(/%%W%%/g,	this.Calcage(secs,604800,100000));
		this.DisplayStr = this.DisplayStr.replace(/%%D%%/g,	this.Calcage(secs,86400,7));
	} else {
		this.DisplayStr = this.DisplayStr.replace(/%%D%%/g,	this.Calcage(secs,86400,10000));
	}
	this.DisplayStr = this.DisplayStr.replace(/%%D%%/g,		this.Calcage(secs,86400,10000));
	this.DisplayStr = this.DisplayStr.replace(/%%H%%/g,		this.Calcage(secs,3600,24));
	this.DisplayStr = this.DisplayStr.replace(/%%M%%/g,		this.Calcage(secs,60,60));
	this.DisplayStr = this.DisplayStr.replace(/%%S%%/g,		this.Calcage(secs,1,60));
	
	document.getElementById(this.Div).innerHTML = this.DisplayStr;

	if (this.CountActive) {
		// MKA - Added throbber function and implimented CountStepper
		this.SetTimeOutPeriod = (Math.abs(this.CountStepper)-1)*1000 + 990;
		if (this.DisplayStr.indexOf('%%W%%') >0){
			this.CounterThrobber(this.CalcageInt(secs,86400,7),this.CalcageInt(secs,3600,24),this.CalcageInt(secs,60,60),this.CalcageInt(secs,1,60),this.Calcage(secs,604800,100000));
		} else {
			this.CounterThrobber(this.CalcageInt(secs,86400,100000),this.CalcageInt(secs,3600,24),this.CalcageInt(secs,60,60),this.CalcageInt(secs,1,60),null);
		}
		secs=(secs+this.CountStepper);
		setTimeout(this.obj +".CountBack(" + (secs) + ")", this.SetTimeOutPeriod);
	}
}

function cd_Setup() {
//	var dthen	= new Date(this.TargetDate);
//	var dnow	= new Date();
//	ddiff		= new Date(dthen-dnow);
//	gsecs		= Math.floor(ddiff.valueOf()/1000);
	this.CountBack();
}

