// graphics //
function circle(x, y, radius, color) {
	ctx.beginPath();
	ctx.lineWidth = 1;
	ctx.arc(x, y, radius, 0, Math.PI*2, true);
	ctx.fillStyle = color;
	ctx.fill();
	ctx.closePath();
}

// math //
function d2r(deg) { //degrees to radians
	return deg * Math.PI / 180;
}

function rand(min, max) {
	return Math.random()*(max-min)+min;
}

function round(number, decimals) {
	var x10 = Math.pow(10, decimals);
	return Math.round(number*x10)/x10;
}

function dist(x1, y1, x2, y2) {
	var dx = Math.abs(x1-x2);
	var dy = Math.abs(y1-y2);
	return Math.sqrt(dx*dx+dy*dy);
}

// collision detection //
$("canvas").inside = function(x, y, r, just_outside) {
	if (just_outside) {
		if (x < -r) return "right";
		else if (x > this.width+r) return "left";
		
		if (y < -r) return "top";
		else if (y > this.height+r) return "bottom";
	}
	else {
		if (x < r) return "right";
		else if (x > this.width-r) return "left";
		
		if (y < r) return "top";
		else if (y > this.height-r) return "bottom";
	}
	
	return true;
}

function isPointInPoly(poly, pt){ // code stolen from jsfromhell: http://jsfromhell.com/math/is-point-in-poly
	for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
		((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y))
		&& (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)
		&& (c = !c);
	return c;
}

// general //
function $(id) {
	return document.getElementById(id);
}

function isset(variable) {
	return variable!==undefined;
}

function fixPos(fixType) {
	var offX = window.innerWidth/2-canvas.width/2;
	var offY = window.innerHeight/2-canvas.height/2;
	
	if ((fixType === undefined) || (fixType == "canvas")) {
		canvas.style.left = offX + "px";
		canvas.style.top = offY + "px";
		
		$("info").style.left = offX + 10 + "px";
		$("info").style.top = offY + 20 + "px";
	}
	if ((fixType === undefined) || (fixType == "menu")) {
		$("menu").style.left = window.innerWidth / 2 + canvas.width / 2 + 20 + "px";
		$("menu").style.top = offY + "px";
	}
}

$("canvas").randPos = function(axis) {
	if (isset(axis)) {
		return axis=="x" ? rand(0, this.width) : rand(0, this.height);
	}
	else {
		return {
			"x": rand(0, this.width),
			"y": rand(0, this.height)
		};
	}
};

$("canvas").restart = function() {
	var off_x = window.innerWidth / 2 - canvas.width / 2;
	var off_y = window.innerHeight / 2 - canvas.height / 2;
	$("score").style.left = off_x + 20 + "px";
	$("score").style.top = off_y + 20  + "px";

	$("score").innerHTML = "";
	clearInterval(draw_interval);
	init();
};

$("canvas").pause_unpause = function() { // automatically knows whether to pause or not
	paused = !paused;
	
	if (paused) {
		var off_x = window.innerWidth / 2 - canvas.width / 2;
		var off_y = window.innerHeight / 2 - canvas.height / 2;
		$("score").style.left = off_x + canvas.width/4 + "px";
		$("score").style.top = off_y + canvas.height/4 + "px";
		$("score").innerHTML = "Paused";
	}
	else {
		$("score").style.left = -100 + "px";
		$("score").style.top = -100 + "px";
		$("score").innerHTML = "";
	}
};
