/*
	Javascript functions 
	(C)2006 Dustin Spicuzza
*/


var timeoutPeriod = 85;		// timeout in ms
var dontDrive = 0;
var curScore = 10000;			// get this from cookie
var weapon = 0;				// 0: paint, 1: bullet, 2: mine, 3: rocket, 4: nuke
var rulesOn = 1;			// rules screen is showing

// put loading crap here... 
function onPageLoad(){
	// browser detection for IE PNG support
	var ieVersion = parseFloat(navigator.appVersion.split("MSIE")[1]);
	fixIE = document.body.filters && ieVersion >= 5.5 && ieVersion < 7;
	
	// this is cheaper than browser detection -- almost works
	document.getElementById('hideme').style.display = 'none';
	if (!curTimer){
		curTimer = window.setInterval("timerHandler()",timeoutPeriod);
	}
	if (dontDrive){
		switchDriving();
	}
	
	document.getElementById('roadside').onclick = roadClick;
	
}

// run this when the page loads.. 
window.onload = onPageLoad;

// run this to start/stop driving
function switchDriving(){

	var o = document.getElementById('mStop');

	if (curTimer){
		o.innerHTML = "Start Driving";
		window.clearInterval(curTimer);
		curTimer = null;
	}else{
		o.innerHTML = "Stop Driving";
		curTimer = window.setInterval("timerHandler()",timeoutPeriod);
	}
}

// switches direction that bb's travel... 
function switchDirection(){
	if (!gameOn){
		for (var i = 0;i< 2;i++){
			waitingForDual[i] = 0;
		}
		bbDirection = bbDirection * -1;
	}
}

// makes it so things blow up
function switchBlowUp(){
	
	var o = document.getElementById('mBlow');
	
	if (!gameOn){
		
		// if the rules are on, make sure we are not driving
		if (rulesOn){
			if (curTimer){
				switchDriving();
			}
		}else{
			// otherwise, make sure we are driving
			if (!curTimer){
				switchDriving();
			}
		}

		// unhide div
		document.getElementById('blowMenu').style.display = 'block';
		document.getElementById('score').innerHTML = "Score: " + curScore;
		o.innerHTML = "Declare Peace";
		
		if (bbDirection == -1)
			switchDirection();
		
		cPtr(0);
		
	}else{
		document.getElementById('blowMenu').style.display = 'none';
		o.innerHTML = "Play Game!";
		
		cPtr(1);
	}
	
	gameOn = !gameOn;
}

function switchRules(){
	o = document.getElementById('rules');
	
	if (rulesOn){
		switchDriving();
		o.style.display = 'none';
	}else{
		if (curTimer){
			switchDriving();
		}
		o.style.display = 'block';
	}
	rulesOn = !rulesOn;
}



// change the pointers for all the elements
function cPtr(type){
	var name;
	var o;
	
	if (type){
		name = 'pointer';
		document.getElementById('roadside').style.cursor = 'default';
	}else{
		name = 'crosshair';
		document.getElementById('roadside').style.cursor = name;
	}

	var parentNode = animationList;
	var curNode = parentNode.next;

	// change all the icons
	while(curNode){
		if (curNode.type < 3){
			o = document.getElementById(curNode.id);
			if (o){
				// IE 5 bug
				try{
					o.style.cursor = name;
				}catch(e){
					o.style.cursor = 'hand';
				}
			}
		}
		parentNode = curNode;
		curNode = curNode.next;
	}

}

function updateScore(amt){
	if (!gameOn)
		return;
	curScore += parseInt(amt);
	document.getElementById('score').innerHTML = "Score: <strong>" + curScore + "</strong>"; 
}

function selectWeapon(num){
	var c = curTimer;
	if (c)
		switchDriving();
	switch (num){
		case 0:		// paint
			weapon = 0;
			break;
		case 1:		// pistol
			if (curScore < 50000){
				window.alert("Need 50000 points!");
			}else{
				weapon = 1;
			}
			break;
		case 2:
			if (curScore < 150000){
				window.alert("Need 150000 points!");
			}else{
				weapon = 2;
			}
			break;
		case 3:
			if (curScore < 300000){
				window.alert("Need 300000 points!");
			}else{
				weapon = 3;
			}
			break;
		case 4:		// nuke
			if (curScore < 750000){
				window.alert("Need 750000 points!");
			}else{
				weapon = 4;
			}
			break;
	}
	if (c)
		switchDriving();
}

function shoot(o){
	switch(weapon){
		case 0:		// paint
			addToQueue(new projectile(o.offsetX,o.offsetY,0));
			break;
		case 1:		// gun
			addToQueue(new projectile(o.offsetX,o.offsetY,1));
			break;
		case 2:		// mine
			addToQueue(new projectile(o.offsetX,o.offsetY,2));
			break;
		case 3:		// rocket
			addToQueue(new rocketItem(o.offsetX,o.offsetY));
			break;
		case 4:		// nuke
			nuke();
			break;
	}
}

var fixIE = 0;

