function Clock (Type)
{
	//alert(Style);
	//alert(Type);
	//parameters
	this.object = '';
	this.end 	= '';
	this.type 	= Type;
	this.style	= 'PlainText';
	
	//current time
	this.currentHour = 0;
	this.currentMinute = 0;
	this.currentSecond = 0;
	
	//display time
	this.dispH=0;
	this.dispM=0;
	this.dispS=0;
	
	//running variables
	this.elapsed = -1;
	this.running = false;	
	this.output = '';
	
	this.Run = function() {	
		//get the current time
		this.currentdate = new Date();
		this.currentHour = this.currentdate.getHours();
		this.currentMinute = this.currentdate.getMinutes();
		this.currentSecond = this.currentdate.getSeconds();
		
		switch(this.type)
		{
			case 'Countdown':
				if (this.end != '')
				{
					this.enddate = new Date(this.end);
					//this.enddate.setTime(this.end);
					
					this.elapsed = Math.ceil((this.enddate.getTime() - this.currentdate.getTime())/(1000));
					this.dispS = this.elapsed;
					this.dispM = 0;
					this.dispH = 0;
					while (this.dispS >= 60*60)
					{
						this.dispH += 1;
						this.dispS -= 60*60;
					}
					while (this.dispS >= 60)
					{
						this.dispM += 1;
						this.dispS -= 60;
					}
				}
				else
				{
					alert('You must set an end time for this clock style.');
					this.running = false;
				}
				break;
			case 'StopWatch':
				this.elapsed += 1;
				this.dispS = this.elapsed;
				this.dispM = 0;
				this.dispH = 0;
				while (this.dispS >= 60*60)
				{
					this.dispH += 1;
					this.dispS -= 60*60;
				}
				while (this.dispS >= 60)
				{
					this.dispM += 1;
					this.dispS -= 60;
				}
				break;
			case 'Clock':
				this.dispH = this.currentHour;
				this.dispM = this.currentMinute;
				this.dispS = this.currentSecond;
				break;
			default:
				this.dispH = this.currentHour;
				this.dispM = this.currentMinute;
				this.dispS = this.currentSecond;
				break;
		}
		
		this.Format(this.style);
		this.object.innerHTML = this.output;

		if (this.running)
		{
			var _self = this;
			setTimeout(function() { _self.Run(); },1000);
		}
	};
	
	this.Init = function(ID) {
		if(typeof(ID) !== 'undefined')	this.object = document.getElementById(ID);
		//current time
		this.currentHour = 0;
		this.currentMinute = 0;
		this.currentSecond = 0;
		
		//display time
		this.dispH=0;
		this.dispM=0;
		this.dispS=0;
		
		//running variables
		this.elapsed = -1;	
		this.output = '';
		
		if (!this.running)
			this.Run();
	};
	
	this.Start = function() {
		if(!this.running)
		{
			this.running = true;
			this.Run();
		}
	};

	this.Stop = function() {
		this.elapsed -= 1;
		this.running = false;
	};
	
	this.Reset = function() {
		this.Init(this.object.id);
	};
	
	this.getTime = function() {
		this.output = '';
		this.output += PadDigits(this.dispH, 2) + ':' + PadDigits(this.dispM, 2) + ':' + PadDigits(this.dispS, 2);
		return this.output;
	};
	
	this.setEndTime = function(time) {
		this.end = time;
	};
	
	this.setStyle = function(Style) {
		this.style = Style;
	};
	
	this.Format = function(Style) {
		if(typeof(Style) !== 'undefined')
		{
			this.style = Style;
		}
		
		this.output = '';
		
		switch(this.style)
		{
			case 'PlainText':
				this.output += PadDigits(this.dispH, 2) + ':' + PadDigits(this.dispM, 2) + ':' + PadDigits(this.dispS, 2);
				break;
			case 'Simple':
				this.output += "<div style='border:1px solid #AAA; background:#EEE; padding:1px 2px; text-align:center; width:75px;'>" + PadDigits(this.dispH, 2) + ':' + PadDigits(this.dispM, 2) + ':' + PadDigits(this.dispS, 2) + "</div>";
				break;
			case 'Bordered':
				this.output += "<div style='border:2px solid #000; padding:0px; background:#EEE; text-align:center; width:85px;'>" + 
								"<span style='border-right:1px solid #000; margin:0px; padding:1px 5px;'>" + PadDigits(this.dispH, 2) + '</span>' + 
								"<span style='border-right:1px solid #000;border-left:1px solid #000; margin:0px; padding:1px 5px;'>" + PadDigits(this.dispM, 2) + '</span>' + 
								"<span style='border-left:1px solid #000; margin:0px; padding:1px 5px;'>" + PadDigits(this.dispS, 2) + "</span></div>";
				break;
			case 'None':
				break;
			default:
				this.output += PadDigits(this.dispH, 2) + ':' + PadDigits(this.dispM, 2) + ':' + PadDigits(this.dispS, 2);
				break;
		}
	};
	
	function PadDigits (Number, Digits) {
		Number = Number.toString();
		var pad = '';
		
		if(Digits > Number.length)
			for(i = 0; i < (Digits - Number.length); i++)
				pad += '0';
		
		return pad + Number;
	}
}
