/*
 *	JS-Game-Lib
 *
 *	Author: Sandro Wolf, Generation Digitale (s.wolf@generationdigitale.net)
 *	Created: 25.08.2006
 */

/**
 * 	GameApplication (GA)
 */
function GameApp(win,name){
	this.parent=win;
	if(name) this.name=name; else this.name="GA";
	this.Containers=new Array();
	this.GS=new GameScreens(this);
	this.LD=new LevelData(this);
	this.GB=new Array();
	this.FPS=25;	//25 bilder per sek.
	this.Lang="en";
	this.Trans=new Array();
	this.GameStates=new Array();
	this.GameStates["Initial"]="Initial";
	this.GameStates["Settings"]="Settings";
	this.GameStates["Help"]="Help";
	this.GameStates["GameLevelStart"]="GameLevelStart";
	this.GameStates["GameLevelPlay"]="GameLevelPlay";
	this.GameStates["GameLevelPause"]="GameLevelPause";
	this.GameStates["GameLevelSucceed"]="GameLevelSucceed";
	this.GameStates["GameOver"]="GameOver";
	this.GameStates["GameOverSucceed"]="GameOverSucceed";
	this.GameState=this.GameStates["Initial"];
	this.OldGameState=this.GameState;
	onLoadObj[onLoadObj.length]=this.name+".__initInitial()";
}


GameApp.prototype.__initInitial=function(){
	this.initInitial();
	this.__register();
	this.__init();
	this.showState();
}

GameApp.prototype.__register=function(){
	this.GS.register();
	this.register();
}

GameApp.prototype.__init=function(){
	this.gameLoopIntervall=null;
	this.init();
}


GameApp.prototype.initInitial=function(){
// OVERWRITE!
}

GameApp.prototype.register=function(){
//OVERWRITE!
}

GameApp.prototype.init=function(){
//OVERWRITE!
}


GameApp.prototype.showState=function(toState,keepOldScreen){
	this.OldGameState=this.GameState;
	if(toState){
		this.GameState=toState;
	}
	this.GS.showScreen(this.GameState,keepOldScreen);
}


GameApp.prototype.addBoardEl=function(elName){
	this.GB[elName]=new GameBoard(this);
	var gb = this.parent.document.getElementById(elName);
	this.GB[elName].registerEl(gb);
}

GameApp.prototype.addContainer=function(elName){
	this.Containers[elName] = this.parent.document.getElementById(elName);

}


GameApp.prototype.initLevel=function(){
	this.FrameCount=-1;
	this.Moves=0;
	this.timeElapsed=0;
	this.LD.init();
}


GameApp.prototype.start=function(){
	if(this.GameState==this.GameStates["Initial"] || this.GameState==this.GameStates["GameOver"] || this.GameState==this.GameStates["GameOverSucceed"]){
		this.init();
		this.showState("Settings");
	}else{
		if(this.GameState==this.GameStates["Settings"] || this.GameState==this.GameStates["GameLevelSucceed"]){
			this.initLevel();
			this.showState("GameLevelPlay");
			this.showState("GameLevelStart",true);
		}else{
			if(this.GameState==this.GameStates["GameLevelStart"]){
				this.showState("GameLevelPlay");
				this.__GameLoop();
			}else{
		
			}		
		}
	}
}

GameApp.prototype.quit=function(){
	if(this.GameState==this.GameStates["GameLevelPause"]){
		this.switchPause();
	}else{
		this.__clearGameLoop();
		this.LD.reset();
		this.showState("Initial");
	}
}


GameApp.prototype.__clearGameLoop=function(){
	if(this.gameLoopIntervall!=null) {
		clearTimeout(this.gameLoopIntervall);
		this.gameLoopIntervall=null;
	}
}

GameApp.prototype.__repeatGameLoop=function(){
	if(this.gameLoopIntervall==null) this.gameLoopIntervall=window.setTimeout(this.name+".__GameLoop();", Math.ceil(1000/this.FPS));
}

