var lastTime = 0;
var fTimer = 0;
var zeitScheibe = 0;

function supportsCanvas() {
	return !!document.createElement('canvas').getContext;
};

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

function randColor () {
	return "#"+((1<<24)*Math.random()|0).toString(16);
};

function randGreyColor () {
	var grey = rand(MIN_COLOR, MAX_COLOR).toString(16);
    grey = (grey.length > 1 ? grey : '0'+ grey);
    return "#"+ grey +''+ grey +''+ grey;
};

function setPixel (xPos, yPos) {
	if(unicorn)
		ctx.fillStyle = unicornColor;
	else
		ctx.fillStyle = randGreyColor();
		
	ctx.fillRect (xPos, yPos, PXL_DIAMETER, PXL_DIAMETER);
};

function calcFrameDelta () {

	var time = new Date();
	var timeInMs = time.getTime();

	if(lastTime == 0)
		lastTime = timeInMs;

	var interval = lastTime - fTimer;
	
	if(interval >= 1000)
		fTimer = lastTime;
	else
		zeitScheibe = 100 / 1000 * interval;

	lastTime = timeInMs;
};

/**
 * Provides requestAnimationFrame in a cross browser way.
 * http://paulirish.com/2011/requestanimationframe-for-smart-animating/
 */
if ( !window.requestAnimationFrame ) {

	window.requestAnimationFrame = ( function() {

		return window.webkitRequestAnimationFrame ||
		window.mozRequestAnimationFrame || // comment out if FF4 is slow (it caps framerate at ~30fps: https://bugzilla.mozilla.org/show_bug.cgi?id=630127)
		window.oRequestAnimationFrame ||
		window.msRequestAnimationFrame ||
		function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {

			window.setTimeout( callback, 1000 / 60 );

		};

	} )();
};

function checkInput( e ) {

	switch(e.keyCode ) {
		case 27: /*ESC*/
			rubbelDieKatz = !rubbelDieKatz;
			updateFx();
			break;
	}
};

function createRenderContextAt (elementID) {

	var container = document.getElementById(elementID);

	var canvas = document.createElement('canvas');

	container.appendChild(canvas);
	
	canvas.width  = container.offsetWidth;
	canvas.height = container.offsetHeight;

	return canvas.getContext('2d');
};

//circle with bresenheim algorithm
//ported from http://en.wikipedia.org/wiki/Midpoint_circle_algorithm#Optimization
function circle(cx, cy, radius) {

	var mRadius = radius * PXL_DIAMETER;

	var error = -mRadius;
	var x = mRadius;
	var y = 0;

	while (x >= y) {
		
		plot8points(cx, cy, x, y);

		error += y;
		y += PXL_DIAMETER;
		error += y;

		if (error >= 0)	{
			x -= PXL_DIAMETER;
			error -= x;
			error -= x;
		}
	}
};

function plot8points(cx, cy, x, y) {

	plot4points(cx, cy, x, y);
	if (x != y) plot4points(cx, cy, y, x);
};

function plot4points(cx, cy, x, y) {

	setPixel(cx + x, cy + y);

	if (x != 0) setPixel(cx - x, cy + y);
	if (y != 0) setPixel(cx + x, cy - y);
	if (x != 0 && y != 0) setPixel(cx - x, cy - y);
};

function changeSiteText (newLang) {
	
	var legalTextDe = "<address>BurgEins GmbH.\nBahnhofstraße 6,\n09111 Chemnitz,\nDeutschland</address><p>Tel.: +49-371-27 230 230<br>Fax: +49-371-27 230 232<br><span id='eMail'>=)</span><br></p><p>Handelsregister Chemnitz, HRB 24291<br>Geschäftsführer: Maik Golomb<br>USt-Id Nr.: DE 260683425<br></p>";

	var legalTextEn = "<address>BurgEins GmbH.\nBahnhofstrasse 6,\n09111 Chemnitz,\nGermany</address><p>Phone: +49-371-27 230 230<br>Fax: +49-371-27 230 232<br><span id='eMail'>=)</span><br></p><p>Commercial Register Chemnitz, HRB 24291<br>Managing Director: Maik Golomb<br>VAT id number: DE 260683425<br></p>";
	
	if(newLang == 'de') {
		
		document.getElementById('address').innerHTML = legalTextDe;
		document.getElementById('impressum').innerHTML = 'Impressum';
		
	} else {

		document.getElementById('address').innerHTML = legalTextEn;
		document.getElementById('impressum').innerHTML = 'Legal Info';
	}
	
	insertMail();
};

function changeLang(newLang) {
	
	if(getLang() != newLang) {
		
		setLang(newLang);
		
		changeSiteText(newLang);
		
		var std = 'langSwitch clickable';
		
		document.getElementById('de').className = std;
		document.getElementById('en').className = std;
		
		document.getElementById(newLang).className += " active";
		
		getSentence();
	}
};
