/*
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.
********************************************************
Slightly modified version from original
*/

var countdown = {
        compoStartDate: "12/14/2009 12:00 PM UTC+0",
        compoEndDate: "01/11/2010 11:59 AM UTC+0",
        displayFormat: "%%D%%:%%H%%:%%M%%:%%S%%",
        countActive: true,
        finishMessage: "00:00:00:00",
        countStepper: -1,
        leadingZero: true,
        setTimeOutPeriod: 1000,
       
    init: function() {
        this.countStepper = Math.ceil(this.countStepper);
        if (this.countStepper == 0) {
            this.countActive = false;
        }
        this.setTimeOutPeriod = (Math.abs(this.countStepper)-1)*1000 + 990;
        
        var seconds = countdown.countSecs(this.compoStartDate);
        if( seconds < 0 ) {
            seconds = countdown.countSecs(this.compoEndDate);           
            this.showCountdownTitle("after"); 
        } else {
            this.showCountdownTitle("before");
        }
        this.countBack(seconds);
    },
    countSecs: function(targetDate) {
        var dthen = new Date(targetDate);
        var dnow = new Date();
        if(this.countStepper>0) {
            ddiff = new Date(dnow-dthen);
        } else {
            ddiff = new Date(dthen-dnow);
        }      
        gsecs = Math.floor(ddiff.valueOf()/1000);
        return gsecs;
    },
   
    calcAge: function(secs, num1, num2) {
        s = ((Math.floor(secs/num1))%num2).toString();
        if (this.leadingZero && s.length < 2) {
            s = "0" + s;
        }
        return s;
    },   
    countBack: function(secs) {
        if (secs == 0) {
            secs = this.countSecs(this.compoEndDate);   
            this.hideCountdownTitle("before");
            this.showCountdownTitle("after");
        }  
        if (secs < 0) {
            document.getElementById("countdown").innerHTML = this.finishMessage;
            return;
        }
        var displayStr = this.displayFormat.replace(/%%D%%/g, this.calcAge(secs,86400,100000));
        displayStr = displayStr.replace(/%%H%%/g, this.calcAge(secs,3600,24));
        displayStr = displayStr.replace(/%%M%%/g, this.calcAge(secs,60,60));
        displayStr = displayStr.replace(/%%S%%/g, this.calcAge(secs,1,60));     
        document.getElementById("countdown").innerHTML = displayStr;
        
        if (this.countActive) {
            setTimeout("countdown.countBack(" + (secs+this.countStepper) + ")", this.setTimeOutPeriod);
        }
    },
    showCountdownTitle: function(titleId) {
        document.getElementById(titleId).style.display = "block";
    },
    hideCountdownTitle: function(titleId) {
        document.getElementById(titleId).style.display = "none";   
    }
}