

function DisplayZeroTime(time)
{
	if (time <= 9) return "0"+time;
	return time;
}

function display() {
    $( ".hodiny-cas" ).each(function() {
		// later record end time
		var spantime = parseInt($( this ).attr('time'));
		if(spantime > 0) {
		   // var endTime = new Date(spantime);
			var actualTime = new Date().getTime()/1000;
			// time difference in ms
			var timeDiff = spantime - actualTime;
			 if (timeDiff < 0)
			 {
				  $( this ).text("-" );
				  $("#add_to_cart_discount").remove();
				  return;
			 }
			// get seconds
			var seconds =  (Math.round(timeDiff % 60));
			if (seconds === 60){
				seconds = 0;
			}
			seconds =DisplayZeroTime(seconds);
		
			// remove seconds from the date
			timeDiff = Math.floor(timeDiff / 60);
		
			// get minutes
			var minutes = DisplayZeroTime(Math.round(timeDiff % 60));
		
			// remove minutes from the date
			timeDiff = DisplayZeroTime( Math.floor(timeDiff / 60));
		
			// get hours
			var hours =timeDiff ; // Math.round(timeDiff / 24);
			// remove hours from the date
			//timeDiff = Math.floor(timeDiff / 24);
			var days = timeDiff;
			//$("#discount_end").text(days + " days, " + hours + ":" + minutes + ":" + seconds);
			$( this ).text( hours + ":" + minutes + ":" + seconds);
		}
		else {
			$('#timecountdown').text('');
		}
    });
}

if(window.attachEvent) {
    window.attachEvent('onload', startDiscount);
} else {
    if(window.onload) {
        var curronload = window.onload;
        var newonload = function() {
            curronload();
           startDiscount();
        };
        window.onload = newonload;
    } else {
        window.onload = startDiscount;
    }
}


function startDiscount()
 {
	 if (document.readyState === "complete")
	 {
		setInterval(function(){display()}, 1000);
	 }
}