/**
 * Copyright (c) 2006 eSolutions Group Inc.
 * All rights reserved.
 *
 * Author: Timothy Grant Vogelsang
 */
 
/**
 * Public default constructor.
 */
function JSCountDown()
{
	this.id = "JSCountDown_instance";
	this.parent = null;
	this.parentId = 0;
	this.serverDate = null;
	this.targetDate = null;
}

/**
 * This public function performs the JSCountDown registration.
 */
JSCountDown.prototype.SetJSCountDown = function()
{
	var key = "JSCountDown." + this.id;
	window[key] = this;
}

/**
 * This public function performs the JSCountDown retrieval.
 */
JSCountDown.GetJSCountDown = function(id)
{
	var key = "JSCountDown." + id;
	return window[key];
}

/**
 * This public function performs the JSCountDown initialization routine.
 */
JSCountDown.prototype.Init = function(parentId, year, month, day, hour, minute, second)
{
	this.parentId = parentId;
	this.serverDate = new Date(year, month, day, hour, minute, second, 00);
	this.targetDate = new Date(target_Year, target_Month - 1, target_Day, target_Hour, target_Minute, target_Second, 00);
	if (target_Active == "1")
	{
		this.Callback();  // callback
	}
}

/**
 * This public function performs the JSCountDown callback routine.
 */
JSCountDown.prototype.Callback = function()
{
	this.Render();  // render control
	this.AddSecond();  // add seconds
	
	setTimeout("JSCountDown.GetJSCountDown('" + this.id + "').Callback();", 1000);
}

/**
 * This public function performs the JSCountDown second increment routine.
 */
JSCountDown.prototype.AddSecond = function()
{
	var epoch = this.GetEpoch(this.serverDate) + 1;

	this.serverDate = this.GetDate(epoch);
}

/**
 * This public function performs the JSCountDown render routine.
 */
JSCountDown.prototype.Render = function()
{
	var timeRemain = Math.round((this.targetDate.getTime() - this.serverDate.getTime()) / 1000);
	
	if (timeRemain > 0)
	{
		var days = Math.floor(timeRemain / (60 * 60 * 24))
		timeRemain %= (60 * 60 * 24);
		var hours = Math.floor(timeRemain / (60 * 60));
		timeRemain %= (60 * 60);
		var mins = Math.floor(timeRemain / 60);
		timeRemain %= 60;
		var seconds = timeRemain;
		
		this.GetParent().innerHTML = days + ' days ' + hours + ' hours ' + mins + ' mins and ' + seconds + ' seconds remaining';
	}
	else
	{
		this.GetParent().innerHTML = target_End;
	}
}

/**
 * This public function converts a date into Epoch time.
 */
JSCountDown.prototype.GetEpoch = function(date)
{
	return (date.getTime() - date.getMilliseconds()) / 1000;
}

/**
 * This public function converts an Epoch time into a date.
 */
JSCountDown.prototype.GetDate = function(epoch)
{
	var date = new Date();
	
	if (epoch < 10000000000)
	{
		epoch = epoch * 1000;  // convert to milliseconds
	}
	
	date.setTime(epoch);
	return date;
}

/**
 * This public function gets the Parent object.
 */
JSCountDown.prototype.GetParent = function()
{
	if (this.parent == null)
	{
		this.parent = document.getElementById(this.parentId);	
	}
	return this.parent;
}

jsCountDown = new JSCountDown();
jsCountDown.SetJSCountDown();