function addImg(id,src,alt,href,isPNG){

	// don't duplicate stuff.
	if (document.getElementById(id)){
		return;
	}

	var item = document.createElement('div');
	var itemImg;
	
	item.id = id;
	
	// fix PNG images in IE only
	if (isPNG && fixIE){
		itemImg = document.createElement('div');
		// width and height don't matter here, because when we animate it will get fixed
		itemImg.style.cssText = "height:1px;width:1px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'" + src + "\', sizingMethod='scale');";
	}else{
		itemImg = document.createElement('img');
		itemImg.height = "1px";
		itemImg.width = "1px";
		itemImg.setAttribute('src', src );
		itemImg.setAttribute('alt',alt);
	}
	
	item.style.position = 'absolute';
	// firefox MAC fix -- move it off the screen at first
	item.style.left = "800px";
	
	itemImg.id = id + '-img';
	item.appendChild(itemImg);
	
	// custom attribute
	item.boom = 0;
	
	// cheap item detection
	if (href && href !== ""){
		
		item.onclick = function(e){
			
			if (gameOn && curTimer){
				if (!e) e = window.event;
				var x = getOffsets(e);
				x.offsetX += parseInt(this.style.left,10);
				x.offsetY += parseInt(this.style.top,10);
				shoot(x);
				// stop propagation
				e.cancelBubble = true;
				if (e.stopPropagation) e.stopPropagation();
			}else{
				window.open(href);
			}		
		};
		
		if (gameOn){
			item.style.cursor = 'crosshair';
		}else{
			try{
				item.style.cursor = 'pointer';
			}catch(e){
				item.style.cursor = 'hand';
			}
		}
			
		item.onmouseover = adIn;
		item.onmouseout = adOut;
	}

	document.getElementById('roadside').appendChild(item);
}

// id to add, src of image, is it a PNG?, zId of the item to append it to :), left
function addBoom(id,src,isPNG,zId){

		// don't duplicate stuff.
	if (document.getElementById(id)){
		return;
	}

	var item = document.createElement('div');
	var itemImg;
	
	item.id = id;
	
	// fix PNG images in IE only
	if (isPNG && fixIE){
		itemImg = document.createElement('div');
		// width and height don't matter here, because when we animate it will get fixed
		itemImg.style.cssText = "height:1px;width:1px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'" + src + "\', sizingMethod='scale');";
	}else{
		itemImg = document.createElement('img');
		itemImg.height = "1px";
		itemImg.width = "1px";
		itemImg.setAttribute('src', src );
		itemImg.setAttribute('alt', "KA-BOOM!");
	}
	
	item.style.position = 'relative';
	
	item.onclick = function(e){
			
		if (gameOn && curTimer){
			if (!e) e = window.event;
			var x = getOffsets(e);
			x.offsetX += parseInt(this.style.left,10);
			x.offsetY += parseInt(this.style.top,10);
			shoot(x);
			// stop propagation
			e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();
		}		
	};
	
	itemImg.id = id + '-img';
	item.appendChild(itemImg);
		
	document.getElementById(zId).appendChild(item);
}

function roadClick(e){
	if (gameOn && curTimer){
		var t;
		if (!e) 
			e = window.event;
		if (e.target) 
			t = e.target;
		else 
			t = e.srcElement;
		if (t.id == 'roadside')
			shoot(getOffsets(e));
	}
}

// pauses animation when over an ad
var lastCur = 0;
var gameOn = 0;		// set this to stop this.. 
function adOut(){
	if (lastCur && !curTimer && !gameOn){
		curTimer = window.setInterval("timerHandler()",timeoutPeriod);
	}
	lastCur = 0;
}

function adIn(){
	if (curTimer && !gameOn){
		window.clearInterval(curTimer);
		curTimer = null;
		lastCur = 1;
	}
}

// set this if we are trying to go to the previous or next ad
var movingStatically = 0;

// go to previous, or next ad --- NOT airplanes... 
function gotoAd(dir){
	var oldDirection = bbDirection;
	
	if (curTimer){
		switchDriving();
	}
		
	bbDirection = dir;
	movingStatically = 1;
	var cnt = 0;
	
	while (movingStatically && cnt < 150){
		timerHandler();
		cnt += 1;
	}
	
	bbDirection = oldDirection;
}

function checkCnt(cnt,type){
	if (type < 2 && cnt == 115){
		movingStatically = 0;
	}
}

// unique item 
function uObject(){
	this._next = 0;
	this.next = function(){
		this._next += 1;
		return this._next;
	};
}

// fixme: make motion smoother... 
function setSpeed(speed){
	switch (speed){
		case 1:
			timeoutPeriod = 200;
			break;
		case 2:
			timeoutPeriod = 100;
			break;
		case 3:
			timeoutPeriod = 85;
			break;
		case 4:
			timeoutPeriod = 66;
			break;
		case 5:
			timeoutPeriod = 50;
			break;
	}
	
	if (curTimer){
		clearInterval(curTimer);
		curTimer = setInterval("timerHandler()",timeoutPeriod);
	}
}

// utility functions
function getOffsets (e) {
	// for firefox/other compatibility
	if (typeof e.offsetX == 'undefined'){
		var target = e.target;
		if (typeof target.offsetLeft == 'undefined') {
			target = target.parentNode;
		}
		var pageCoords = getPageCoords(target);
		
		return {
			offsetX: window.pageXOffset + e.clientX - pageCoords.x,
			offsetY: window.pageYOffset + e.clientY - pageCoords.y
		}
	}
	
	// IE/opera/safari does this for us :)
	return {
		offsetX: e.offsetX,
		offsetY: e.offsetY
	}
}

function getPageCoords (element) {
  var coords = {x : 0, y : 0};
  while (element) {
    coords.x += element.offsetLeft;
    coords.y += element.offsetTop;
    element = element.offsetParent;
  }
  return coords;
}

/*
function debug(stuff){
	document.getElementById('debug').innerHTML += stuff + '<br>';
}*/

var unique = new uObject();
var animationList = new llItem(null,null,null);
var animationQueue = null;
var curTimer = null;

