//
// echo "王 ";
//
/*
Author: Robert Hashemian
http://www.hashemian.com/

You can use this code in any manner so long as the author's
name, Web address and this disclaimer is kept intact.
********************************************************
Usage Sample:

<script language="JavaScript">
TargetDate = "12/31/2020 5:00 AM";
BackColor = "palegreen";
ForeColor = "navy";
CountActive = true;
CountStepper = -1;
LeadingZero = true;
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
FinishMessage = "It is finally here!";
</script>
<script language="JavaScript" src="http://scripts.hashemian.com/js/countdown.js"></script>
*
* targetDate: PHP passed date time
*/
function CountDownClass(countDownID, targetDate, tzoffset, finishMessage) {
	var objThis = this;
	//this.CountDownID;
	//this.TargetDate = "12/31/2020 5:00 AM";
	this.CountDownID = countDownID;
	this.TargetDate = targetDate;
	this.TimezoneOffset = tzoffset;
	//
	this.BackColor = "";
	this.ForeColor = "";
	this.CountActive = true;
	this.CountStepper = -1;
	this.LeadingZero = true;
	//this.DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
	this.DisplayFormat = "%%H%%:%%M%%:%%S%%";
	if(finishMessage)	{
	  this.FinishMessage = finishMessage;
  }
  else {
	  this.FinishMessage = "It is finally here!";
	}
	this.gsecs = 0;

	this.init = function() {
		var d, strUtc;
		d = new Date(this.TargetDate);
//
// visitor's localtime: getTimezoneOffset() is in min:
    strUtc = d.getTime() + (d.getTimezoneOffset() * 60000);
//
// deduce from server local time in France CET GMT+1,
// PHP Timezone offset in s:
//offsetServer = $tzoffset;
    d = new Date(strUtc + 1000*this.TimezoneOffset);
//TargetDate = \"$strTargetDateTime\";
    this.TargetDate = dtmFormatCountDownTarget(d);
  }
	
  this.calcage = function(secs, num1, num2) {
  	var s;
    s = ((Math.floor(secs/num1))%num2).toString();
    if (this.LeadingZero && s.length < 2)
      s = "0" + s;
    return "<b>" + s + "</b>";
  }

  this.CountBack = function() {
    if (this.gsecs < 0) {
      document.getElementById(this.CountDownID).innerHTML = this.FinishMessage;
      return;
    }
    var DisplayStr;
    DisplayStr = this.DisplayFormat.replace(/%%D%%/g, this.calcage(this.gsecs,86400,100000));
    DisplayStr = DisplayStr.replace(/%%H%%/g, this.calcage(this.gsecs,3600,24));
    DisplayStr = DisplayStr.replace(/%%M%%/g, this.calcage(this.gsecs,60,60));
    DisplayStr = DisplayStr.replace(/%%S%%/g, this.calcage(this.gsecs,1,60));

    document.getElementById(this.CountDownID).innerHTML = DisplayStr;
    if(this.CountActive) {
      var SetTimeOutPeriod = (Math.abs(this.CountStepper)-1)*1000 + 990;
      //setTimeout("CountBack(" + (secs+this.CountStepper) + ")", SetTimeOutPeriod);
      this.gsecs = this.gsecs + this.CountStepper;
      window.setTimeout(function () {objThis.CountBack();}, SetTimeOutPeriod);
    }
  }

  this.putspan = function(backcolor, forecolor) {
  	var strAttr = "";
  	if(backcolor)
  	{
  		strAttr += "background-color: " + backcolor + ";";
  	}
  	if(forecolor)
  	{
  		strAttr += "color: " + forecolor + ";";
  	}
  	if(strAttr)
  	{
  		strAttr = " style='" + strAttr + "'";
  	}
    document.write("<span id='" + this.CountDownID + "'"
      + strAttr + "></span>");
  }

  this.doCountDown = function() {
  	this.init();
    if (typeof(this.BackColor)=="undefined")
      this.BackColor = "white";
    if (typeof(this.ForeColor)=="undefined")
      this.ForeColor= "black";
    if (typeof(this.TargetDate)=="undefined")
      this.TargetDate = "12/31/2020 5:00 AM";
    if (typeof(this.DisplayFormat)=="undefined")
      this.DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
    if (typeof(this.CountActive)=="undefined")
      this.CountActive = true;
    if (typeof(this.FinishMessage)=="undefined")
      this.FinishMessage = "";
    if (typeof(this.CountStepper)!="number")
      this.CountStepper = -1;
    if (typeof(this.LeadingZero)=="undefined")
      this.LeadingZero = true;

    this.CountStepper = Math.ceil(this.CountStepper);
    if (this.CountStepper == 0)
      this.CountActive = false;
    //var SetTimeOutPeriod = (Math.abs(this.CountStepper)-1)*1000 + 990;
    this.putspan(this.BackColor, this.ForeColor);
    var dthen = new Date(this.TargetDate);
    var dnow = new Date();
    var ddiff;
    if(this.CountStepper>0) {
      ddiff = new Date(dnow-dthen);
    }
    else {
      ddiff = new Date(dthen-dnow);
    }
    this.gsecs = Math.floor(ddiff.valueOf()/1000);
    this.CountBack(); 
  }
//
  this.doCountDown();
//
//return this;
}
