﻿var countDownClock = {
	// Properties
	cdDate: "3/12/2019 12:00",
	isLeadingZero: false,
	cdFormat: "{D} Days, {H} Hrs, {M} Min, {S} Sec....",
	cdDone: "",
	target: null,
	current: null,
	cdDiff: null,
	secondsLeft: null,
	// Methods
	getTimeValue: function (seconds, n1, n2) 
		{
			x = ((Math.floor(seconds/n1))%n2).toString();
			if (this.isLeadingZero && x.length < 2)
			{
				x = "0" + x;
			}
			return "<strong>" + x + "</strong>";
		},
	countOut: function ()
		{
			if (this.secondsLeft < 0) 
			{
				document.getElementById("countdown").innerHTML = this.cdDone;
				return;
			}
			
			var output = this.cdFormat;
			output = output.replace(/\{D\}/g, this.getTimeValue(this.secondsLeft, 86400, 100000));
			output = output.replace(/\{H\}/g, this.getTimeValue(this.secondsLeft, 3600, 24));
			output = output.replace(/\{M\}/g, this.getTimeValue(this.secondsLeft, 60, 60));
			output = output.replace(/\{S\}/g, this.getTimeValue(this.secondsLeft, 1, 60));

			document.getElementById("countdown").innerHTML = output;

			this.secondsLeft = this.secondsLeft-1;
		},
	start: function () 
		{
			this.target = new Date(this.cdDate);
			this.current = new Date();
			this.cdDiff = new Date(this.target-this.current);
			this.secondsLeft = Math.floor(this.cdDiff.valueOf()/1000);
			
			if (document.getElementById("countdown"))
			{
				// Reload the page after 10 minutes				
				setTimeout("window.location.reload();", 600000);
				setInterval("countDownClock.countOut(" + this.secondsLeft + ");", 990);
			}
		}
}