﻿// JScript 文件
var time = {
    //商品剩余时间倒计时
    //obj: 页面标签id   time:剩余时间(秒)  overstr:结束后显示的信息
    CountDownSetTime : null,
    CountDownTime : function(obj,timeVal,overstr,callback){
        var overtime=parseInt(timeVal);
        jQuery("#"+obj).html(this.ConversionTime(overtime,overstr,callback));
        //if((60*60)<overtime)return;
        //时间结束，清除延时操作
        if(overtime<1){clearTimeout(this.CountDownSetTime);
            if(callback)callback.call(); 
        return;}
        overtime--;
        this.CountDownSetTime=setTimeout(function(){
            time.CountDownTime(obj,overtime,overstr,callback);
        },1000);
    },
    ConversionTime:function(timeVal,overstr,callback){
        var ss=0,mm=0,hh=0,dd=0;//秒，分钟，小时，天 
        var ddss=0,hhss=0;
        var str=(null!=overstr)?overstr:"时间结束"; 
        dd=parseInt(timeVal/(60*60*24));//天数 取整
        ddss=timeVal%(60*60*24) ;//取余，天数的余数（秒）
        hh=parseInt(ddss/(60*60));//小时，取整
        hhss=ddss%(60*60);//取余，小时的余数（秒）
        mm=parseInt(hhss/60);//分钟，取整
        ss=hhss%60;//最后取余 就是秒数
        
        //小时，分钟，秒  各补0
        hh=(9<hh)?hh:"0"+hh;
        mm=(9<mm)?mm:"0"+mm;
        ss=(9<ss)?ss:"0"+ss;
        
        if(0<dd){//大于1天
            str=dd+"天"+hh+"小时";
            if(null!=callback)
                str+=mm+"分钟"+ss+"秒";
        }else if(0<hh){//大于1小时
            str=hh+"小时"+mm+"分钟";
            if(null!=callback)
                str+=ss+"秒";
        }else if(0<ss||0<mm){ //1小时以内
            str=((mm>0)?mm+"分钟":"")+ss+"秒";
        }
        return str;
    }
}
