/*
	Nine Circles Numbers
	@author Robert O'Rourke
	@author_url http://www.sanchothefat.com/
 */
 
$(".nojs").removeClass("nojs");
 

var colours = [
[255,0,0],
[80,80,255],
[255,255,30],
[0,210,0],
[255,30,255],
[30,255,255]
];

function ncn(en,canvas){
n = parseInt(en);
var width = canvas.width;
var height = width;
var radius = (width/6)-2;
var ctx = canvas.getContext('2d');
var current = 0;
var bg = 255;
var fg = 0;
var coords = [
[width/6,height/6],
[width/2,height/6],
[width-(width/6),height/6],
[width/6,height/2],
[width/2,height/2],
[width-(width/6),height/2],
[width/6,height-(height/6)],
[width/2,height-(height/6)],
[width-(width/6),height-(height/6)]
];
var mathPI2 = Math.PI*2;

var grid = [
"0",
"5",
"4,6",
"4,5,6",
"1,3,7,9",
"1,3,5,7,9",
"1,2,3,7,8,9",
"1,2,3,5,7,8,9",
"1,2,3,4,6,7,8,9",
"1,2,3,4,5,6,7,8,9"
];

// positive and negative numbers
if ( n < 0 ) {
bg = 0;
fg = 255;
} else {
bg = 255;
fg = 0;
}

function addColour(i){
	colours[i] = [
		Math.floor(Math.random()*256),
		Math.floor(Math.random()*256),
		Math.floor(Math.random()*256)
	];
}

function circle(num,current,xy,radius,ci) {
ctx.lineWidth = 3;
ctx.strokeStyle = 'rgb('+fg+','+fg+','+fg+')';
if ( typeof(colours[ci]) == "undefined" ) addColour(ci);
if ( grid[current].match(num) ) {
	ctx.fillStyle = 'rgb('+colours[ci][0]+','+colours[ci][1]+','+colours[ci][2]+')';
} else {
	ctx.fillStyle = 'rgb('+bg+','+bg+','+bg+')';
}
ctx.beginPath();
ctx.arc((xy[0]),(xy[1]),radius,0,mathPI2,true);
ctx.stroke();
ctx.fill();
ctx.closePath();
}

var iString = en.toString().split("");
if ( n < 0 ) {
iString.shift();
}
var sLength = iString.length;
var ci = sLength-1;
var r = radius;

for ( c=0; c < sLength; c++ ) {
current = parseInt(iString.shift());
ci = (sLength-c-1);
circle(1,current,coords[0],r,ci);
circle(2,current,coords[1],r,ci);
circle(3,current,coords[2],r,ci);
circle(4,current,coords[3],r,ci);
circle(5,current,coords[4],r,ci);
circle(6,current,coords[5],r,ci);
circle(7,current,coords[6],r,ci);
circle(8,current,coords[7],r,ci);
circle(9,current,coords[8],r,ci);
r = r - (radius/sLength);
}

}

if ( !$.cookie('ncn_clock_display') ) {
	$.cookie('ncn_clock_display','triplet');
}

var clockBox = $('#clockBox');
var display = $.cookie('ncn_clock_display');

var hours = document.getElementById('hours');
var minutes = document.getElementById('minutes');
var seconds = document.getElementById('seconds');
var hms = document.getElementById('clock');

var c1;
var c2;

function clock() {
	var today=new Date();
	var h=today.getHours();
	var m=today.getMinutes();
	var s=today.getSeconds();
	m = (m<10) ? "0"+m: m;
	s = (s<10) ? "0"+s: s;
	ncn(""+h+m+s,hms);
	c1=setTimeout('clock()',500);
}

function clocktwo() {
	var today=new Date();
	var h=today.getHours();
	var m=today.getMinutes();
	var s=today.getSeconds();
	ncn(h,hours);
	ncn(m,minutes);
	ncn(s,seconds);
	c2=setTimeout('clocktwo()',500);
}


var expnum = $("#countbox").val();
function updateExp() {
	$("#countbox").val(expnum);
	if ( expnum == "" ) expnum = "0";
	expnum = parseInt(expnum) + "";
	ncn(expnum,$("#counter")[0]);
}
$("#counter").click(function(){
	expnum = parseInt( expnum ) + 1;
	updateExp();
});
$("#countbox").keyup(function(){
	expnum = $(this).val();
	updateExp();
}).blur(function(){
	if ( $(this).val() == "" ) {
		$(this).val("1");
		updateExp();
	}
});
$("#reset").click(function(){
	expnum = "0";
	updateExp();
	return false;
});
$("#factor").click(function(){
	if ( expnum.length < 20 ) expnum = expnum + "0";
	updateExp();
	return false;
});

updateExp();


function setDisplay(link) {

	// $("html").removeClass("white-bg");
	$("#about,#exp,#sing,#trip").hide();
	$("#views .active").removeClass("active");

	if ( link.match(/about/) ) {
		
		$('#about-link').addClass('active');
		$("#about").show();
		
	} else if ( link.match(/exp/) ) {
		
		// $("html").addClass("white-bg");
		$("#exp-link").addClass("active");
		$("#exp").show();
		
	} else {
		
		if ( link.match(/triplet/) ) {
			$.cookie("ncn_clock_display","triplet");
			$('#triplet').addClass('active');
			
			$("#trip").show();
			clearTimeout(c2);
			clocktwo();
			
		}

		if ( link.match(/single/) ) {
			$.cookie("ncn_clock_display","single");
			$('#single').addClass('active');
			
			$("#sing").show();
			clearTimeout(c1);
			clock();
			
		}

		
	}
	
	return false;
	
}

setDisplay(display);

$("#views").show();
