// <SCRIPT SRC="">
// TOJAM specific methods v1.0

// -----------------------------------------------------------------------------------------------------------------------
// Space Invaders: Explosion, Ship, Invader and the World methods that control them.
var mmilli_hold = 0;
var mEnterMouseover = false;
var mEnterTop = -2000, mEnterLeft = -2000;

var mShip = null;
var mShipCount = 0;

var mExplosions = null;   // array of Explosions
var mExplosionCount = 0;
var mExplosionIndex = -1;

var mInvaders = null;   // array of Invaders
var mInvaderCount = 0; 
var mInvaderSpawn = 0;
var mInvaderHeight = 0;

var mWorldWidth = 0, mWorldHeight = 0;
var mGlovesAreOff = false;

var mInvaderShow = new Array(20);
var mInvaderHit = new Array(20);

function Invader_Attack() {	
	for (var i = 0; i < mInvaderCount; i++) {
		if ((mInvaders[i].alive) && (!mInvaders[i].hurt)) { mInvaders[i].attack=true;}
	}		
}

function Invader_Layer(pID, pWorld, pZIndex, pIndexStart, pIndexFinish, pExplosion) {
	var html = "";

	if (pWorld) { html += "<DIV ID='" + pID + "' Style='position:absolute; left:0px; top:0px; width:0px; height:0px; padding:0px; margin:0px; overflow:hidden; z-Index:" + pZIndex + ";'>"; }
		
		html += "<IMG ID='Ship' Src='../images/world_ship.gif' width=52 height=28 Style='position:absolute; left:-100px; top:-100px; z-Index:1;'>";
		html += "<IMG ID='Laser' Src='../images/world_laser.gif' width=2 height=10 Style='position:absolute; left:-100px; top:-100px; z-Index:1;'>";
			
		for (var index=pIndexStart; index < pIndexFinish; index++) {
			html += "<IMG ID='Invader" + index + "' Src='../images/world_invader.gif' Width=44 Height=32 Style='position:absolute; left:-100px; top:-100px; width:44px; Height:32px; z-Index:1;'>";
		}

		if (pExplosion) {
			// Explosions only get added to the top layer.
			for (index=0; index < mExplosionCount; index++) {
				html += "<IMG ID='Explosion" + index + "' Src='../images/world_explosion.gif' width=35 height=35 Style='position:absolute; left:-100px; top:-100px; z-Index:200;'>";
			}
		}

		if (pZIndex == 1) { html += "<IMG ID='Background' Src='../images/world_cntower_left.gif' width=429 height=550 Style='position:absolute; left:-1000px; top:-1000px;'>"; }
		if (pExplosion) { html += "<IMG ID='BackgroundRight' Src='../images/world_cntower_right.gif' width=150 height=500 Style='position:absolute; left:-1000px; top:-1000px;'>"; }

	if (pWorld) { html += "</DIV>"; }

	return html;
}

function Invader_Initialize(pInvaderCount, pInvaderSpawn, pInvaderHeight, pShip, pWorld) {
	var html = "";
	
	if (typeof(pInvaderCount) != "number") { pInvaderCount = 100; }
	if (typeof(pInvaderSpawn) != "number") { pInvaderSpawn = 0.012; }  // used to be .01 and .02
	if (typeof(pInvaderHeight) != "number") { pInvaderHeight = 1.0; }
	if (typeof(pShip) != "boolean") { pShip = false; }
	if (typeof(pWorld) != "boolean") { pWorld = true; }

	mShip = null;
	if (pShip) { mShipCount = 1 } else { mShipCount = 0 };
	
	mInvaderCount = pInvaderCount;	
	mInvaders = new Array(mInvaderCount);	
	mInvaderSpawn = pInvaderSpawn;
	mInvaderHeight = pInvaderHeight;

	mExplosionCount = 30;
	mExplosions = new Array(mExplosionCount);
	mExplosionIndex = -1;

	// Need World containers to prevent scrollbars from appearing
	if ((pWorld) && (gBrowser.name == "Internet Explorer")) {
		// 2 Containers/Layers. Background and Foreground. 80% of the invaders are in the background.
		var Upperz = parseInt(mInvaderCount * 0.80);
		html += Invader_Layer("world", pWorld, 1, 0, Upperz, false);
		html += Invader_Layer("world2", pWorld, 9, Upperz, mInvaderCount, true);
		// Can't do this in FireFox since overlapped DIVs do not pass events underneath them (doesn't count as bubbling, that's only parent/child divs - not overlapped).
	} else {
		// Page takes care of the World container, so dump everything into it.
		html += Invader_Layer("world", pWorld, 1, 0, mInvaderCount, true);
	}

	document.write(html);	
	addLoadEvent(world_onload);
}

