/*

Original script developed by Robert Hashemian of http://www.hashemian.com/
Adapted by Ben Major

********************************************************

*/

function calcage(secs, num1, num2) 
{
  s = ( (Math.floor(secs / num1) ) % num2).toString();
  if (leadingZero && s.length < 2)
    s = "0" + s;
  return "<b>" + s + "</b>";
}

function CountBack(secs) 
{
  if (secs < 0) 
  {
    document.getElementById("cntdwn").innerHTML = finishMessage;
    return;
  }
  
  DisplayStr = displayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
  DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
  DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
  DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));
  
	// If the event is more than 7 days, only show days and hours:
	if(calcage(secs, 86400, 100000) < 7)
	  	displayFormat = "%%D%% Days, %%H%% Hours.";
	else
  		displayFormat = "There are only<br />%%D%% days, %%H%% hours, %%M%% minutes, %%S%% seconds<br />left until the 2010 event!";

  document.getElementById("cntdwn").innerHTML = DisplayStr;
  if (countActive)
    setTimeout("CountBack(" + (secs+countStepper) + ")", SetTimeOutPeriod);
}

function putspan(backcolor, forecolor) 
{
 	document.write("<div id='cntdwn'></div>");
}

if (typeof(backColor) == "undefined")
  backColor = "white";
if (typeof(foreColor) == "undefined")
  foreColor= "black";
if (typeof(targetDate) == "undefined")
  targetDate = "12/31/2020 5:00 AM";
if (typeof(displayFormat) == "undefined")
  displayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
if (typeof(countActive) == "undefined")
  countActive = true;
if (typeof(finishMessage) == "undefined")
  finishMessage = "";
if (typeof(countStepper) != "number")
  countStepper = -1;
if (typeof(leadingZero) == "undefined")
  leadingZero = true;


countStepper = Math.ceil(countStepper);
if (countStepper == 0)
  countActive = false;
  
var SetTimeOutPeriod = (Math.abs(countStepper)-1)*1000 + 990;
putspan(backColor, foreColor);
var dthen = new Date(targetDate);
var dnow = new Date();

if(countStepper > 0)
  ddiff = new Date(dnow-dthen);
else
  ddiff = new Date(dthen-dnow);
  
gsecs = Math.floor(ddiff.valueOf()/1000);
CountBack(gsecs);
