CountDown =
{
  nr : 0,
  configs : [],

  // Init method
  output : function(config)
  {
    // Resolve unique Id & cache configs
    var id = (this.nr++);
    config.Id = id;
    this.configs[id] = config;

    // Output
    config.spanId = 'cntdwn_'+id;
    this.write(config);

    // Countdown timeout
    config.CountStepper = Math.ceil(config.CountStepper);
    config.CountActive = (config.CountStepper == 0) ? false : true;
    config.SetTimeOutPeriod = (Math.abs(config.CountStepper)-1) * 1000 + 990;

    // Countdown secs
    var dthen = new Date(config.TargetDate);
    var dnow = new Date(config.CurrentDate);
    if(config.CountStepper>0)
      ddiff = new Date(dnow-dthen);
    else
      ddiff = new Date(dthen-dnow);
    config.Secs = Math.floor( ddiff.valueOf()/1000 );

    // Start
    this.CountBack(config);
  },

  write : function(config)
  {
    document.write("<span id='"+config.spanId+"' style='"+config.Style+"'></span>");
  },

  calcage : function(config, num1, num2)
  {
    s = ((Math.floor(config.Secs/num1))%num2).toString();
    if (config.LeadingZero && s.length < 2)
      s = "0" + s;
    return s;
    // return "<b>" + s + "</b>";
  },

  CountBack : function(config)
  {
    if (config.Secs < 0) 
    {
      document.getElementById( config.spanId ).innerHTML = config.FinishMessage;
      return;
    }
    var DisplayStr = config.DisplayFormat;
    DisplayStr = DisplayStr.replace(/%%D%%/g, this.calcage(config,86400,100000));
    DisplayStr = DisplayStr.replace(/%%H%%/g, this.calcage(config,3600,24));
    DisplayStr = DisplayStr.replace(/%%M%%/g, this.calcage(config,60,60));
    DisplayStr = DisplayStr.replace(/%%S%%/g, this.calcage(config,1,60));

    document.getElementById( config.spanId ).innerHTML = DisplayStr;
    if (config.CountActive)
    {
      config.Secs += config.CountStepper;
      setTimeout("CountDown.CountBack(CountDown.configs[" + config.Id + "])", config.SetTimeOutPeriod);
    }
  }
}