﻿/**
* @file Analog clock object without using any JavaScript libraries.
* @author Raymond Chau
*/

// Create a default, global, namespace
if (!www)
  var www = {};

// Setup child namespaces, using objects
if (!www.ui)
  www.ui = {};

/**
* AnalogClock object constructor.
* @constructor
*/
www.ui.AnalogClock = function(id) {
	// note: no superclass constructor to call
	this.container = document.getElementById(id);
};

www.ui.AnalogClock.prototype = new www.ui.Clock();

// Override redraw method
www.ui.AnalogClock.prototype.redraw = function() {
	var container = this.container;
	
	if (container && container.getContext) {
		var ctx = container.getContext("2d");
		var date = this.date;
		var h = date.getHours() % 12 || 12;
		var m = date.getMinutes();
		var s = date.getSeconds();
		var drawHands = function(ctx, radius, angle, hand) {
			ctx.save();			
			ctx.translate(radius, radius);
			ctx.strokeStyle = '#fff';
			ctx.rotate(angle);
			ctx.beginPath();
			ctx.moveTo(0, 0);
			ctx.lineTo(0, hand);
			ctx.closePath();
			ctx.stroke();
			ctx.restore();
		}
		
		if (ctx) {
			// define clock radius and clock hands' length
			var radius = 40;
			var hHand = 20;
			var mHand = 30;
			var sHand = 37;			
			var hAngle = Math.PI * 2 * ((h + m / 60) / 12 + 0.5);
			var mAngle = Math.PI * 2 * ((m + s / 60) / 60 + 0.5);
			var sAngle = Math.PI * 2 * (s / 60 + 0.5);
						
			// draw clock outer circle
			ctx.beginPath();
			ctx.arc(radius, radius, radius, 0, Math.PI * 2, true);
			ctx.closePath();
			ctx.fill();
			
			// draw clock hands
			drawHands(ctx, radius, hAngle, hHand);
			drawHands(ctx, radius, mAngle, mHand);
			drawHands(ctx, radius, sAngle, sHand);
		}		
	} 
};
