﻿var CountDownTimer = new Class({
	options : {
		month : '*',     //  '*' for next month, '0' for this month or 1 through 12 for the month 
		day : '1',       //  Offset for day of month day or + day  
		hour : 0,        //  0 through 23 for the hours of the day
		tz : -5        //  Offset for your timezone in hours from UTC	
	},
	
	initialize : function(options)
	{
		this.setOptions(options);

		this.timer = this.displayCountDown.periodical(1000,this);
	},

	setCountDown : function() 
	{
		var toDate = new Date();
		var fromDate = new Date();
		var diffDate = new Date(0);
		if (this.options.month == '*')
			toDate.setMonth(toDate.getMonth() + 1);
		else if (this.options.month > 0) 
		{ 
			if (this.options.month <= toDate.getMonth())
				toDate.setYear(toDate.getYear() + 1);
			toDate.setMonth(this.options.month - 1);
		}
		if (this.options.day.substr(0,1) == '+') 
		{
			var day1 = parseInt(this.options.day.substr(1));
			toDate.setDate(toDate.getDate() + day1);
		} 
		else
		{
			toDate.setDate(this.options.day);
		}
		toDate.setHours(this.options.hour);
		toDate.setMinutes(0-(this.options.tz * 60));
		toDate.setSeconds(0);
		fromDate.setMinutes(fromDate.getMinutes() + fromDate.getTimezoneOffset());
		diffDate.setMilliseconds(toDate - fromDate);
		return Math.floor(diffDate.valueOf() / 1000);
	},
	
	displayCountDown : function() 
	{
		var countdown = this.setCountDown();
		
		if (countdown < 0) 
		{
			$('cdtDays').set('html','000');
			$('cdtHours').set('html','00');
			$('cdtMinutes').set('html','00');
			$('cdtSeconds').set('html','00');
		}
		else {
			var secs = countdown % 60; 
			if (secs < 10) secs = '0' + secs;
				var countdown1 = (countdown - secs) / 60;
			var mins = countdown1 % 60; 
			if (mins < 10) mins = '0' + mins;
				countdown1 = (countdown1 - mins) / 60;
			var hours = countdown1 % 24;
			if (hours <10) hours = '0' + hours;
			var days = (countdown1 - hours) / 24;
			
			$('cdtDays').set('html',days);
			$('cdtHours').set('html',hours);
			$('cdtMinutes').set('html',mins);
			$('cdtSeconds').set('html',secs);
		}
	}

});

CountDownTimer.implement(new Options);