GameApp.prototype.switchPause=function(){
	if(this.GameState==this.GameStates["GameLevelPause"]){
			this.showState(this.OldGameState);
			if(this.GameState==this.GameStates["GameLevelPlay"]) this.__repeatGameLoop(); 
	}else{
		if(this.GameState==this.GameStates["GameLevelPlay"]) this.__clearGameLoop(); 
		this.showState(this.GameStates["GameLevelPause"],true);
	}
}

GameApp.prototype.switchHelp=function(){
	if(this.GameState==this.GameStates["Help"]){
			this.showState(this.OldGameState);
			if(this.GameState==this.GameStates["GameLevelPlay"]) this.__repeatGameLoop(); 
	}else{
		if(this.GameState==this.GameStates["GameLevelPlay"]) this.__clearGameLoop(); 
		this.showState(this.GameStates["Help"],true);

	}
}


GameApp.prototype.__GameLoop=function(){
	if(this.GameState==this.GameStates["GameLevelPlay"]){	// is paused?
		this.FrameCount++;
		this.timeElapsed=this.FrameCount*1000/this.FPS;
		this.checkOverCondition();
		if(this.GameState==this.GameStates["GameLevelPlay"]){
			this.onGameLoop();
			this.__clearGameLoop();
			this.__repeatGameLoop();
		}else{
			this.__clearGameLoop();
			if(this.GameState==this.GameStates["GameLevelSucceed"]){
				
			}else{
				if(this.GameState==this.GameStates["GameOver"]){
					this.onGameOver();
				}else{
					if(this.GameState==this.GameStates["GameOverSucceed"]){
						
					}
				}
			}
			this.showState(this.GameState);
		}
	}

}




/**
 * 	GameScreens (GS)
 */
function GameScreens(app){
	this.parent=app;
	this.classScreen="screen";
	this.classScreenVis="screenVis";
	this.active=null;
	this.screens=new Array();
}



GameScreens.prototype.addScreen=function(state, elName){
	this.screens[state]=elName;
}

GameScreens.prototype.register=function(){
	var newScreens=new Array();
	for(x in this.screens){
		newScreens[x]=this.parent.parent.document.getElementById(this.screens[x]);
	}
	this.screens=newScreens;
}

GameScreens.prototype.showScreen=function(name, dontInactive){
	if(!dontInactive && this.active)
		switchStyleClass(this.screens[this.active],this.classScreen);
	this.__onShow(name);
	switchStyleClass(this.screens[name],this.classScreenVis);	

	this.active=name;

}

GameScreens.prototype.__onShow=function(scrName){
	this.onShow(scrName);
}

GameScreens.prototype.onShow=function(scrName){
	//OVERWRITE
}


/**
 * 	LEVELDATA (LD)
 */
function LevelData(app){
	this.parent=app;
	this.Levels=new Array();
	this.Tiles=new Array();

	this.reset();
}

LevelData.prototype.setNext=function(){
	if(this.Levels.length>this.current+1){
		this.current++;
		return true;
	}else return false;
}

LevelData.prototype.reset=function(){
	this.current=-1;
}

LevelData.prototype.addLevel=function(lvl){
	this.Levels[this.Levels.length]=lvl;
	lvl.parent=this;
	return lvl;
}

LevelData.prototype.init=function(){
	if(this.current==-1){
		this.initInitial();
	}
	this.Levels[this.current].init();
}

LevelData.prototype.initInitial=function(){
	//OVERWRITE
}

LevelData.prototype.getCurrent=function(){
	return this.Levels[this.current];
}

/**
 * 	LEVEL
 */
function Level(){
	this.parent=null;
	this.GBName=null;
	this.GBCols=1;
	this.GBRows=1;
	this.TilesData=new Array();
}

Level.prototype.addTile=function(tile){
	this.TilesData[this.TilesData.length]=tile;
	tile.parent=this;
	return tile;
}


Level.prototype.setBoard=function(boardName,c,r){
	this.GBName=boardName;
	this.GBCols=c;
	this.GBRows=r;
}

Level.prototype.init=function(){
	for(i in this.parent.parent.GB)
		this.parent.parent.GB[i].hide();
	
	this.parent.parent.GB[this.GBName].setDim(this.GBCols,this.GBRows);
	this.parent.parent.GB[this.GBName].show();
	this.parent.parent.GB[this.GBName].initLevel(this);
}


