/*
	(C)2006 Dustin Spicuzza

	Animation stuff -- I'm not even going to bother supporting old browsers atm
*/


// left = 0, right = 1, airplane = 2, center = 3, boom = 4
var objectTypes = 5;

var bbDirection = 1;
var syncCount = 0;			// synchronization counter

var bbCaption = ['adcaptionl','adcaptionr'];

// start positions
var bbStartX = [ 370, 390, 0, 380];
var bbStartY = [ 196, 196, 90, 210];

var waitingForDual = [0,0];
var curDual = [0, 0];
var inSeq = [0, 0];
var curSeq = [0, 0];

// move right  edge: t/f
var bbMoveEdge = [ 1, 0, 1, 0];

// [type] [idx] [info]
	// type: 0- objectTypes-1
	// idx: 0-149
	//  info: [0]: multiplier , [1]: right offset, [2]: top offset
var paths = new Array();

// initialize objects -- is this necessary?
for (var i = 0;i<objectTypes;i++){
	paths[i] = new Array();
}

// initialize paths
for (i = 0; i < 150; i++){

	var d = (150 - i) / 13;
	// piecewise function
	var r = i <= 40 ?(i * i) / 7500 : 2 / d;
	// ad path
	paths[0][i] = [r, -300/d, 109/d];
	paths[1][i] = [r, 300/d, 109/d ];
	
	// airplane path
	paths[2][i] = [(i/15)/8 , (1050/150)*i, 0 ];
	// center line path
	paths[3][i] = [i/80, 0, (i/80)*4*i];
	
	// path of other objects -- ok, there is now a mechanism to calculate all this
}

// primary state machine
function timerHandler(){

	var parentNode = animationList;
	var curNode = parentNode.next;

	// animate all objects sitting in the queue
	while(curNode){
		
		if (animate(curNode)){
			
			// remove the item from the document tree...
			removeImg(curNode.id);
			
			// move linked list around
			parentNode.next = curNode.next;
			// delete stuff!
			delete curNode.eNode;
			delete curNode;
			curNode = parentNode.next;
			
		}else{
			
			parentNode = curNode;
			curNode = curNode.next;
		}
	}
	
	syncCount += 1;
	if (syncCount >= 150){ 
		syncCount = 0;
	}
	
	var r = 1;
	if (gameOn){
		r = parseInt(curScore/140000);
		!r ? r = 1 : r < 7 ? r = r : r = 7;
	}
	
	// center line
	if (!(syncCount % 15)){
		addItem(center,3);
	}
	
	if (!((syncCount - 36) % parseInt(74/r)))
		selectNextBB(0);
		
	if (!((syncCount + 75) % parseInt(74/r)))
		selectNextBB(1);
		
	if (!((syncCount + 149) % parseInt(150/r))){
		selectNextBB(2);
	}

	// add anything in the queue to the 'real' list now..
	if (animationQueue){
		var n = animationQueue;
		var ln = null;
		do{
			ln = n;
			n = n.next;
		}while(n);
		
		ln.next = animationList.next;
		animationList.next = animationQueue;
		animationQueue = null;
	}
}

