// JavaScript Document <<< Clock v0.1.0 Object >>>

function Clock(theme) {	
	//this.doSweepBack = false;
	//this.sound = null;

	this.clockface = new Image ();
	this.hourhand = new Image ();
	this.minhand = new Image ();
	this.sechand = new Image ();
	
	this.changeTheme(theme);
	
		
	var me = this;
	this.runCallback_ = function () {
		me.run_();
	};
}	

Clock.prototype.run_ = function () {
	var now = new Date();
	
	var secondsAngle = 
		(now.getSeconds() + 0) * 0.10471975511965977;
	var minutesAngle = 
		now.getMinutes() * 0.10471975511965977;
	var hoursAngle = 
		((now.getHours()%12) * 0.523598775598) + 
		(minutesAngle/6.283185481853 * 0.523598775598);

	var canvas = document.getElementById('clock');
	var context = canvas.getContext('2d');
  	
	
	// Draw clock face
	context.clearRect (0, 0, canvas.width, canvas.height);
	context.drawImage(this.clockface,0,0);
	
	context.save();
	context.translate (this.clockface.width / 2, this.clockface.height / 2);
	
	// Draw the hour hand
	context.save();
	context.rotate (hoursAngle);
	context.drawImage (
		this.hourhand, 
		-(this.hourhand.width / 2), 
		-(this.hourhand.height / 2)
	);
	context.restore();
	
	// Draw the hour minute
	context.save();
	context.rotate (minutesAngle);
	context.drawImage (
		this.minhand, 
		-(this.minhand.width / 2), 
		-(this.minhand.height / 2)
	);
	context.restore();
	
	// Draw the second hand
	context.save();
	context.rotate (secondsAngle);
	context.drawImage (
		this.sechand, 
		-(this.sechand.width / 2), 
		-(this.sechand.height / 2)
	);	
	context.restore();
	
	context.restore();
	
	setTimeout(this.runCallback_, 1000);
	
}

Clock.prototype.run = function () {
	
	var canvas = document.getElementById('clock');
	canvas.setAttribute("width",this.clockface.width);
	canvas.setAttribute("height",this.clockface.height);
		
	this.run_();
}

Clock.prototype.changeTheme = function (theme) {
	this.sechand.src = 'http://www.xart/global/clock/images/' + theme + '_s.png';
	this.minhand.src = 'http://www.xart/global/clock/images/' + theme + '_m.png';
	this.hourhand.src = 'http://www.xart/global/clock/images/' + theme + '_h.png';
	this.clockface.src = 'http://www.xart/global/clock/images/' + theme + '.png';
}