// --- Explosion ---
function Explosion(pID) {
	this.id = parseInt(pID);
	this.left = parseFloat(0);
	this.top = parseFloat(0);
	this.imagez = null;
	this.counter = 0;
	
	this.Initialize = function (pleft, ptop) {
		this.left = pleft;
		this.top = ptop;
		this.imagez = window.document.getElementById("Explosion" + this.id);
		this.counter = 20 + parseInt(Math.random() * 40);
	}
	
	this.Movez = function() {
		this.counter--;
		
		if (this.counter > 0) {
    		this.left += -2 + parseInt(Math.random() * 5);
			this.top += -2 + parseInt(Math.random() * 5);				

    		this.imagez.style.left = this.left;
			this.imagez.style.top = this.top;
			
		} else {
    		this.imagez.style.left = -100;
			this.imagez.style.top = -100;
		}				
	}
}	

// --- Ship ---
function Ship() {
	this.left = parseFloat(0);
	this.top = parseFloat(0);
	this.imagez = null;
	this.imagelaser = null;
	this.speed = 15;
	this.mouseXHold = 0;

	this.Initialize = function () {
		this.left = parseInt((gBrowser.width - 52) / 2);
		this.top = gBrowser.height - 40;
		this.imagez = window.document.getElementById("Ship");
		this.imagelaser = window.document.getElementById("Laser");
	}
	
	this.Movez = function()
	{		
		if ((gBrowser.keys[37]) || (gBrowser.keys[65])) { this.left = this.left - this.speed; } // left arrow or w
		if ((gBrowser.keys[39]) || (gBrowser.keys[68])) { this.left = this.left + this.speed; } // right arrow or d			
		if (gBrowser.mouseX != this.mouseXHold) { this.left = gBrowser.mouseX - 26 - 4; }
		this.mouseXHold = gBrowser.mouseX;
		if (this.left < 18) { this.left = 18; } else if (this.left > gBrowser.width - 52 - 18) { this.left = gBrowser.width - 52 - 18; }

		this.top = gBrowser.height - 28;
		
		this.imagez.style.left = this.left;
		this.imagez.style.top = this.top;

		if (gBrowser.mouseDownFlag) {	// gBrowser set mouseDownFlag to true when pressed, but NEVER sets it to False
			this.Shoot();
			gBrowser.mouseDownFlag = false;
		} else if ((gBrowser.keys[38]) || (gBrowser.keys[87]) || (gBrowser.keys[32])) {
			this.Shoot();
		} else {
			if (this.imagelaser.height != 5) {
	    		this.imagelaser.style.left = -100;
	    		this.imagelaser.style.top = -100;
				this.imagelaser.height = 5;
			}
		}			
	}
	
	this.Shoot = function()
	{
		var indexhit = -1;
		var invader = null;
		
		for (var index = 0; index < mInvaderCount; index++) {
			invader = mInvaders[index];
			if ((invader.alive) && (invader.top > -100)) {
				if ((mShip.left + 25 >=  invader.left) && (mShip.left + 27 <= invader.left + invader.imagez.width)) {
					if ((indexhit == -1) || (invader.top > mInvaders[indexhit].top)) {
						indexhit = index;
					}
				}
			}
		}

		if (indexhit == -1) {
    		this.imagelaser.style.left = this.left + 25;
    		this.imagelaser.style.top = 0;
			this.imagelaser.height = (this.top - 1);
		} else {
			invader = mInvaders[indexhit];
			this.imagelaser.style.zIndex = invader.imagez.style.zIndex;
			this.imagelaser.style.left = this.left + 25;
    		this.imagelaser.style.top = invader.top + parseInt(invader.imagez.height / 2);
			this.imagelaser.height = ((this.top - 1) - (invader.top + parseInt(invader.imagez.height / 2)));			
			
			// invader_hit(indexhit, invader.left + 26, invader.top + 16);
			invader_hit(indexhit, this.left + 25, invader.top + parseInt(invader.imagez.height / 2));
		}
	}
}

