/*
	2dare.co.uk blog code
	working, ready to be cleaned up
*/
var dx = 160;
var dy = 160;

var dist = 64;

var canvas;
var ctx;

var dheight;

$(document).ready(function() {
	// alert('hello');
	$(window).resize( windowResized );
	
	// canvas stuff
	canvas = document.getElementById('drawing');
	ctx = canvas.getContext('2d');
	dheight = $(document).height();
	canvas.height = dheight;
	
	drawGrad( ctx, 60, 60, 400, 400);
	
	setInterval( anim, 20 );
	showOffset();
});

function drawGrad( ctx, x, y, wid, hei ) {
	rad = (wid + hei)/2;
	nx = x + (wid/2);
	ny = y + (hei/2);
	grad = ctx.createRadialGradient(nx,ny,rad * 0.1,nx,ny,rad);
	grad.addColorStop(0, "rgba(255,255,255,0.15)");
	//		grad.addColorStop(0.2, $('body').css('backgroundColor'));
	grad.addColorStop(0.2, 'rgba(255,165,0,0)');
	ctx.fillStyle = grad;
	// ctx.strokeStyle = 'rgba(0,0,0,0.15)';
	ctx.fillRect(x,y,wid,hei);
	// ctx.strokeRect(x,y,wid,hei);
}

function anim() {
	wid = $(window).width();
	$('#page').css('left', (wid - 960) /2);
	$('#drawing').css('left', ((wid - 960) /2)+10);
	
	//canvas
	// ctx.strokeStyle = "rgba(20,20,20,0.5)";
	// ctx.strokeStyle = "rgba(55,55,55,0.25)";
	ctx.strokeStyle = $('body').css('backgroundColor');
	ctx.beginPath();
	ctx.moveTo(dx,dy);
	updown = Math.ceil(Math.random() * 3)-2;
	oldx = dx;
	oldy = dy;
	if((Math.random()*10) > 5) {
		// horizontal
		dx += dist*updown;
	} else {
		// vertical
		dy += dist*updown;
	}
	if((Math.random()*20) > 19) {
		ns = 32 + (Math.random() * 1200);
		nx = (Math.random() * 960) - (ns/2);
		ny = (Math.random() * dheight) - (ns/2);
		drawGrad( ctx, nx, ny, ns, ns);
	}
	// ctx.lineTo(dx,dy);
	ctx.quadraticCurveTo(dx,dy,dx+((oldx-dx)/2)+(Math.random()*dist*updown),dy+((oldy-dy)/2)+(Math.random()*dist*updown));
	ctx.closePath();
	ctx.stroke();
	
	if( (dx > 960) | (dx < 0) | (dy>canvas.height) | (dy<0) ) {
		dx=160;
		dy=160;
	}
	
	showOffset();
}

function showOffset() {
	off = $('#some_text').offset();
	$('#tl').text( ""+ off.left );
	$('#tt').text( ""+ ($(window).height() - off.top) );
}

function windowResized() {
	showOffset();
}