Framework.CountdownCounter = Class.create({
	initialize: function(timestamp, element, unit) 
	{
		this.date = new Date((timestamp * 1000));
		this.timestamp = this.date.getTime();
		
		this.element = element;
		
		switch (unit)
		{
			case 'seconds':
				this.units = 1;
				break;
			case 'minutes':
				this.units = 2;
				break;
			case 'hours':
				this.units = 3;
				break;
			case 'days':
				this.units = 4;
				break;
			default:
				this.units = 4;
				break;
		}
		
		this.executer = new PeriodicalExecuter(this.update.bind(this), 1);
	},

	update: function(event)
	{
		var now = new Date();
		var difference = Math.floor((this.timestamp - now.getTime()) / 1000);
		
		if (difference > 0)
		{
			var seconds = difference;
			if (this.units == 1)
			{
				var secondsDiff = seconds;
			}
			else if (this.units > 1)
			{
				var secondsDiff = seconds % 60;
			}
			if (this.units >= 1 && this.secondsDiff != secondsDiff)
			{
				this.secondsDiff = secondsDiff;
				this.element.fire(':update_seconds', "" + this.secondsDiff);
			}
			
			var minutes = Math.floor(seconds / 60);
			if (this.units == 2)
			{
				var minutesDiff = minutes;
			}
			else if (this.units > 2)
			{
				var minutesDiff = minutes % 60;
			}
			if (this.units >= 2 && this.minutesDiff != minutesDiff)
			{
				this.minutesDiff = minutesDiff;
				this.element.fire(':update_minutes', "" + this.minutesDiff);
			}
			
			var hours = Math.floor(minutes / 60);
			if (this.units == 3)
			{
				var hoursDiff = hours;
			}
			else if (this.units > 3)
			{
				var hoursDiff = hours % 24;
			}
			if (this.units >= 3 && this.hoursDiff != hoursDiff)
			{
				this.hoursDiff = hoursDiff;
				this.element.fire(':update_hours', "" + this.hoursDiff);
			}
						
			if (this.units == 4)
			{
				var daysDiff = Math.floor(hours / 24);
			}
			if (this.units >= 4 && this.daysDiff != daysDiff)
			{
				this.daysDiff = daysDiff;
				this.element.fire(':update_days', "" + this.daysDiff);
			}
		}
		else
		{
			this.executer.stop();
			this.element.fire(':ZERO', "0");
		}
	}
});

Framework.LegacyCountDownCounter = Class.create({
	initialize: function(timestamp, element) 
	{
		this.element = element;
		
		this.makeDOM();
		
		this.element.on(':update_seconds', this.updateSeconds.bind(this));
		this.element.on(':update_minutes', this.updateMinutes.bind(this));
		this.element.on(':update_hours', this.updateHours.bind(this));
		this.element.on(':update_days', this.updateDays.bind(this));
		
		this.element.on(':ZERO', this.counterEnd.bind(this));
		
		this.counter = new Framework.CountdownCounter(timestamp, this.element, 'hours');
	},
	
	makeDOM: function()
	{
		this.seconds = this.element.down('.seconds_diff');
		this.minutes = this.element.down('.minutes_diff');
		this.hours = this.element.down('.hours_diff');
		this.days = this.element.down('.days_diff');
	},
	
	updateSeconds: function(event)
	{
		this.seconds.update(this.getValue(event));
		this.element.show();
	},
	
	updateMinutes: function(event)
	{
		this.minutes.update(this.getValue(event));
		this.element.show();
	},
	
	updateHours: function(event)
	{
		this.hours.update(this.getValue(event));
		this.element.show();
	},
	
	updateDays: function(event)
	{
		this.hours.update(this.getValue(event));
		this.element.show();
	},
	
	counterEnd: function(event)
	{
		this.updateSeconds(event);
		this.updateMinutes(event);
		this.updateHours(event);
		this.updateDays(event);
	},
	
	getValue: function(event)
	{
		var value = event.memo;
		value = (value < 10) ? "0" + value: value;
		return value;
	}
});

Framework.BasicHourlyCountdownCounter = Class.create({
	initialize: function(timestamp, element) 
	{
		console.log(timestamp);
		console.log(element);
		
		this.element = element;
		
		this.makeDOM();
		
		this.element.on(':update_seconds', this.updateSeconds.bind(this));
		this.element.on(':update_minutes', this.updateMinutes.bind(this));
		this.element.on(':update_hours', this.updateHours.bind(this));
		
		this.element.on(':ZERO', this.counterEnd.bind(this));
		
		this.counter = new Framework.CountdownCounter(timestamp, this.element, 'hours');
	},
	
	makeDOM: function()
	{
		this.element.hide();
		
		this.element.insert({bottom: new Element('span',{id: 'hours'})});
		this.element.insert(":");
		this.element.insert({bottom: new Element('span',{id: 'minutes'})});
		this.element.insert(":");
		this.element.insert({bottom: new Element('span',{id: 'seconds'})});
		
		this.seconds = $('seconds');
		this.minutes = $('minutes');
		this.hours = $('hours');
	},
	
	updateSeconds: function(event)
	{
		this.seconds.update(this.getValue(event));
		this.element.show();
	},
	
	updateMinutes: function(event)
	{
		this.minutes.update(this.getValue(event));
		this.element.show();
	},
	
	updateHours: function(event)
	{
		this.hours.update(this.getValue(event));
		this.element.show();
	},
	
	counterEnd: function(event)
	{
		this.updateSeconds(event);
		this.updateMinutes(event);
		this.updateHours(event);
	},
	
	getValue: function(event)
	{
		var value = event.memo;
		value = (value < 10) ? "0" + value: value;
		return value;
	}
	
});