function invader_hit(pIndex, pHitX, pHitY) {
	var invader = null;	
	invader = mInvaders[pIndex];

	mExplosionIndex += 1 + parseInt(Math.random() * 3);
	if (mExplosionIndex >= mExplosionCount) { mExplosionIndex -= mExplosionCount; }
	mExplosions[mExplosionIndex].Initialize(pHitX - 17, pHitY - 17); // -17 is 1/2 the width/height of explosion graphic
	mExplosions[mExplosionIndex].imagez.style.zIndex = invader.imagez.style.zIndex - 1;

	if ((invader.hitcounter < 2) && (invader.type > 2)) {
		if (invader.hitcounter == 0) {
			if (mInvaderHit[invader.type] != 1) {
				if (pageTracker) { pageTracker._trackPageview("/invader/sponsor_" + invader.googlename + "_2Hit.html"); }
				mInvaderHit[invader.type] = 1;
			}
			if (mInvaderShow[invader.type] != 1) {
				if (pageTracker) { pageTracker._trackPageview("/invader/sponsor_" + invader.googlename + "_1Show.html"); }
				mInvaderShow[invader.type] = 1;
			}
		}
		invader.speed_lefthold = -(invader.speed_lefthold * 1.10);

		invader.speed_left = invader.speed_lefthold;
		invader.speed_top = -10 + parseInt(Math.random() * 20);

		invader.left += invader.speed_left; invader.top += invader.speed_top;
		invader.hitcounter++;
	}
	else
	{
		if (!invader.hurt) {
			if ((pageTracker) && (invader.type > 2)) { pageTracker._trackPageview("/invader/sponsor_" + invader.googlename + "_3Destroy.html"); }
			var deltax = 0, deltay = 0;
			deltax = parseInt((parseInt(invader.imagez.deathwidth) - parseInt(invader.imagez.width)) / 2);
			deltay = parseInt((parseInt(invader.imagez.deathheight) - parseInt(invader.imagez.height)) / 2);

			invader.left -= deltax; invader.top -= deltay;
    		invader.imagez.style.left = invader.left;
    		invader.imagez.style.top = invader.top;
    		invader.imagez.width = invader.imagez.deathwidth; invader.imagez.height = invader.imagez.deathheight;
    		invader.imagez.style.width = invader.imagez.deathwidth; invader.imagez.style.height = invader.imagez.deathheight;

			invader.imagez.src = invader.imagez.src.replace("invader", "invaderhurt");
			invader.hurt = true;
			mInvaderSpawn += .002;  // increase the odds of more invaders appearing
			if (mInvaderSpawn > .95) { mInvaderSpawn = .95; }   /// have to ensure staggered
		}

		if (invader.type <= 2) {
			invader.speed_left = (5.0 + Math.random() * 5.0); if (Math.random() < 0.5) { invader.speed_left = -invader.speed_left; }
		 	// (-9.0 + Math.random() * 18.0);
			invader.speed_top = -(invader.speed_top * 0.5) - 6.0 - (Math.random() * 10.0);
		} else {
			invader.speed_left = invader.speed_left / 2;
			invader.speed_top = -5; // invader.speed_top;
		}
		mGlovesAreOff = true;
	}
}

