/**
 * Created by huanggang on 16/10/17.
 */

var CountDowner = function()
{
    this.server_time = 0;
    this.start_time  = 0;
    this.delta_secs  = 0;

    this.init = function (server_time,start_time) {
        this.server_time = server_time;
        this.start_time = start_time;
        nowTime = new Date();
        cur_sec = parseInt(nowTime.getTime() / 1000);
        this.delta_secs = this.server_time - cur_sec;
    };
    
    this.run = function ()
    {
        d_secs = 24 * 60 * 60;
        h_secs = 60 * 60;
        m_secs = 60;

        nowTime = new Date();
        left_secs = this.start_time - (parseInt(nowTime.getTime() / 1000) + this.delta_secs);

        left_h = parseInt(left_secs / h_secs);
        left_m = parseInt((left_secs % h_secs) / m_secs);
        left_s = parseInt(left_secs % m_secs);

        this.add_zero(left_h,"hour");
        this.add_zero(left_m,"min");
        this.add_zero(left_s,"second");
    };
    this.isEnd = function ()
    {
        left_secs = this.start_time - (parseInt(nowTime.getTime() / 1000) + this.delta_secs);
        return (left_secs <= 0);
    };
    this.add_zero = function (time, id)
    {
        if(time<10){
            document.getElementById(id).innerHTML ="0"+time;
        }
        else {
            document.getElementById(id).innerHTML = time;
        }
    }
};

