// Collision detection class for two rectangle object
// doesnt really work.. shit..
function Collision(pObj1,pObj2){
	if(((pObj1.x>=pObj2.x&&pObj1.x<pObj2.x+pObj2.width)||(pObj1.x+pObj1.width>=pObj2.x&&pObj1.x+pObj1.width<pObj2.x+pObj2.width))&&((pObj1.y>=pObj2.y&&pObj1.y<pObj2.y+pObj2.height)||(pObj1.y+pObj1.height>=pObj2.y&&pObj1.y+pObj1.height<pObj2.y+pObj2.height))){
		return true;
	}
}

//Add an image to the screen, with a unique id based on div, and id
function AddImageToGame(pDivRef, pFileName, pScreenId, pStartX, pStartY){
	pDivRef.innerHTML+="<img src="+kImageFolder+pFileName+" id="+pScreenId+" style='position:absolute;left:"+pStartX+";top:"+pStartY+";'>";
}

function DrawString(pWidth, pY, pCol, pString, pDivRef){
	tmpString = "<div style='position:absolute;top:"+pY+";width:"+pWidth+";color:"+pCol+";";
	tmpString +="text-align:center;";
	tmpString += "'>"+pString+"</div>";
	if( typeof pDivRef == "string")
	{
		pDivRef = $(pDivRef);
	}
	pDivRef.innerHTML= tmpString;
}

function $( id )
{
	return document.getElementById( id );
}

//TextFader class - fade up text on a div.
function TextFader(pX, pY, pHold, pRepeat, pTextArray, pDivRef){
	this.div = pDivRef;
	this.x = pX;
	this.y = pY;
	this.hold = pHold;	
	this.dir;
	this.repeat = pRepeat;
	this.textArray = pTextArray;
 	this.fadeArray = new Array("#000000","#333333","#777777","#999999","#CCCCCC","#DDDDDD","#EEEEEE","#FFFFFF");
	
	this.counter1;
	this.curText;
	
	this.start = function(){
		this.counter = 0;
		this.curText = 0;
		this.dir=1;
		this.text = this.textArray[this.curText];
		this.draw();
	}

	this.draw = function(){
		if(this.dir==1)
			DrawString(this.x,this.y,this.fadeArray[this.counter], this.text, this.div);
		//else
			//DrawString(this.x,this.y,this.fadeArray[(this.counter/2)-this.fadeArray.length-1], this.text, this.div);
	}

	this.update = function(){
		if(this.counter<this.fadeArray.length){
			this.draw();
		}
		if(this.counter>this.hold){
			if(++this.curText<this.textArray.length){
				this.counter=0;
				this.text = this.textArray[this.curText];
				this.draw();
			}
		}
		this.counter++;
	}
	
	this.stop = function(){
		DrawString(0,0,"#000000", "", this.div);
	}
}

function KeyHandler(pGameObject,e){
	if(!e)var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	
   	key = e.which ? e.which : e.keyCode;
	if(pGameObject.status!=3){
		if(pGameObject.status==0){
			if(key!=116)
				pGameObject.status=1;
		}
	}
	else{
		//alert(key);
		switch(key){
		      	case 68:
				pGameObject.keyRIGHT();
				break;
			case 65:
				pGameObject.keyLEFT();
				break;
		      	case 87:
				pGameObject.keyUP();
				break;
				case 83:
				pGameObject.keyDOWN();
				break;
			case 75:
				pGameObject.keySPACE();
				break;
			case 76:
				pGameObject.keyRIGHT_CTRL();
				break;
		}
	}
}

/*
function Frames(pNumFrames, pFileName, pFileExt){
	var preloadImages=new Array(pNumFrames);

	for (p=0;p<pNumFrames;p++){
		preloadImages[p]=new Image();
		preloadImages[p].src=kImageFolder+pFilesName+"."+pFileExt;	
	}
	
	return preloadImages;
}
*/