// --- Invader ---
function Invader(pID) {
	this.id = parseInt(pID);
	this.imagez = null;
	this.left = parseFloat(0);
	this.top = parseFloat(0); this.tophold = parseFloat(0);
	this.speed_left = parseFloat(0); this.speed_lefthold = parseFloat(0);
	this.speed_top = parseFloat(0); this.speed_tophold = parseFloat(0);
	this.hurt = false;
	this.attack = true;	
	this.alive = false;
	this.hitcounter = 0;
	this.type = 0;
	this.href = "";
	this.googlename = "";
	
	this.enterx = parseFloat(0);

	this.Initialize = function () {
		if (this.imagez == null) {
			this.imagez = window.document.getElementById("Invader" + this.id);
		}
		this.imagez.jimindex = pID;
		if (mShipCount == 0) { this.imagez.onmouseover = function () { invader_hit(this.jimindex, gBrowser.mouseX, gBrowser.mouseY); }; }
		
		this.googlename = "";
		this.hitcounter = 0;
		var r = Math.random();

		// put this back up to .03
		r = 0.85;
		if (r < 0.02) {
			this.googlename = "innovation";
			this.imagez.width = 50; this.imagez.height = 50;
			this.imagez.style.width = 50; this.imagez.style.height = 50;
			this.imagez.deathwidth = 150; this.imagez.deathheight = 100;
			this.imagez.src= "../images/world_invader_sponsor_innovation.gif";
			this.type = 6;

		} else if (r < 0.04) {
			this.googlename = "firetoad";
			this.imagez.width = 50; this.imagez.height = 50;
			this.imagez.style.width = 50; this.imagez.style.height = 50;
			this.imagez.deathwidth = 150; this.imagez.deathheight = 100;
			this.imagez.src= "../images/world_invader_sponsor_firetoad.gif";
			this.type = 5;

		} else if (r < 0.06) {
			this.googlename = "bedlam";
			this.imagez.width = 36; this.imagez.height = 50;
			this.imagez.style.width = 36; this.imagez.style.height = 50;
			this.imagez.deathwidth = 150; this.imagez.deathheight = 100;
			this.imagez.src= "../images/world_invader_sponsor_bedlam.gif";
			this.type = 4;

		} else if (r < 0.08) {
			this.googlename = "clocke";
			this.imagez.width = 50; this.imagez.height = 50;
			this.imagez.style.width = 50; this.imagez.style.height = 50;
			this.imagez.deathwidth = 150; this.imagez.deathheight = 100;
			this.imagez.src= "../images/world_invader_sponsor_clocke.gif";
			this.type = 3;

		} else if (r < 0.10) {
			this.googlename = "fuse";
			this.imagez.width = 60; this.imagez.height = 17;
			this.imagez.style.width = 60; this.imagez.style.height = 17;
			this.imagez.deathwidth = 150; this.imagez.deathheight = 100;
			this.imagez.src= "../images/world_invader_sponsor_fuse.gif";
			this.type = 8;

		} else if (r < 0.12) {
			this.googlename = "notebook";
			this.imagez.width = 60; this.imagez.height = 30;
			this.imagez.style.width = 60; this.imagez.style.height = 30;
			this.imagez.deathwidth = 150; this.imagez.deathheight = 100;
			this.imagez.src= "../images/world_invader_sponsor_notebook.gif";
			this.type = 7;

		} else if (r < 0.90) {
			this.imagez.width = 44; this.imagez.height = 32;
			this.imagez.style.width = 44; this.imagez.style.height = 32;
			this.imagez.deathwidth = 44; this.imagez.deathheight = 32;
			this.imagez.src= "../images/world_invader.gif";
			this.type = 1;

		} else  {
			this.imagez.width = 44; this.imagez.height = 29;
			this.imagez.style.width = 44; this.imagez.style.height = 29;
			this.imagez.deathwidth = 44; this.imagez.deathheight = 29;
			this.imagez.src = "../images/world_cheese_invader.gif";
			this.type = 2;
		}

		this.speed_left = 1 + Math.random() * 3.0;
		if (Math.random() < .05) { this.speed_left = 12; }
		this.left = parseFloat(-this.imagez.width); if (Math.random() < .5) { this.left = parseFloat(gBrowser.width); this.speed_left = -this.speed_left;}
		this.speed_lefthold = this.speed_left;
		this.top = parseInt((mWorldHeight - this.imagez.height) * mInvaderHeight * Math.random()); this.tophold = this.top;

		this.speed_top = parseFloat(0);
		this.hurt = false;
		if (mEnterMouseover) {
			this.attack = true;
		} else if (mGlovesAreOff) {
			if (Math.random() < 0.50) { this.attack = true; } else { this.attack = false; }
		} else {
			this.attack = false; 
		}

		// By getting rid of zIndex EVERY PLACE on the site, and just default to order on page = zIndex, we would get a significant speed boost
		if (Math.random() < 0.25) { this.imagez.style.zIndex = 15; } else { this.imagez.style.zIndex = 2; }

		this.enterx = (mEnterLeft + 20) + Math.random() * (194 - 40 - this.imagez.width);
		this.alive = true;
	}

	this.Movez = function () {
		this.left += this.speed_left; this.top += this.speed_top;

		if (this.hurt) {
			this.speed_top += 0.5;
		} else if ((this.type <= 1) && (this.attack) && (mEnterTop > -125)) {
			if ((Math.abs(mEnterTop - 20 - this.top) < 15) && (this.left > mEnterLeft - 10) && (this.left < mEnterLeft + 237 - 34)) {
				this.imagez.style.zIndex = 100;				
				mEnterTop -= .75; this.top = parseInt(mEnterTop - 20 - Math.random() * 5);
				var e = window.document.getElementById("Enterz");
				e.style.top = mEnterTop; 
				if (e.style.zIndex != 99) { e.style.zIndex = 99; } 
				this.speed_left = 0; this.speed_top = 0;
			} else {
				this.speed_left += ((this.enterx - this.left) / 40.0);
				this.speed_top += (((mEnterTop - 25) - this.top) / 40.0);
				this.speed_left = this.speed_left * .75;
				this.speed_top = this.speed_top * .75;
			}
		} else {
			this.speed_left += this.speed_lefthold;
			this.speed_top += ((this.tophold - this.top) / 40.0);
			this.speed_left = this.speed_left * .75;
			this.speed_top = this.speed_top * .75;
		}			

		if (this.top > mWorldHeight) {
			this.Terminate();
		} else if ((this.left < 0) && (this.speed_left < 0)) {
			if (this.hurt) { this.left = 0; this.speed_left = 1 + Math.abs(this.speed_left); } else if (this.left < -this.imagez.width) { this.Terminate(); }
		} else if ((this.left > gBrowser.width - this.imagez.width) && (this.speed_left > 0)) {
			if (this.hurt) { this.left = gBrowser.width - this.imagez.width; this.speed_left = -1 - Math.abs(this.speed_left); } else if (this.left > gBrowser.width) { this.Terminate(); }
		}
		if (this.alive) {				
    		this.imagez.style.left = this.left;
    		this.imagez.style.top = this.top;
		}
	}

	this.Terminate = function () {		
		if ((this.type > 2) && (mInvaderShow[this.type] != 1)) {
			if (pageTracker) { pageTracker._trackPageview("/invader/sponsor_" + this.googlename + "_1Show.html"); }
			mInvaderShow[this.type] = 1;
		}

		this.imagez.style.left = -100;
		this.imagez.style.top = -100;
		this.alive = false;
	}

}

