
var util=new Util();

World.implement({
    checkForCollisionWithWalls: function(dynaObject,newX,newY,direction){
        var hit=false;
        
        /////////// this is the check for the case when i go up or left
        var currentZone = null;
        var currentZoneFreeZone = true;
        
        if((newX<=dynaObject.x && newY==dynaObject.y) || (newX==dynaObject.x && newY<=dynaObject.y)){
	        currentZone = util.getZoneAtPoint(newX,newY);
	        if($defined(currentZone)){
		        currentZoneFreeZone = currentZone.isWalkable();
		        if(currentZoneFreeZone == false){
		              if(dynaObject.myZone==currentZone)
		                  currentZoneFreeZone=true;
		        }
	        }
        }
        //////////////////////// UP LEFT END
        
        /////////////////// RIGHT or DOWN
        var nextZone=null;
        var nextZoneFreeZone = true;
        
        ///////////////// the check for the case when moving right
        if(direction=='left'){
            if(newX>dynaObject.x){ // it means the object goes right, so it makes sense to check
              nextZone=util.getZoneAtPoint(newX+game.standardWidth,newY);
              if($defined(nextZone)){
                nextZoneFreeZone = nextZone.isWalkable();
                /*if(!nextZoneFreeZone){
                    if(nextZone.hasType(dynaObject.type))
                        nextZoneFreeZone=true;
                  }*/
              }
            } else {
                nextZoneFreeZone=true;
            }
        }
        ///////////////// the check for the case when moving down
        if(direction=='top'){
          if(newY>dynaObject.y){// it means the object goes down, so it makes sense to check
            nextZone=util.getZoneAtPoint(newX,newY+game.standardHeight);
            if($defined(nextZone)){
                nextZoneFreeZone = nextZone.isWalkable();
                /*if(!nextZoneFreeZone){
                    if(nextZone.hasType(dynaObject.type))
                        nextZoneFreeZone=true;
                }*/
            }
          } else {
              nextZoneFreeZone=true;
          }
        }
        
        ////////////// RIGHT or DOWN END
        if(!(currentZoneFreeZone && nextZoneFreeZone)){
            hit=true;
            if(dynaObject.type==GAMEUNITTYPE.HERO){
                var movedIntozone = currentZone==null ? nextZone : currentZone;
                if(movedIntozone.hasType(GAMEUNITTYPE.MONSTER)){
                    var badguys = movedIntozone.getElements();
                    for(var i=0;i<badguys.length;i++){
                      if(badguys[i].type==GAMEUNITTYPE.MONSTER)
                          badguys[i].killTheHero();
                          break;
                    }
                  hit=false;
                }
            }
        }
        else{
            /// ideally the code below inside this else should be inside hero class
          /// i put this here, because of performance reasons [i already have the zones computed]
          if(dynaObject.type==GAMEUNITTYPE.HERO){
              var movedIntozone = currentZone==null ? nextZone : currentZone;
              if(movedIntozone.hasType(GAMEUNITTYPE.GOODY))
                  dynaObject.assimilateGoody(movedIntozone);
          }
        }
        return hit;
    }
  });


function keyPressedInputForwarder(e){
    if(window.event){
            //e.cancelBubble is supported by IE - this will kill the bubbling process.
            window.event.cancelBubble = true;
            window.event.returnValue = false;
		}
		else{
            //e.stopPropagation works only in Firefox.
            if (e.stopPropagation) {
                e.stopPropagation();
                e.preventDefault();
            }
	    }
    if(!$defined(game.heart))
        return;
    var keyCode=window.event ? event.keyCode : e.keyCode;
    if(keyCode>=37 && keyCode<=40){
        if($defined(game.hero))
              game.hero.processKeyPressedInput(keyCode);
    } else {
        if(keyCode != 27)
            keyCode = window.event ? keyCode : e.charCode;
        if(keyCode==112 || keyCode==80 || keyCode==27){
            game.pause = game.pause ? false : true;
            if(!game.pause){
                worldbeat();
            }
        }
    }
}
function keyUpInputForwarder(e){
    if(!$defined(game.heart))
            return;
    var keyCode=window.event ? event.keyCode : e.keyCode;
    if(keyCode == 32){
    } else{
        if($defined(game.hero)){
            if(parseInt(keyCode)>=37 && parseInt(keyCode)<=40){
                  game.hero.processKeyUpInput(keyCode);
            }
        }
    }
}
function keyDownInputForwarder(e){
    if(!$defined(game.heart))
        return;
    var keyCode=window.event ? (event.charCode || event.keyCode) : (e.charCode || e.keyCode);
    if(keyCode==32){
                if($defined(game.hero)){
                if($defined(game.lastKeyCodeSentToServer))
                    sendInfoToServer('info=I:'+keyCode+'&'+computeVerificationStatus());
                game.hero.launchBomb();
        }
	if(window.event){
          //e.cancelBubble is supported by IE - this will kill the bubbling process.
          window.event.cancelBubble = true;
          window.event.returnValue = false;
	}else{
          //e.stopPropagation works only in Firefox.
          if (e.stopPropagation) {
              e.stopPropagation();
              e.preventDefault();
          }
	  }
    } else{
        if($defined(game.hero)){
            if(parseInt(keyCode)>=37 && parseInt(keyCode)<=40){
                  game.hero.processKeyDownInput(keyCode);
            }
        }
    }
}

function worldbeat(){
    //if(game.stateCounter>100 && game.stateCounter<105)
   //     console.profile("gabi");
   if(game.pause)
       return;
    if(game.stateCounter<5) // this if is a hack, when changing levels, it seems worldbeat gets called more than once and the speed of the game increases,like 2 parallel threads of animating
        clearTimeout(game.heart);
    game.stateCounter++;
    var heartBeatTime = (new Date()).getTime();
    if($defined(game.hero))
      game.hero.moveAlongDirection();
  else
      return;
  var others = game.heroes;
  var othersLength = others.length;
    for(var i=0;i<othersLength;i++){
        if(game.stateCounter>0)
         others[i].moveAlongDirection();
    }
    checkGlobalPosition();
    if(game.debug==1)
        debug(heartBeatTime);
    var nextCall = game.speed - ((new Date()).getTime()-heartBeatTime);
    if(nextCall<=0)
        nextCall=2;
    //if(nextCall<game.speed-5)
    //    console.log("nextCall = "+nextCall);
    if($defined(game.heart) && $defined(game.hero)){
        game.heart=setTimeout("worldbeat()",nextCall);
    }
}


// if the effect called by world.move* methods is still in action
// we shouldnot call this function again
// so an if should be in place in worldBeat function, on a global variable
//
function checkGlobalPosition() {
    if(!$defined(game.hero))
        return;
    if(game.hero.x+game.standardWidth+game.world.x>game.world.worldWindow.width-(game.standardWidth+10) && game.world.x+game.world.width>game.world.worldWindow.width){
          game.world.moveLeft();
          return;
    }
    if(game.hero.x+game.world.x<game.standardWidth+10 && game.hero.x>0){
        game.world.moveRight();
        return;
    }
    if(game.hero.y+game.standardHeight+game.world.y>game.world.worldWindow.height-(game.standardHeight+10)){
          game.world.moveUp();
          return;
    }
    if(game.hero.y+game.world.y<(game.standardHeight+10)){
        game.world.moveDown();
        return;
    }
}

document.onkeypress=keyPressedInputForwarder;
document.onkeyup=keyUpInputForwarder;
document.onkeydown=keyDownInputForwarder;

