var BKGO = {
        countdown: function(date) {
                var id="bkgo_countdown_id";
                document.write("<span id=\""+id+"\"></span>");
                // sync time w/ server, only once
                // var request = new Request.JSON({url:'/api/servertime.php', onSuccess:function(x){
                //         var server_time = new Date(x);
                //         var time_delta = server_time-(new Date());
                //         BKGO.perform_countdown(id, (new Date(date)).getTime(), time_delta)}
                // }).get({});
                BKGO.perform_countdown(id, (new Date(date)).getTime(), 0);
        },
        perform_countdown: function(id, target_date, time_delta) {
                var today = new Date().getTime() + time_delta;
                if (today > target_date){
                        document.getElementById(id).innerHTML="已结束";
                        return;       
                }
                var ds = (target_date - today)/1000;
                var h = Math.floor(ds/3600);
                var m = Math.floor((ds - h*3600)/60);
                var s = Math.round(ds - h*3600 - m*60);
                // add a zero in front of numbers<10
                m = BKGO.checktime(m);
                s = BKGO.checktime(s);
                document.getElementById(id).innerHTML=h+"小时"+m+"分钟"+s+"秒";
                setTimeout(function(){ BKGO.perform_countdown(id, target_date, time_delta); }, 500);
        },
        checktime: function(i) {
                if (i<10) { i="0" + i; }
                return i;
        }
};