// --- Invader Controllers: Load & Timer --
function world_onload() {	
	// Step 1: Initialize ship (if developer wants it)
	if (mShipCount > 0) {		
		mShip = new Ship();
		mShip.Initialize();
	}
	
	// Step 2: Initialize aliens
	for (var i = 0; i < mInvaderCount; i++) {
		mInvaders[i] = new Invader(i); 
	}

	// Step 3: Initialize Explosions
	for (var i = 0; i < mExplosionCount; i++) {
		mExplosions[i] = new Explosion(i);
	}
	
	mmilli_hold = new Date().getTime();

	world_onresize();
	addResizeEvent(world_onresize);
	world_ontimer();
}

function world_ontimer() {
	var index = 0, counter = 0;

	// called every second, but check just in case inconsitent across browsers
	if (window.document) {
		if (mShip != null) { mShip.Movez(); }
	
		if (Math.random() < mInvaderSpawn) {
			// Create an Invader. Don't start at 0 since first 1/2 are on world, and second 1/2 are on world2 (front)
			index = parseInt(mInvaderCount * Math.random());
			while (counter < mInvaderCount) {
				if (!mInvaders[index].alive) {
					mInvaders[index].Initialize(); break;
				} else {
					index++; if (index >= mInvaderCount) { index = 0; }
					counter++;
				}
			}
			
			/*
			for (var i = 0; i < mInvaderCount; i++) {
				if (!mInvaders[i].alive) { mInvaders[i].Initialize(); break; }
			}
			*/
		}

	
		if (new Date().getTime() - mmilli_hold > 104) {
			
			for (var i = 0; i < mInvaderCount; i++) {
				if ((mInvaders[i].alive) && (!mInvaders[i].hurt)) { mInvaders[i].Movez();}
			}
			mmilli_hold = new Date().getTime();
		}			


		for (var i = 0; i < mInvaderCount; i++) {
			if ((mInvaders[i].alive) && (mInvaders[i].hurt)) { mInvaders[i].Movez();}
		}		
		
		for (var i = 0; i < mExplosionCount; i++) {
			if (mExplosions[i].counter > 0) { mExplosions[i].Movez();}
		}
	}		

	window.setTimeout("world_ontimer();", 30);
}