// this is a sick, twisted piece of code... 
function selectNextBB(type){
	var next;
	var aType;
	
	//debug ("Checking next: " + curBB + ", type: " + type);
	if (type < 2){
	
		if (BB.length < 1)
			return;
	
		// if we're in a sequence
		if (inSeq[type]){
			// check next item
			next = getNextBB(curSeq[type]);
			//debug('Already in seq: ' + curBB + ', type: ' + type + ', curDual: ' + curDual[type]);
			// add current item
			addItem(BB[curSeq[type]],type);
			// no changes
			if (curBB == curSeq[type]){
				curBB += 1;
			}
				
			// if same sequence, keep it
			if (BB[next].isSeq == inSeq[type]){
				curSeq[type] = next;
			}else{
				inSeq[type] = 0;
			}
				
		// waiting for the next dual
		}else if (waitingForDual[type]){
			
			if (bbDirection == 1){
				addItem(BB[curDual[type]],0);
				addItem(BB[curDual[type]+1],1);
			}else{
				addItem(BB[curDual[type]-1],0);
				addItem(BB[curDual[type]],1);
			}
			
			waitingForDual[type] = 0;
			curBB = getNextBB(curBB);
		
		// show next BB
		}else{
			next = getNextBB(curBB);
		
			if (BB[curBB].isSeq){
				//debug("Found sequence: " + curBB + ", type: " + type);
				aType = type ? 0 : 1;
				
				// if they're the same sequence, ignore them
				if (inSeq[aType] == BB[curBB].isSeq){
					
					var cnt = 0;	// avoid infinite loops
					while (BB[next].isSeq == inSeq[aType] && ++cnt < BB.length){
						next = getNextBB(next);
					}
					
					// set this
					curBB = next;
					
					if (cnt == BB.length){
						return;		// error, don't display anything
					}
						
					selectNextBB(type);
				
					// restore order
					next = curBB;
				
				}else{
					
					// new sequence!
					inSeq[type] = BB[curBB].isSeq;		// assign unique id
					curSeq[type] = next;
					
					addItem(BB[curBB],type);
				}
				
			}else if (BB[curBB].isDual){
				
				aType = type ? 0 : 1;
				if (waitingForDual[aType]){
					// this should always hold true
					next = curBB;
				}else{
					waitingForDual[aType] = 1;
					curDual[aType] = curBB;
					// always true
				}

			}else{
				// too easy
				addItem(BB[curBB],type);
			}
			
			curBB = next;
		}
	
	}else if (type == 2){
	
		if (air.length < 1)
			return;
	
		// show next airplane
		addItem(air[curAir],type);
		
		curAir = curAir + bbDirection;
		if (bbDirection == 1 && curAir == air.length){
			curAir = 0;
		}else if (bbDirection == -1 && curAir < 0){
			curAir = air.length - 1; 
		}
	
	}
	
	//debug ("Exiting: " + curBB + ", type: " + type);
}

function getNextBB(item){
	item = item + bbDirection;
	if (bbDirection == 1 && item == BB.length){
		return 0;
	}else if (bbDirection == -1 && item < 0){
		return BB.length - 1;
	}
		
	return item;
}

// pass it an animation object to animate
function animate(node){

	if (!node || !node.item){
		return 0;
	}
	
	var item = node.item;
	
	if (!node.divCreated){
		item.loadImg();
		addImg(node.id,item.src,item.title,item.href,item.isPNG);
		node.divCreated = 1;
	}
	
	// grab these objects
	var adItem = document.getElementById(node.id);
	var adImg = document.getElementById(node.imgId);
	
	if (item.isPNG && fixIE && adImg)
		adImg = adImg.style;

	// move and resize the item if it happens to exist
	if (adItem && adImg){
	
		// update the animation parameters.. 
		node.update();
	
		// do it
		adImg.width = node.w;
		adImg.height = node.h;
		adItem.style.left = node.l + 'px';
		adItem.style.top = node.t + 'px';

		// adjust the z-index
		adItem.style.zIndex = node.zIndex;
	}
	
	// do a specific action to the element
	var ret = node.doAction();
	if (ret) return ret;
		
	if (adItem.boom){

		document.getElementById('boomright').innerHTML = 
			'<h5><a href="' + item.href + '">' + item.title + '</a></h5>' + item.description;

		// should we explode an item?
		if (adItem.boom == 1){
			return node.explode(adItem.boomX,adItem.boomY);
		}else if(adItem.boom == -1){
			// remove element
			return 2;
		}
	}
	
	// check dimensions just in case
	if (node.l > 750 || (node.l + node.w) < 0){
		return 1;
	}
	
	if (movingStatically){
		checkCnt(node.count, node.type);
	}
	
	return 0;
}

// remove item from document tree... no sense keeping it there if its not being used
function removeImg(id){

	if (!id){
		return;
	}
	
	// remove passed node's children
	var node = document.getElementById(id);
	if (node){
		while (node.hasChildNodes()){
			node.removeChild(node.firstChild);
		}
		
		// remove passed node
		node.parentNode.removeChild(node);
		
	}	
}

function addToQueue(item){
	item.next = animationQueue;
	animationQueue = item;
}

function addItem(item,type){
	addToQueue(new llItem(item,type));
}

// add a BB to the array in an easy manner
function initBB(type,item){
	switch (type){
		case "1":
		case 1:
			BB[BB.length] = item;
			break;
		case "2":
		case 2:
			air[air.length] = item;
			break;
	}
}

// blow up all objects -- add huge mushroom cloud
function nuke(){
	var c = curScore;
	var parentNode = animationList;
	var curNode = parentNode.next;
	var o;
	while(curNode){
		if (curNode.type != 4){
			curNode.detonate(curNode.w/2,curNode.h/2);
		}
		parentNode = curNode;
		curNode = curNode.next;
	}
	updateScore(c - curScore);
}






