$(function(){

	$('canvas.pie').each(function(){
		var hlutfall = $(this).attr('value');
	
		function toRad(integer){
			return (Math.PI/180)*integer;
		}

		var dim = {'w': $(this).attr('width'), 'h': $(this).attr('height')};

		dim.size = dim.w > dim.h ? dim.h : dim.w;
		dim.x = dim.w / 2;
		dim.y = dim.h / 2;
		dim.r = Math.round(dim.size / 2 * 0.80);
		dim.r2 = Math.round(dim.size / 2 * 0.85);

		var ctx = $(this).get(0).getContext('2d');
		ctx.clearRect(0,0, 1000, 1000);	
		ctx.save();	

		// Full Shadow
		ctx.beginPath();
		ctx.moveTo(dim.x, dim.y);
		ctx.arc(dim.x+3, dim.y+3, dim.r, toRad(-hlutfall*1.8), toRad(360+hlutfall*1.8), true);
		ctx.closePath();
		ctx.fillStyle = '#eee';
		ctx.fill();

		// Slice Shadow
		ctx.beginPath();
		ctx.moveTo(dim.x, dim.y);
		ctx.arc(dim.x+3, dim.y+3, dim.r2, toRad(hlutfall*1.8), toRad(-hlutfall*1.8), true);
		ctx.closePath();
		ctx.fillStyle = '#eee';
		ctx.fill();

		// Full Circle
		ctx.beginPath();
		ctx.moveTo(dim.x, dim.y);
		ctx.arc(dim.x, dim.y, dim.r, toRad(-hlutfall*1.8), toRad(360+hlutfall*1.8), true);
		ctx.closePath();
		ctx.fillStyle = '#9F9';
		ctx.fill();
		ctx.lineWidth  = 1;
		ctx.strokeStyle = '#6C6';
		ctx.stroke();

		// 3d highlight
		ctx.beginPath();
		ctx.moveTo(dim.x-dim.r+3, dim.y);
		ctx.arc(dim.x, dim.y, dim.r-3, toRad(180), toRad(275), false);
		ctx.arc(dim.x, dim.y, dim.r-3, toRad(275), toRad(180), true);
		ctx.closePath();
		ctx.lineWidth  = 1;
		ctx.strokeStyle = 'rgba(255,255,255,0.4)';
		ctx.stroke();

		// 3d shade
		ctx.beginPath();
		ctx.moveTo(dim.x+dim.r-3, dim.y);
		ctx.arc(dim.x, dim.y, dim.r-3, toRad(0), toRad(90), false);
		ctx.arc(dim.x, dim.y, dim.r-3, toRad(90), toRad(365), true);
		ctx.closePath();
		ctx.lineWidth  = 1;
		ctx.strokeStyle = 'rgba(0,0,0,0.03)';
		ctx.stroke();


		// Slice
		ctx.beginPath();
		ctx.moveTo(dim.x, dim.y);
		ctx.arc(dim.x+1, dim.y-1, dim.r2, toRad(hlutfall*1.8), toRad(-hlutfall*1.8), true);
		ctx.closePath();
		ctx.fillStyle = '#F99';
		ctx.fill();
		ctx.lineWidth  = 1;
		ctx.strokeStyle = '#C66';
		ctx.stroke();
	});


	$("#almenn_leit")
	.val('Leit...')
	.focus(function(){if(this.value=='Leit...')this.value=''; $('body').addClass('leit_i_gangi');})
	.blur(function(){if(this.value=='')this.value='Leit...'; $('body').removeClass('leit_i_gangi');})
	.autocomplete('leit.php', {'maxItemsToShow': 5, 'cacheLength': 5, 'onItemSelect': function(li){
		document.location = li.extra[0];
	}});
});

var dags = function(date)
{
	var manudir = ['janúar','febrúar','mars','apríl','maí','júní','júlí','ágúst','september','október','nóvember','desember'];
	return date.getDate() + '. ' + manudir[date.getMonth()] + ' ' + date.getFullYear();
}

var graf_dags = function(t)
{
	var date = new Date();
	date.setTime(t);
	var manudir = ['jan','feb','mar','apr','maí','jún','júl','ágú','sep','okt','nóv','des'];
	return date.getDate() + '. ' + manudir[date.getMonth()] + '. ' + new String(date.getFullYear()).substr(2,2);
}

var dags_timi = function(t)
{
	var date = new Date();
	date.setTime(t);
	var manudir = ['jan','feb','mar','apr','maí','jún','júl','ágú','sep','okt','nóv','des'];
	return date.getDate() + '. ' + manudir[date.getMonth()] + '. ' + new String(date.getFullYear()).substr(2,2) + ' kl. ' + date.toTimeString().substr(0,5);
}

var peningar = function (tala)
{
	return (new String(tala)).replace('.',',');
} 
