﻿var showCounter = true;
var TargetDate;
var CountActive;
var CountStepper;
var LeadingZero;

//TargetDate = "01/24/2009 12:00 AM";
//TargetDate = "01/24/2009 00:00";
TargetDate = "11/30/2009 23:59";	 
CountActive = true;
CountStepper = -1;
LeadingZero = true;

function Calculate(secs, num1, num2) {
  s = ((Math.floor(secs/num1))%num2).toString();
  if (LeadingZero && s.length < 2)
    s = "0" + s;    
  return s;
}

var count = 1;

function CountBack(secs) {

    if((count%3) == 0){
        alert(secs);
        count++;
    }        
    DisplayTime(Calculate(secs,86400,100000), "days");
    DisplayTime(Calculate(secs,3600,24), "hours");
    DisplayTime(Calculate(secs,60,60), "minutes");
    DisplayTime(Calculate(secs,1,60), "seconds");
  
    if (CountActive)
        setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
}

function DisplayTime(time, type)
{

    var first = ("" + time).substr(0, 1);
    var second = ("" + time).substr(1, 1);
    var third;

    if(time > 99)
    {
        third = ("" + time).substr(2,1);
    }
           
    var span1 = document.getElementById(type + "_one");
    var span2 = document.getElementById(type + "_two");
    var span3 = document.getElementById(type + "_three");
    
    span1.className = "countdown_" + type;
    span2.className = "countdown_" + type;
    
    if(time > 99)
        span3.className = "countdown_" + type;
        
    var top = "";
    switch(type)
    {
        case "days":
            top = "0px";
            break;
        case "hours":
            top = "-30px";
            break;
        case "minutes":
            top = "-60px";
            break;
        case "seconds":
            top = "-91px";
            break;
    }
    
    var position = GetNumberPosition(first);
    span1.style.backgroundPosition = position + "px " + top;
    
    position = GetNumberPosition(second);
    span2.style.backgroundPosition = position + "px " + top;
    
    if(time > 99)
    {
        position = GetNumberPosition(third);
        span3.style.backgroundPosition = position + "px " + top;
    }
    
    if(first == "1")
        span1.style.width = "15px";
    else
        span1.style.width = "20px";
    
    if(second == "1")
        span2.style.width = "15px";
    else
        span2.style.width = "20px";
    
    if(third == "1" && type == "days")
        span3.style.width = "15px";
}

function GetNumberPosition(number)
{
    switch(number)
    {
        case "0":
            return 22;
        case "1": 
            return -2;
        case "2": 
            return 235;
        case "3": 
            return 209;
        case "4": 
            return 181;
        case "5": 
            return 156;
        case "6": 
            return 129;
        case "7": 
            return 103;
        case "8": 
            return 77;
        case "9": 
            return 50;
    }
}

function putspan() {
    
    document.write("<div id='time'>");
    document.write("<span class='spacer'>");
    document.write("<span id='days_one'></span>");
    document.write("<span id='days_two'></span>");
    document.write("<span id='days_three'></span>");
    document.write("<br clear='all' />");
    document.write("<img src='/images/days.gif' alt='' style='margin-top: 8px;' />");
    document.write("</span>");
    
    document.write("<span class='spacer'>");
    document.write("<span id='hours_one'></span>");
    document.write("<span id='hours_two'></span>");
    document.write("<br clear='all' />");
    document.write("<img src='/images/hours.gif' alt='' style='margin-top: 8px;' />");
    document.write("</span>");
    
    document.write("<span class='spacer'>");
    document.write("<span id='minutes_one'></span>");
    document.write("<span id='minutes_two'></span>");
    document.write("<br clear='all' />");
    document.write("<img src='/images/minutes.gif' alt='' style='margin-top: 8px;' />");
    document.write("</span>");
    
    document.write("<span class='spacer'>");
    document.write("<span id='seconds_one'></span>");
    document.write("<span id='seconds_two'></span>");
    document.write("<br clear='all' />");
    document.write("<img src='/images/seconds.gif' alt='' style='margin-top: 8px;' />"); 
    document.write("</span>");
    document.write("</div>");
}

CountStepper = Math.ceil(CountStepper);

if (CountStepper == 0)
  CountActive = false;
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;

putspan();

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);

if(gsecs<=0){ showCounter = false; }
else{ CountBack(gsecs); }
