//var date_obj = ''; 显示倒计时的所有时间
//var year_obj = '';显示倒计时年的对象标签对象
//var month_obj = '';显示倒计时月的对象标签对象
//var day_obj = '';显示倒计时日的对象标签对象
//var hour_obj = '';显示倒计时小时的对象标签对象
//var min_obj = '';显示倒计时分钟的对象标签对象
//var second_obj = '';显示倒计时秒的对象标签对象
//var end_time_stamp = '';结束的时间戳
var server_time_url = '';
var time_count_down_handle = '';
var count_down_interval = 1000;
var get_new_data_interval = 60000;

function format_time(time, prefix, length)
{
    time = time.toString();
    var length_now = time.length;
    if(length_now < length)
    {
       for(var i = 0; i< length - length_now; i++)
       {
           time = prefix + time;
       }
    }
    return time;
}

function set_time(time, count_down_interval)
{
    time -= count_down_interval;
    time = Math.ceil(time/1000);
    if(time<0){
        time = 0;
    }
    var time_per_hour = 3600;
    var time_per_min = 60;
    var hour = (time-time%time_per_hour)/time_per_hour;
    if(hour>99)
    {
        hour = 99;
        min = 59;
        sec = 59
    }else{
        var tmp = time-hour*time_per_hour;
        var min = (tmp-tmp%time_per_min)/time_per_min;
        var sec = tmp - min * time_per_min;
    }
    hour = format_time(hour, '0', 2);
    min = format_time(min, '0', 2);
    sec = format_time(sec, '0', 2);

    
    typeof hour_obj == 'object' && hour_obj.html(hour);
    typeof hour_obj1 == 'object' && hour_obj1.html(hour.substring(0,1));
    typeof hour_obj2 == 'object' && hour_obj2.html(hour.substring(1,2));
    
    typeof min_obj == 'object' && min_obj.html(min);
    typeof min_obj1 == 'object' && min_obj1.html(min.substring(0,1));
    typeof min_obj2 == 'object' && min_obj2.html(min.substring(1,2));
    
    typeof second_obj == 'object' && second_obj.html(sec);
    typeof second_obj1 == 'object' && second_obj1.html(sec.substring(0,1));
    typeof second_obj2 == 'object' && second_obj2.html(sec.substring(1,2));
    if(time==0){
        clearTimeout(time_count_down_handle);
        return false;
    }
    time_count_down_handle = setTimeout('set_time(' + time * 1000 + ', ' + count_down_interval + ')', count_down_interval);
}

function start_count_down(end_time_stamp)
{
    $.ajax({
        url: server_time_url,
        type: 'GET',
        dataType: 'json',
        success: function(retdat, status) {
            if (retdat['status'] == 1 && retdat['code'] == 200) {
                var diff_time = end_time_stamp - retdat['content']['time'];
                clearTimeout(time_count_down_handle);
                time_count_down_handle = setTimeout('set_time('+diff_time+', '+ count_down_interval +')', count_down_interval);
            } else {
                showMessage('操作失败', '<font color="#990000">' + retdat['msg'] + '</font>');
            }
        },
        error: function () {
            show_product_obj.dialog("close");
            showMessage('请求错误', '<font color="#990000">请稍后重新尝试！</font>');
        }
    });
}
function start_count(end_time_stamp)
{
    start_count_down(end_time_stamp);
    setInterval('start_count_down('+end_time_stamp+')',get_new_data_interval);
}
$(document).ready(function(){
    if(typeof end_time_stamp != 'undefined')
    {
        if(typeof url_base != 'undefined')
        {
            server_time_url = url_base + 'site/get_server_time';
        }
        start_count(end_time_stamp);
    }
});