/**
 * 	Gameboard (GB)
 */
function GameBoard(app){
	this.parent=app;
		this.cols=0;
		this.rows=0;
	this.fields=new Array();

	this.el=null;
}

GameBoard.prototype.init=function(){
	
}

GameBoard.prototype.hide=function(){
	this.el.style.display="none";
}

GameBoard.prototype.show=function(){
	this.el.style.display="block";
}

GameBoard.prototype.setDim=function(cols,rows){
	this.cols=cols;
	this.rows=rows;
}

GameBoard.prototype.registerEl=function(el){
	this.el=el;
	this.fields=new Array((this.cols*this.rows));
	var cnts=getElementsByAttributeValue("class", "GB_EleContainer", "div", el, false);
	for(cnt in cnts){
		this.fields[this.fields.length] = new GBField(this, cnts[cnt],this.fields.length);
	}
}

GameBoard.prototype.initLevel=function(objLevel){
	// OVERWRITE!!
}

GameBoard.prototype.setFieldTile=function(fNo,tileObj){
	this.fields[i].__setTile(tileObj);
}

GameBoard.prototype.switchFieldTiles=function(f1,f2){
	var tmp=f1.tile;
	f1.__setTile(f2.tile);
	f2.__setTile(tmp);
}


/**
 * 	GB-FIELDS (Containers for Tiles)
 */
function GBField(gb,el,idx){
	this.parent=gb;
	this.idx=idx;
	this.PX=0;
	this.PY=0;
	this.tile=null;
	if(el) this.registerEl(el);
}

GBField.prototype.getAbsPosition=function(){
	// nijreiwjrefw-....................
}

GBField.prototype.registerEl=function(el){
	this.el=el;
		AddRefHandler ("onmousedown", GBF_onMDown, this, el);
		AddRefHandler ("onmouseup", GBF_onMUp, this, el);
		AddRefHandler ("onmouseover", GBF_onMOver, this, el);
		AddRefHandler ("onmouseout", GBF_onMOut, this, el);
}


GBField.prototype.__setTile=function(tileObj){
	this.tile=tileObj;
	this.setTile(tileObj);
}

GBField.prototype.setTile=function(tileObj){
//OVERWRITE!!!
}


function GBF_onMUp(obj,even){
	//dbWrite(even.which);
	if(even.which==1 || even.which==5 || even.button==1 || even.button==5){
		return obj.MouseUp(even);
	}
}

function GBF_onMDown(obj,even){
	//dbWrite(even.which);
	if(even.which==1 || even.which==5 || even.button==1 || even.button==5){
		return obj.MouseDown(even);
	}
}

function GBF_onMOver(obj,even){
	return obj.MouseOver(even);
}

function GBF_onMOut(obj,even){
	return obj.MouseOut(even);
}



/**
 * 	GB-TILES
 */
function GBTile(data,el){
	this.data=data;
	this.parent=null;
	if(el) this.registerEl(el);
}



GBTile.prototype.registerEl=function(el){
	this.el=el;
//	AddRefHandler ("onmousedown", GBT_onMDown, this, el);
//	AddRefHandler ("onmouseup", GBT_onMUp, this, el);
}
/*
GBTile.prototype.MouseDown=function(ev){
	dbInspect(this, "mouseDown: ");	
}

GBTile.prototype.MouseUp=function(ev){
	dbInspect(this,"mouseUp: ");
}

function GBT_onMDown(obj,even){
	//dbWrite(even.which);
	if(even.which==1 || even.which==5 || even.button==1 || even.button==5){
		//dbWrite(even.type);
		if(even.type=='mousedown'){
			obj.MouseDown(even);
		}
	}
}

function GBT_onMUp(obj,even){
	//dbWrite(even.which);
	if(even.which==1 || even.which==5 || even.button==1 || even.button==5){
		//dbWrite(even.type);
		if(even.type=='mouseup'){
			obj.MouseUp(even);
		}
	}
}

*/

