/* Written by Rafael Robayna */

var canvasClock;
var contextClock;

var digiClock;

function DigiNum(context, fontSize) {
	this.context = context;
	this.fontSize = fontSize;
	this.stubSize = fontSize/3;

	//Stub Position Definition
	this.pos = [{x:0, y:0, vert:true}, 
		{x:0, y:0, vert:false}, 
		{x:1*(this.stubSize*1.5), y:0, vert:true}, 
		{x:1*(this.stubSize*1.5), y:1*(this.stubSize*1.5), vert:true}, 
		{x:0, y:2*(this.stubSize*1.5), vert:false}, 
		{x:0, y:1*(this.stubSize*1.5), vert:true}, 
		{x:0, y:1*(this.stubSize*1.5), vert:false}];
	
	/*
		Number Definitions - each of the array points represents a segment or 
		character stub starting on the upper right
		ex. 
		this.num[0] == 0
							-
		this.num[0][0] == *   |
						  |   |
						    -
	*/
	this.num = new Array();
	this.num[0] = [true, true, true, true, true, true, false];
	this.num[1] = [false, false, true, true, false, false, false];
	this.num[2] = [false, true, true, false, true, true, true];
	this.num[3] = [false, true, true, true, true, false, true];
	this.num[4] = [true, false, true, true, false, false, true];
	this.num[5] = [true, true, false, true, true, false, true];
	this.num[6] = [true, true, false, true, true, true, true];
	this.num[7] = [false, true, true, true, false, false, false];
	this.num[8] = [true, true, true, true, true, true, true];
	this.num[9] = [true, true, true, true, true, false, true];

	this.drawCharStub = function(position, vert) {
		var quarter = this.stubSize/4;
		var xMod = [quarter, quarter, 0, -quarter, -quarter];
		var yMod = [quarter, quarter+this.stubSize, (this.stubSize/2)+this.stubSize, quarter+this.stubSize, quarter];
	
		if(!vert) {
			var hold = eval(xMod.toSource());
			xMod = eval(yMod.toSource());
			yMod = eval(hold.toSource());
		}
		
		this.context.moveTo(position.x, position.y);
		for(c = 0; c < xMod.length; c++) {
			this.context.lineTo(Math.abs(position.x+xMod[c]), Math.abs(position.y+yMod[c]));
		}
		this.context.fill();
	}

	this.drawNumber = function(number, position) {
		for(i = 0; i < this.pos.length; i ++) {
			if(this.num[number][i]) 
				this.drawCharStub({x:this.pos[i].x+position.x, 
					y:this.pos[i].y+position.y}, this.pos[i].vert);
		}
	}
	
	this.drawDigitString = function(digits, position) {
		for(n = 0; n < digits.length; n++) {
			if(digits.charAt(n) != " " && !isNaN(digits.charAt(n))) {
				var paintInt = parseInt(digits.charAt(n));
				this.drawNumber(paintInt, position);
				
			} 
			position.x += this.stubSize*3;
		}
		return position;
	}
}

function DigiClock(context, clockPos, fontSize) {
	this.context = context;
	this.digiNum = new DigiNum(context, fontSize);
	this.clockPos = clockPos;
	this.numPos;
	this.clockSize = {x:fontSize*8, y:fontSize*1.5};
	
	this.drawTime = function() {
		this.numPos = eval(this.clockPos.toSource());
		this.context.clearRect(clockPos.x - 10,clockPos.y - 10,this.clockSize.x,this.clockSize.y);

		var seconds = new Date().getSeconds();
		seconds = (seconds > 9)?seconds:"0"+seconds;
		var minutes = new Date().getMinutes();
		minutes = (minutes > 9)?minutes:"0"+minutes;
		var hours = new Date().getHours();
		hours = (hours > 9)?(hours>12?hours-12:hours):(hours==0?12:hours);

		this.numPos = this.digiNum.drawDigitString(hours+"", this.numPos);
		this.drawDots();
		this.numPos = this.digiNum.drawDigitString(minutes+"", this.numPos);
		this.drawDots();
		this.numPos = this.digiNum.drawDigitString(seconds+"", this.numPos);
	}

	this.drawDots = function() {
		this.context.fillRect(this.numPos.x, this.numPos.y + this.digiNum.stubSize/2, this.digiNum.stubSize/2, this.digiNum.stubSize/2);
		this.context.fillRect(this.numPos.x, this.numPos.y + this.digiNum.stubSize*2, this.digiNum.stubSize/2, this.digiNum.stubSize/2);
		this.numPos.x += this.digiNum.stubSize*1.5;
	}
}

function init() {
	canvasClock = document.getElementById("digiClock");
	
	if (canvasClock.getContext){
		contextClock = canvasClock.getContext("2d");

		//clock
		var clockPos = {x:20, y:20};
		var fontSize = 42;
		digiClock = new DigiClock(contextClock, clockPos, fontSize);
		drawCurrentTime();

		//number draw tests
		var digiNum = new DigiNum(contextClock, 12);
		digiNum.drawDigitString("0123456789", {x:20, y:140});
		digiNum = new DigiNum(contextClock, 20);
		digiNum.drawDigitString("0123456789 NOT Recognized", {x:20, y:160});
		digiNum = new DigiNum(contextClock, 30);
		digiNum.drawDigitString("0123456789", {x:20, y:190});
	}
}

function drawCurrentTime() {
	digiClock.drawTime();
	if (window.focus) {
		window.setTimeout("drawCurrentTime()", 1000);
	}
}