var width = 0; // Window width
var height = 0; // Window height
var iWidth = 50; // Image width
var iHeight = 50; // Image height
var delay = 12;
var body = null;
var elKillingField = null;
var elDivGame = null;
var elDivHurraText = null;
var elDivHurraBtn = null;
var elSpanLabel = null;
var elSelBalls = null;
var elBtnStart = null;
var elBtnPause = null;
var elBtnForfra = null;
var liveBalls = 0;
var deadBalls = 0;
var starttime = 0;
var endtime = 0;
var finaltime = 0;
var ballArray = new Array();
ballArray[0] = '';

function addListeners() {
	if (!document.getElementById) {pleaseUpgrade(); return;}

	body = document.getElementsByTagName('body')[0];
	elKillingField = getEl('killingField');
	elDivGame = getEl('game');
	elDivHurraText = getEl('hurraText');
	elDivHurraBtn = getEl('hurraButton');
	elSpanLabel = getEl('spanLabel');
	elSelBalls = getEl('selBalls');
	elBtnStart = getEl('btnStart');
	elBtnPause = getEl('btnPause');
	elBtnForfra = getEl('btnForfra');

	width = winWidth();
	height = winHeight();
	elKillingField.style.width = width + 'px';
	elKillingField.style.height = height + 'px';

	newBeginning();
}

function newBeginning() {
	for (i = 1; i < ballArray.length; i++) {
		if (getEl('imgImage' + i)) {
			ballArray[i].clearBall();
			body.removeChild(getEl('imgImage' + i));
		}
	}

	elDivGame.style.display = 'block';
	elDivHurraText.style.display = 'none';
	elDivHurraBtn.style.display = 'none';
	elSpanLabel.style.display = 'inline';
	elSelBalls.style.display = 'inline';
	elBtnStart.style.display = 'inline';
	elBtnPause.style.display = 'none';
	elBtnForfra.style.display = 'none';
}

function startBalls() {
	if (!document.getElementById) {pleaseUpgrade(); return;}
	liveBalls = elSelBalls.value - 1;
	var theDiv = null;
	var theImage = null;
	var step = 1;
	var theDirectionX = 0;
	var theDirectionY = 0;

	for (i = 1; i < elSelBalls.value; i++) {
		var xPos = 0;
		var yPos = 0;
		var xOn = false;
		var yOn = false;
		theDiv = document.createElement('div');
		theDiv.id = 'imgImage' + i;
		theDiv.className = 'flyAble';
		theImage = document.createElement('img');
		theImage.src = 'Images/FlyingBalls/Ball' + i + '.png';
		theImage.style.width = '50px';
		theImage.style.height = '50px';
		theDiv.appendChild(theImage);
		body.appendChild(theDiv);
		xPos = (Math.floor(Math.random() * width) + 1) + 'px';
		yPos = (Math.floor(Math.random() * height) + 1) + 'px';
		theDirectionX = Math.floor(Math.random() * 2) + 1;
		theDirectionY = Math.floor(Math.random() * 2) + 1;
		xOn = ((theDirectionX % 2) == 0) ? true : false;
		yOn = ((theDirectionY % 2) == 0) ? true : false;
		step += 1;
		ballArray[i] = new flyingBall('imgImage' + i, xPos, yPos, xOn, yOn, step, delay);
		ballArray[i].start();
	}

	var allDivs = document.getElementsByTagName('div');
	for (var i = 0; i < allDivs.length; i++) {
		if (allDivs[i].className.indexOf('flyAble') != -1)
			addEvent(allDivs[i], 'mousedown', killBall, false);
	}

	elSpanLabel.style.display = 'none';
	elSelBalls.style.display = 'none';
	elBtnStart.style.display = 'none';
	elBtnPause.style.display = 'inline';
	elBtnForfra.style.display = 'inline';

	startClock();
	deadBalls = 0;
}

function killBall(e) {
	var e = e || window.event;
	var t = e.target || e.srcElement; // Must be t!
	var theDiv = getEl(t.parentNode.id);
	if (theDiv) {
		deadBalls++;
		t.src = 'Images/FlyingBalls/Ball20.png';
		setTimeout("removeBall('" + t.parentNode.id + "')", 100);
	}
}

function removeBall(theId) {
	ballArray[getEnd(theId, 'imgImage')].clearBall();
	body.removeChild(getEl(theId));
	if (deadBalls == liveBalls) {
		endClock();
		var numBalls = (deadBalls > 1) ? 'bolde' : 'bold';
		var textMsg = "<span class='resultHead'>"
		textMsg += "<br />Tillykke!<br /></span>"
		textMsg += "<span class='resultBody'>"
		textMsg += "<br /><br />Du skød " + deadBalls + " " + numBalls
		textMsg += "<br />på " + calcTime() + "</span>"
		elDivGame.style.display = 'none';
		elDivHurraText.innerHTML = textMsg;
		elDivHurraText.style.display = 'block';
		elDivHurraBtn.style.display = 'block';
		elBtnPause.style.display = 'none';
		elBtnForfra.style.display = 'none';
	}
}


// Local Little Helpers

function resize() {
//	alert('resize'); // Called twice when loading and resizing in IE only!
	width = winWidth();
	height = winHeight();
	elKillingField.style.width = width + 'px';
	elKillingField.style.height = height + 'px';
}

function startClock() {var today = new Date(); starttime = today.getTime();}
function endClock() {var today = new Date(); endtime = today.getTime();}
function calcTime() {
	var m = 0;
	var s = 0;
	var time = (endtime - starttime - 0)/1000;
	if (time > 60) {
		m = Math.round(time / 60);
		s = Math.round(time % 60);
		return m + ((m < 2) ? ' minut' : ' minutter') + ' og ' + s + ' sekunder!';
	} else if (time > 20)
		return Math.round(time) + ' sekunder!';
	else
		return time.toFixed(2) + ' sekunder!';
}

function pleaseUpgrade() {
	alert('Please upgrade to a new web browser!');
}

addEvent(window, 'load', addListeners, false);
addEvent(window, 'resize', resize, false);

