$(function(){
	jQuery(".count_down").each(function(){
      var el = jQuery(this);
      if(!el.attr('endtime')) 
         return;
	  if(!el.attr('starttime')) return;	
	    //time start depend on user define -- bearfly001    
 		defineTime = new Date(el.attr('starttime'));  
 		endTime = new Date(el.attr('endtime'));
         var startTime;         	
            try {
                startTime = new Date(serHs);
            }catch (e) {
                startTime = new Date();
            }            
        //
        if(startTime<defineTime) {
        	el.hide();
        	return;	
        } 
        if(startTime>endTime){
        	el.hide();
        	return;	
        }     	     
            var timer;
            var now = (new Date(el.attr('endtime')) - startTime);            
            var displayDay, displayHour, displayMin, displaySec;
            var l_d = 2, l_h = 2, l_m = 2, l_s = 2;            
            el.find('td').each(function(){
                var td = jQuery(this);
                if (td.hasClass('d')) {
                    displayDay = td;
                    l_d = td.html().length;
                }
                if (td.hasClass('h')) {
                    displayHour = td;
                    l_h = td.html().length;
                }
                if (td.hasClass('m')) {
                    displayMin = td;
                    l_m = td.html().length;
                }
                if (td.hasClass('s')) {
                    displaySec = td;
                    l_s = td.html().length;
                }
            })
            if (now <= 0) {
            	return;
            }
            var tick = function(){
                now -= 1000;                
                if (now.valueOf() < 0) {
                    window.clearInterval(timer);
                    return;
                };
                //update the values
                var seconds = now.valueOf() / 1000;
                var day = (Math.floor(seconds / 86400)) % 86400;
                var hrs = (Math.floor(seconds / 3600)) % 24;
                var min = (Math.floor(seconds / 60)) % 60;
                var sec = (Math.floor(seconds / 1)) % 60;
                
                if (l_d == 2) 
                    day = (day + "").length < 2 ? "0" + day : day;
                if (l_h == 2) 
                    hrs = (hrs + "").length < 2 ? "0" + hrs : hrs;
                if (l_m == 2) 
                    min = (min + "").length < 2 ? "0" + min : min;
                if (l_s == 2) 
                    sec = (sec + "").length < 2 ? "0" + sec : sec;
                
                if (displayDay) 
                    displayDay.html(day);
                if (displayHour) 
                    displayHour.html(hrs);
                if (displayMin) 
                    displayMin.html(min);
                if (displaySec) 
                    displaySec.html(sec);
                
                
                if (parseInt(day) > 1) {
                    el.find('.day_2').show();
                }else {
                	el.find('.day_2').hide();
                    el.find('.day_1').show();
                }
            }
            tick();
            timer = window.setInterval(tick, 1000);
            el.parent().show();
	});
});
