var DigitalClock = Class.create(
{
	startByInit: true,
	formatTwelvehour: false,
	
	initialize: function(clockContainer, startByInit, formatTwelvehour)
	{
		if(typeof formatTwelvehour != 'undefined')
			this.formatTwelvehour = true;
			
		if(typeof startByInit == 'undefined')
		{
			this.updateClock(clockContainer);
		}
	},
	
	updateClock: function(clockContainer, timeoutObj)
	{
		this.today = new Date();
		this.hours = this.today.getUTCHours()+1;
		this.minutes = this.today.getUTCMinutes();
		this.seconds = this.today.getUTCSeconds();
		
		//this.serverTime = $(clockContainer).innerHTML;
		//this.serverTime.replace(/ AM/g,'').replace(/ PM/g,'');
		//this.today = this.serverTime.split(':');
		//this.hours = this.today[0];
		//this.minutes = this.today[1];
		//this.seconds = this.today[2];
		
		if(typeof timeoutObj != 'undefined')
			clearTimeout(timeoutObj);
			
		var self = this;
		var ampm = ((self.hours >= 12) ? " PM" : " AM");    

		if(this.formatTwelvehour == true)
			this.hours = ((self.hours == 0) ? "12" : (self.hours > 12) ? self.hours - 12 : self.hours);
			
		this.minutes = ((self.minutes < 10) ? "0" + self.minutes : self.minutes); 
		this.seconds = ((self.seconds < 10) ? "0" + self.seconds : self.seconds);
		this.currentTime = self.hours + ":" + self.minutes +':'+self.seconds+' '+ ampm;
		$(clockContainer).update(self.currentTime);
		
		var timer = setTimeout(function()
		{
			self.updateClock(clockContainer, timer);
		},1000);
	}
});
document.observe('dom:loaded', function()
{
	var PageClock = new DigitalClock('clockContainer');
});