var flag = false;
function world_onresize() {
	// invader_onresize occurs AFTER header_tojam_onresize
	if (!flag) {
		flag = true;

		gBrowser.pageWidth = 0; gBrowser.pageHeight = 0;
		gBrowser.pageIdExcludes = new Array("world", "world2");   // don't want world and world2 counted towards pageWidth/pageHeight
		gBrowser.pageTagIncludes = new Array("div");   // // don't want invaders, <b> tags counted towards pageWidth/pageHeight
		Browser_PageWidthHeight(document.getElementsByTagName("body").item(0));

		mWorldWidth = gBrowser.width;
		if (mWorldWidth > gBrowser.widthmin) { mWorldWidth = "100%"; }   // 100% knows how to deal with the scrollbar (discrepency between IE and Firefox)
		mWorldHeight = gBrowser.pageHeight;
	   if (mWorldHeight < gBrowser.height) { mWorldHeight = gBrowser.height; }

		var e = null;

		// this should NOT cause any scrollbars, because it's nothing new to the browser
		e = window.document.getElementById("world");
		if (e) { e.style.width = mWorldWidth; e.style.height = mWorldHeight;}
		e = window.document.getElementById("world2");
		if (e) { e.style.width = mWorldWidth; e.style.height = mWorldHeight;}	

		// Background CN Towers
		// Bottom Left		
		e = window.document.getElementById("Background").style;
		e.left = 0; e.top = gBrowser.pageHeight - 550; // height of graphic. doesn't cause scrollbars
		// Bottom Right
		e = window.document.getElementById("BackgroundRight").style;
		e.left = gBrowser.width - 157; e.top = gBrowser.pageHeight - 500; // height of graphic. doesn't cause scrollbars

		// alert(gBrowser.width + "," + gBrowser.height + "   " + gBrowser.pageWidth + "," + gBrowser.pageHeight);
	}
	flag = false;
}
