//Black Curse Game Core Javascript classes & functions
//Copyright 2004 by Shawn Olson
//All Rights Reserved
//For more information, go to http://www.shawnolson.net

//begin game objects
function theGame(players,difficulty){
 //properties
 this.gameStart = new Date();
 this.players = players;
 this.difficulty = difficulty;
 this.paused = false;
 this.switchDifficulty = switchDifficulty;
 this.displayMode = 'graphic';
 this.chooseDifficulty = chooseDifficulty;
 if(this.players == null){this.players = 1;}
 if(this.difficulty == null){this.difficulty = 'medium';}
 
 //methods
 this.switchMode = switchMode; 
}

function chooseDifficulty(){
 document.getElementById("moveButton").innerHTML='<h4>Choose Difficulty</h4><button onclick="theGame.switchDifficulty(\'easy\');">Easy</button><button onclick="theGame.switchDifficulty(\'medium\');">Medium</button><button onclick="theGame.switchDifficulty(\'hard\');">Hard</button><button onclick="theGame.switchDifficulty(\'expert\');">Expert</button>';
}

function switchDifficulty(theDifficulty){
 document.getElementById("content").innerHTML = map;
 switch(theDifficulty){
 
 case 'easy':
  this.bonus = -1;
  this.curseLimit = Math.round((theMap.mapX * theMap.mapY)/15);
  this.gemFactor = 6;
  this.pointFactor = 20;
 break;
 
 case  'medium':
  this.bonus = 0;
  this.curseLimit = Math.round((theMap.mapX * theMap.mapY)/12);
  this.gemFactor = 12;
  this.pointFactor = 40;
 break;
 
 case  'hard':
  this.bonus = 1;
  this.curseLimit = Math.round((theMap.mapX * theMap.mapY)/10);
    this.gemFactor = 18;
  this.pointFactor = 50;
 break;
 
 case 'expert':
  this.bonus = 2;
  this.curseLimit = Math.round((theMap.mapX * theMap.mapY)/8);
  this.gemFactor = 20;
  this.pointFactor = 100;
 break;
 
 }
 startGame()

}

function switchMode(){

  switch(this.displayMode){
  
   case 'text':
    THEdisplayMode='graphic';
	break;
	
   case 'graphic':
    THEdisplayMode='text';
	break;
	
   default:
    THEdisplayMode='graphic';
   break;
  }
  this.displayMode = THEdisplayMode;
  theMap.renderMap();
}


function gridObject(type){

 //properties
 this.type = type;
 this.src = this.type;

 //methods
 this.gridObjectRender = gridObjectRender;
 
 switch(this.type){
 
 case 'X':
   this.name = 'Wall';
   this.effect = 'Walls impede your path. Luckily... your natural magic often allows you to go through these entities.';
   this.gameType = 'entity';
 break;
 
 case 'E':
   this.name = 'Energy';
   this.effect = 'This Item will give you Energy';
   this.gameType = 'item';
 break;
 
 case  'C':
   this.name = 'Money';
   this.effect = 'Money will increase your potential to survive.';
   this.gameType = 'item';
 break;
 
 case 'T':
   this.name = 'Treasure';
   this.effect = 'Treasure is abundant in this realm. Just be warned that it sometimes offers an unhappy reward.';
   this.gameType = 'item';
 break;
 
 case  'V':
   this.name = 'Shard';
   this.effect = 'Magical Shards are the purpose of your quest. Collect these to help defeat the Dark Ones.';
   this.gameType = 'item';
 break; 
 case  'Y':
   this.name = 'Points';
   this.effect = 'As your experience increases, so shall your power.';
   this.gameType = 'item';
 break;
 
 case  'W':
   this.name = 'Worm';
   this.effect = 'Worms are hatchling dragons.';
   this.gameType = 'monster';
 break;
 
 case  'G':
   this.name = 'Guard';
   this.effect = 'Guards are numerous throughout the cursed realm. They will fight to the death.';
   this.gameType = 'monster';
 break; 
 
 case  'S':
   this.name = 'Centurion';
   this.effect = 'Centurions are powerful warriors that pledge allegiance to the Dark Ones.';
   this.gameType = 'monster';
 break;
 
 case  'D':
   this.name = 'Dragon';
   this.effect = 'Dragons are ferocious creatures. Avoid them at all cost.';
   this.gameType = 'monster';
 break;
 
 case  'P':
   this.name = 'Player';
   this.effect = 'The icon representing your place on the board.';
   this.gameType = 'entity';
 break;
 
 case  'O':
   this.name = 'Origin';
   this.effect = 'This is the place from which you enter a board.';
   this.gameType = 'entity';
 break;
 
 case 'L':
   this.name = 'Exit';
   this.effect = 'This is the portal to the next level.';
   this.gameType = 'entity';
 break;
 
 case 'M':
   this.name = 'Merchant';
   this.effect = 'Merchants will seel you items to help you on your quest.';
   this.gameType = 'entity';
 break;
 
 default:
 this.name = null;
 this.effect=null;
 this.gameType = null;
 
 }
}

function gridObjectRender(){
   if(theGame.displayMode=='graphic' && this.name != null){
     this.src = '<img class=icon src=/bc_media/images/bc_icon_'+this.type+'.gif alt='+this.name+' />';
   }
   if(theGame.displayMode=='text' || this.name == null){
     this.src = this.type;
   }
 
 return this.src;
}

function mapVars(mapId,mapX,mapY,mapOriginX,mapOriginY,mapName){
  this.mapId = mapId;
  this.mapX = mapX;
  this.mapY = mapY;
  this.mapOriginX = mapOriginX;
  this.mapOriginY = mapOriginY;
  this.mapName = mapName;
  this.currentSpace = new Array();
  this.currentSpace[0] = "y"+this.mapOriginY+'x'+this.mapOriginX;
  
  
  switch(theGame.displayMode){
  case 'graphic':
    this.currentSpace[1] = '<img class=icon src=/bc_media/images/bc_icon_O.gif alt=origin />';
  break;  
  
  case 'text':
    this.currentSpace[1] = 'O';
  break;
  
  default:
    this.currentSpace[1] = '<img class=icon src=/bc_media/images/bc_icon_O.gif alt=origin />';
  break;    
  }
  this.curses = new Array();
  
  this.renderMap = renderMap;

}



function renderMap(){

 rowLimit = this.mapY;
 colLimit = this.mapX;
 
 y=1;
 x=1;
 cont = true;
 while(x<=this.mapX && y<=this.mapY && cont == true){
  eval('theType=document.getElementById("y'+y+'x'+x+'").innerHTML;');
  theGridCell = new gridObject(theType);
  theGridCell.gridObjectRender();
  
  switch(theGame.displayMode){
  case 'graphic':
     if(theGridCell.type == theType && theGridCell.src != theGridCell.type){
	   eval('document.getElementById("y'+y+'x'+x+'").innerHTML = theGridCell.src;');
	  }
  break;  
  
  case 'text':
      if(theGridCell.type != theType){
	   eval('document.getElementById("y'+y+'x'+x+'").innerHTML = theGridCell.type;');
	  }
  break;

  
  default: 
   if(theGridCell.type == theType && theGridCell.src != theGridCell.type){
	   eval('document.getElementById("y'+y+'x'+x+'").innerHTML = theGridCell.src;');
	  }
  break; 
  }
  //eval('alert(theGridCell.src);');
  
  x++;
  if(x==this.mapX+1){y++; x=1;} 
  
  if(x>theMap.mapX && y>theMap.mapY){cont = false;}

 
 
 }


}
//theMap = new mapVars(1,25,30,1,1,'Map 1');
theMap = new mapVars(1,theMapX,theMapY,originX,originY,mapName);
theGame = new theGame(1,'medium');

function randomCurse(){


 
  if(theMap.curses.length >0){
    start=1;
    while(start<=theGame.curseLimit){
      document.getElementById(theMap.curses[start]).className="mapCell";
	  start++;
    }
  }
  start=1;
  while(start<=theGame.curseLimit){
 
    randX = Math.round(Math.random()*theMap.mapX);
    if(randX==0){randX=1;}
  
    randY = Math.round(Math.random()*theMap.mapY);
    if(randY==0){randY=1;}
  
    eval('theMap.curses['+start+']="y'+randY+'x'+randX+'";');
    document.getElementById(theMap.curses[start]).className="blackCurse";
    start++;
  }
}


//BEGIN// lifeForm object defines players and monsters
function lifeForm(id,name,energyDie,energy,moveDie,movement,attack,phaseForce,blackCurse,money,gems,points,gemLimit,pointLimit,lightningBolts,shield,ringOfProtection,lives,level,attackBonus,levelTarget){
   this.id = id;
   this.name = name;
   this.energyDie = energyDie;
   this.energy = energy;
   this.moveDie = moveDie;
   this.movement = movement;
   this.attack = attack;
   this.phaseForce = phaseForce;
   this.blackCurse = blackCurse;
   this.attackBonus = attackBonus;
   this.money = money;
   this.gems = gems;
   this.level = level;
   this.points = points;
   this.gemLimit = gemLimit;
   this.pointLimit = pointLimit;
   this.lightningBolts = lightningBolts;
   this.shield = shield;
   this.ringOfProtection = ringOfProtection;
   this.currentRoll;
   this.chanceRoll = chanceRoll;
   this.attackRoll = attackRoll;
   this.moveRoll = moveRoll;
   this.lives = lives;
   this.positionX = theMap.mapOriginX;
   this.positionY = theMap.mapOriginY;
   this.increasePointLimit = increasePointLimit;
   this.increaseGemLimit = increaseGemLimit;
   this.increaseLevel = increaseLevel;
   this.winner = winner;
   this.death = death;
   this.checkForCurse = checkForCurse;
   this.checkLevelIncrease = checkLevelIncrease;
   this.checkWin = checkWin;
   this.gameOver = gameOver;
   this.addRing = addRing;
   this.levelTarget = levelTarget;

}

function checkLevelIncrease(){
  if(this.points >= this.levelTarget){this.increaseLevel();}
}

function increaseLevel(){
 this.level += 1;
 this.moveDie += 1;
 this.energyDie +=1;
 this.attackBonus +=1;
 this.levelTarget += this.levelTarget*2;
    EventRemark('You have become more powerful. You have reached Level '+this.level+'.');
}

function checkWin(){
  if((this.points >= this.pointLimit) && (this.gems>=this.gemLimit)){
   winGame();
  }
}

function checkForCurse(){
 x=0;

 eval('playerPosition = "y'+this.positionY+'x'+this.positionX+'";');
 
 while(x<=theGame.curseLimit){
   //alert(x+'  '+theMap.curses[x]+'  '+playerPosition);
   if(theMap.curses[x]==playerPosition && curseSave()==false){
     addBC(1);
   }
   x++;
 }

}
function addEnergy(){
   energyAdd = Math.round(Math.random()*thePlayer.energyDie);
   if(energyAdd==0){energyAdd = 1;}
   thePlayer.energy += energyAdd;
   playerStats.energy.value = thePlayer.energy;
   document.getElementById("playerEnergy").innerHTML = thePlayer.energy;
   if(energyAdd>0){theDirE = 'up';}
   if(energyAdd<0){theDirE = 'down';}
   energyRemark = 'Your Energy went '+theDirE+' '+energyAdd+'.';
   EventRemark(energyRemark);
   return thePlayer.energy;
}

function addGem(){

   thePlayer.gems += 1;
   playerStats.gems.value = thePlayer.gems;
   document.getElementById("playerGems").innerHTML = thePlayer.gems;
   thePlayer.checkWin();
   EventRemark('You found a Magical Shard');
   return thePlayer.gems;
}

function getMoney(moneyAmount){
 if(moneyAmount==0){moneyAmount = 1;}
   thePlayer.money += moneyAmount;
   playerStats.money.value = thePlayer.money;
   document.getElementById("playerMoney").innerHTML = thePlayer.money;
   if(moneyAmount>0){theDirC = 'up';}
   if(moneyAmount<0){theDirC = 'down';}
   theRemark='Your money went '+theDirC+' '+moneyAmount+'.';
   EventRemark(theRemark);
}
function addMoney(multiple){
   if(multiple == null){multiple = 6;}
   energyAdd = Math.round(Math.random()*multiple);
   getMoney(energyAdd);
   return thePlayer.money;

}

function addPoints(amount, multiple){
  if(multiple==null){multiple=1;}
  if(amount == null){amount = Math.round(Math.random()*10);}
  total = amount * multiple;
  thePlayer.points += total;
  playerStats.money.value = thePlayer.points;
  document.getElementById("playerPoints").innerHTML = thePlayer.points;
  //add function to increase power as points go up
  //if(thePlayer.points > 50 && < 100){}
  thePlayer.checkLevelIncrease();
  thePlayer.checkWin();
   if(total>0){theDirY = 'up';}
   if(total<0){theDirY = 'down';}
   pointRemark='Your points went '+theDirY+' '+total+'.';
   EventRemark(pointRemark);
}

function addRing(ringAmount){
 if(ringAmount==null){ringAmount=1;}
 if(this.ringOfProtection + ringAmount <= 6){
    this.ringOfProtection += ringAmount;
 }
 if(this.ringOfProtection + ringAmount < 0){this.ringOfProtection = 0;}
   playerStats.ringOfProtection.value = this.ringOfProtection;
   document.getElementById("playerRingOfProtection").innerHTML = this.ringOfProtection;
}

function addBC(BCamount){

   if(BCamount==null){BCamount=1;}
   if(thePlayer.ringOfProtection<=0){
    thePlayer.blackCurse += BCamount;
    if(thePlayer.blackCurse < 1){thePlayer.blackCurse = 1;}
    if(thePlayer.blackCurse >20){thePlayer.blackCurse = 20;}
    playerStats.blackCurse.value = thePlayer.blackCurse;
    showBC(thePlayer.blackCurse);
   }
   if(thePlayer.ringOfProtection>0){
    thePlayer.addRing(-1);
   }


}

function addPhase(phaseAmount){
   thePlayer.phaseForce += phaseAmount;
   if(thePlayer.phaseForce < 1){thePlayer.phaseForce = 1;}
   if(thePlayer.phaseForce >20){thePlayer.phaseForce = 20;}
   playerStats.phaseForce.value = thePlayer.phaseForce;
   showPhase(thePlayer.phaseForce);
   if(phaseAmount>0){theDir = 'up';}
   if(phaseAmount<0){theDir = 'down';}
   phaseRemark = 'Your Phase Force went '+theDir+' '+phaseAmount+'.';
   EventRemark(phaseRemark);
}

function resetBC(){
     alert('You got the Black Curse... the quest is getting harder.');
     thePlayer.increasePointLimit();
     thePlayer.increaseGemLimit();
	 thePlayer.blackCurse = 1;
	 playerStats.blackCurse.value = 1;
	 showBC(1);

}

function noMoveRoll(){
   document.getElementById("moveButton").innerHTML='<table cellpadding="0px" cellspacing="0px" style="margin: 0px; padding: 0px"><tr><td rowspan="3"><image class="moveLeft" src="/bc_media/images/bc_left_dead.png" alt="no move"/></td><td><image class="moveUp" src="/bc_media/images/bc_up_dead.png" alt="no move"/></td><td rowspan="3"><image class="moveright" src="/bc_media/images/bc_right_dead.png" alt="no move"/></td></tr><tr><td><div id="actionButton"><image class="moveRoll" src="/bc_media/images/bc_roll_dead.png" alt="no roll"/></div></td></tr><tr><td><image class="moveDown" src="/bc_media/images/bc_down_dead.png" alt="no move"/></td></tr></table>'; 
}

function rollAgain(){

 if(theGame.paused == false){

   document.getElementById("moveButton").innerHTML='<table cellpadding="0px" cellspacing="0px" style="margin: 0px; padding: 0px"><tr><td rowspan="3"><image class="moveLeft" src="/bc_media/images/bc_left_dead.png" alt="no move"/></td><td><image class="moveUp" src="/bc_media/images/bc_up_dead.png" alt="no move"/></td><td rowspan="3"><image class="moveright" src="/bc_media/images/bc_right_dead.png" alt="no move"/></td></tr><tr><td><div id="actionButton"><button class="navBtn moveRoll" onclick="thePlayer.chanceRoll();"><image class="moveRoll" src="/bc_media/images/bc_roll.png" onmouseover="this.src=\'/bc_media/images/bc_roll_hover.png\'" onmouseout="this.src=\'/bc_media/images/bc_roll.png\'" alt="roll"/></button></div></td></tr><tr><td><image class="moveDown" src="/bc_media/images/bc_down_dead.png" alt="no move"/></td></tr></table>'; 
 }
}

function moveAgain(){

 if(thePlayer.movement>0){document.getElementById("moveButton").innerHTML='<table cellpadding="0px" cellspacing="0px" style="margin: 0px; padding: 0px"><tr><td rowspan="3"><button class="navBtn moveLeft" onclick="movePlayer(1,\'left\');"><image class="moveLeft" src="/bc_media/images/bc_left.png" onmouseover="this.src=\'/bc_media/images/bc_left_hover.png\'" onmouseout="this.src=\'/bc_media/images/bc_left.png\'" alt="move left"/></button></td><td><button class="navBtn moveUp" onclick="movePlayer(1,\'up\');"><image class="moveUp" src="/bc_media/images/bc_up.png" onmouseover="this.src=\'/bc_media/images/bc_up_hover.png\'" onmouseout="this.src=\'/bc_media/images/bc_up.png\'" alt="move up"/></button></td><td rowspan="3"><button class="navBtn moveRight" onclick="movePlayer(1,\'right\');"><image class="moveright" src="/bc_media/images/bc_right.png" onmouseover="this.src=\'/bc_media/images/bc_right_hover.png\'" onmouseout="this.src=\'/bc_media/images/bc_right.png\'" alt="move right"/></button></td></tr><tr><td><div id="navRoll"><image class="moveRoll" src="/bc_media/images/bc_roll_dead.png" alt="no roll"/></div></td></tr><tr><td><button class="navBtn moveDown" onclick="movePlayer(1,\'down\');"><image class="moveDown" src="/bc_media/images/bc_down.png" onmouseover="this.src=\'/bc_media/images/bc_down_hover.png\'" onmouseout="this.src=\'/bc_media/images/bc_down.png\'" alt="move down"/></button></td></tr></table>'; }
if(thePlayer.movement<=0){rollAgain();}
}

function movePlayer(distance,direction){
 if(distance==null){distance = 1;}
 if(direction != null){
 move = false;
  if(direction == 'left' || direction =='right'){
   if (direction == 'left' && thePlayer.positionX-distance >= 1){
   	move =true;
    eval('gridType = document.getElementById("y'+thePlayer.positionY+'x'+(thePlayer.positionX-distance)+'").innerHTML;');
	gridType = srcToName(gridType);
	if(gridType =='X'){
	  if(thePlayer.phaseForce==20){move=false;}
	  if(thePlayer.currentRoll <= thePlayer.phaseForce && thePlayer.phaseForce<20){
	    addPhase(1);
	  }
	}
	if(move==true){
    thePlayer.positionX = thePlayer.positionX -distance;}

   }
   if (direction == 'right' && thePlayer.positionX+distance <= theMap.mapX){
   move = true;
   eval('gridType = document.getElementById("y'+thePlayer.positionY+'x'+(thePlayer.positionX+distance)+'").innerHTML;');
   gridType = srcToName(gridType);
	if(gridType =='X'){
	  if(thePlayer.phaseForce==20){move=false;}
	  if(thePlayer.currentRoll <= thePlayer.phaseForce && thePlayer.phaseForce<20){
	    addPhase(1);
	  }
	}
    if(move==true){
    thePlayer.positionX = thePlayer.positionX +distance;}

   } 
  }

  if(direction == 'up' || direction =='down'){
   if (direction == 'up' && thePlayer.positionY-distance >= 1){
   	move =true;
    eval('gridType = document.getElementById("y'+(thePlayer.positionY-distance)+'x'+thePlayer.positionX+'").innerHTML;');
	gridType = srcToName(gridType);
	if(gridType =='X'){
	  if(thePlayer.phaseForce==20){move=false;}
	  if(thePlayer.currentRoll <= thePlayer.phaseForce && thePlayer.phaseForce<20){
	    addPhase(1);
	  }
	}   
    if(move==true){
    thePlayer.positionY = thePlayer.positionY -distance;}
   }
   if (direction == 'down' && thePlayer.positionY+distance <= theMap.mapY){
    move = true;
    eval('gridType = document.getElementById("y'+(thePlayer.positionY+distance)+'x'+thePlayer.positionX+'").innerHTML;');
	gridType = srcToName(gridType);
	if(gridType =='X'){
	  if(thePlayer.phaseForce==20){move=false;}
	  if(thePlayer.currentRoll <= thePlayer.phaseForce && thePlayer.phaseForce<20){
	    addPhase(1);
	  }
	}  
	if(move==true){
     thePlayer.positionY = thePlayer.positionY +distance;}
   } 
  } 
  //alert(thePlayer.positionX+','+thePlayer.positionY);

 if(move==true){changeMove(thePlayer.movement-distance); 
  eval('checkEvent(gridType,"y'+thePlayer.positionY+'x'+thePlayer.positionX+'");');
 }

 if(thePlayer.movement <= 0){rollAgain();}
    
 window.status = direction;
 }
 
}

function pauseGame(type){
 noMoveRoll();
 theGame.paused = type;
}

function srcToName(src){
 theVal = src;
 if(theGame.displayMode=='graphic'){
  theTypeString = new String(src);
  
  if(theTypeString.match('icon')){
   theTypeSplit = theTypeString.split('_');
   theTypeSplit2 = theTypeSplit[3].split('.');
   theVal = theTypeSplit2[0];
   //alert(theTypeString+' / '+theVal );
  }
 }
 return theVal;
}

function getTreasure(){
rollA = Math.round(Math.random()*6);
rollB = Math.round(Math.random()*6);
rollC = Math.round(Math.random()*6);
rollD = Math.round(Math.random()*6);

theRoll = rollA + rollB + rollC + rollD + theGame.bonus + thePlayer.attackBonus;

if(theRoll <= 6){

  addBC(1);
  showBC(thePlayer.blackCurse);

}


if(theRoll >= 8 && theRoll <=11){ addPoints(null,null);}
if(theRoll >= 12 && theRoll <=16){ addEnergy();}
if(theRoll >= 17 && theRoll <=19){ addMoney(10);}
if(theRoll >= 20 && theRoll <=21){ addGem();}
if(theRoll >= 22 && theRoll <=24){ thePlayer.addRing();}


}




function checkEvent(gridType,position){

 gridType = srcToName(gridType);

 eval('theGridObject = new gridObject(\''+gridType+'\');')
 switch(theGridObject.gameType){
  case 'item':
  switch(gridType){
  
   case 'E':
    addEnergy();
    break;
   
   case 'V':
    addGem();
    break;
   
   case 'C':
    addMoney();
    break;
   
   case 'T':
    getTreasure();
    break;
   
   case 'Y':
    addPoints(null,null);
    break;
  }
  theMap.currentSpace[1] = '';
  break;
 case 'monster':
  pauseGame('battle');
  switch(gridType){
    case 'W': eval('enemy'+position+' = makeLifeForm("worm");');break;
    case 'G': eval('enemy'+position+' = makeLifeForm("guard");');break;
    case 'S': eval('enemy'+position+' = makeLifeForm("centurion");');break; 
    case 'D': eval('enemy'+position+' = makeLifeForm("dragon");');break;
  }
  eval('showFightBtn(enemy'+position+'); theEnemy = enemy'+position);
 break;
 case 'entity':
   switch(gridType){
     case 'M':
	  merchantMenu('show');
	  break;
   }
   break;
 }
}

function moveDir(e) {
  if (e.keyCode) {keycode=e.keyCode;}
  else {keycode=e.which;}
 if(thePlayer.movement > 0 && theGame.paused==false){
 
  switch(keycode){
   case 52: movePlayer(1,'left');break;
   case 56: movePlayer(1,'up');break;
   case 54: movePlayer(1,'right');break;
   case 50: movePlayer(1,'down');break;
   default: break;
  }
 }
 
 if(thePlayer.movement <= 0 && theGame.paused != 'battle'){
   if(keycode == 13 || keycode == 53){thePlayer.chanceRoll();}
 }
 if(theGame.paused == 'battle'){
   if(keycode == 13 || keycode == 53){battle(thePlayer,theEnemy);}
 }
}

function moveClick(distance){
 if(distance==null){distance = 1;}
 //movePlayer(distance,direction);//function changes thePlayer.movement
 changeMove(thePlayer.movement-distance); 
 if(thePlayer.movement <= 0){ theGame.paused=true; rollAgain(); document.getElementById("moveButton").innerHTML='No Moves. Roll Again.';}

}


function changeMove(move){
if(theMap.currentSpace[1]!='M'){merchantMenu('hide');}
eval('document.getElementById("'+theMap.currentSpace[0]+'").innerHTML = "'+theMap.currentSpace[1]+'";');

eval('theMap.currentSpace[0] = "y'+thePlayer.positionY+'x'+thePlayer.positionX+'"; theMap.currentSpace[1] =document.getElementById("y'+thePlayer.positionY+'x'+thePlayer.positionX+'").innerHTML;');

if(theGame.displayMode=='graphic'){
 theChange = new String(theMap.currentSpace[1]);
 theChange = theChange.replace('"','');
 theChange = theChange.replace('"','');
 theMap.currentSpace[1] = theChange;
}

  document.getElementById("playerMovement").innerHTML = move;
  playerStats.movement.value = move;
  thePlayer.movement = move;
  
  switch(theGame.displayMode){
  
   case 'graphic':
   	eval('document.getElementById("y'+thePlayer.positionY+'x'+thePlayer.positionX+'").innerHTML="<img class=icon src=/bc_media/images/bc_icon_P.gif alt=player />";');
  break;
  
  case 'text':
    eval('document.getElementById("y'+thePlayer.positionY+'x'+thePlayer.positionX+'").innerHTML="P";');
  break;

  }
  EventRemark();
  thePlayer.checkForCurse();
  if(move==0){rollAgain();}

}

function curseSave(){
 save = false;
 if(thePlayer.ringOfProtection > 0){
  thePlayer.addRing(-1);
  save = true;
 }
 return save;
}

function chanceRoll(){
  theGame.paused = false;
  var roll = Math.round(Math.random()*20);
  if(roll==0){roll = 1;}
  if(roll==1){addPhase(1);}
  if(roll>=20){addPhase(-1);}
  this.movement = Math.round(Math.random()*this.moveDie);
  if(this.movement == 0){this.movement =1;}
  changeMove(this.movement);
  moveAgain();
  randomCurse();
  this.checkForCurse();
  if( roll <= this.blackCurse && curseSave()==false){
   resetBC();
  }
  showBC(this.blackCurse);
  document.getElementById("rollButton").innerHTML=roll; 
  document.getElementById("navRoll").innerHTML = '<image src="/bc_media/images/bc_roll_dead.png" alt="no roll available"/>';

  
  changeNum=1;
  while(changeNum<=20){
  
   if(changeNum <= roll){
    eval('document.getElementById("chanceNum'+changeNum+'").className="chanceNum2";');
   }
   if(changeNum > roll){
    eval('document.getElementById("chanceNum'+changeNum+'").className="chanceNum";');
   }
   changeNum++;
  }

  this.currentRoll = roll;
  return roll;
}


// Start fight button show/hide
function showFightBtn(){

 document.getElementById("actionButton").innerHTML = '<div id="actionButton"><button class="navBtn moveRoll" onclick="battle(thePlayer,theEnemy);"><image class="moveRoll" src="/bc_media/images/bc_fight.png" onmouseover="this.src=\'/bc_media/images/bc_fight_hover.png\'" onmouseout="this.src=\'/bc_media/images/bc_fight.png\'" alt="roll"/></button></div>';

 //document.getElementById("fightButton").innerHTML = '<button onclick="battle(thePlayer,theEnemy);">Fight</button>';
}
function hideFightBtn(){
 //document.getElementById("actionButton").innerHTML = '<img src="/bc_media/images/bc_roll_dead.png" alt="no rolls"/>';
}
// End fight button show/hide



function winner(loser){
  this.money = this.money + loser.money;
}



function death(){

	theGame.paused=false;
	this.lives = this.lives-1;
	
	
	if(this.name=='player'){
	if(this.lives <= 0){this.gameOver(); theGame.paused=true;}
	if(this.lives > 0){
	  playerStats.lives.value = this.lives;
      document.getElementById("playerLives").innerHTML = this.lives;
	  rollAgain();
	  eval('document.getElementById("y'+thePlayer.positionY+'x'+thePlayer.positionX+'").innerHTML=theMap.currentSpace[1];');
	  this.positionX = theMap.mapOriginX;
	  this.positionY = theMap.mapOriginY;
	  this.move = 0;
	  document.getElementById("playerMovement").innerHTML = this.move;
	  this.energy = 20;
	  document.getElementById("playerEnergy").innerHTML = this.energy;
	  playerStats.energy.value = this.energy;
	  
	  
	if(theGame.displayMode=='text'){
    	  eval('document.getElementById("y'+thePlayer.positionY+'x'+thePlayer.positionX+'").innerHTML="P";');
		  theMap.currentSpace[1]='O';
  }
  if(theGame.displayMode=='graphic'){
   	  eval('document.getElementById("y'+thePlayer.positionY+'x'+thePlayer.positionX+'").innerHTML="<img class=icon src=/bc_media/images/bc_icon_P.gif alt=player />";');
     theMap.currentSpace[1] ='<img class=icon src=/bc_media/images/bc_icon_O.gif alt=origin />';

  }
	  

      theMap.currentSpace[0] = "y"+theMap.mapOriginY+'x'+theMap.mapOriginX;
      
	 }
	}
	if(this.name!='player'){
	 theMap.currentSpace[1] = '';
     moveAgain();
	 addPoints(theEnemy.points,null);

	}
	hideFightBtn();
		return ' '+this.name+' died.';
}

function gameOver(){
 if(this.name=='player'){
  alert("Game Over");
     EventRemark('Game Over');
  theGame.chooseDifficulty();
 }
}

function moveRoll(){
  roll = Math.round(Math.random()*this.movement);
  if(roll==0){roll = 1;}
  return roll;
}

function attackRoll(){
  roll = Math.round(Math.random()*this.attack);
  if(roll==0){roll = 1;}
  roll += this.attackBonus;
  return roll;
}

function increasePointLimit(){
  roll = Math.round(Math.random()*theGame.pointFactor);
  if(roll==0){roll = 1;}
  this.pointLimit = this.pointLimit + roll;
  playerStats.pointLimit.value = this.pointLimit;
  document.getElementById("playerPointLimit").innerHTML = this.pointLimit;

}

function increaseGemLimit(){
  roll = Math.round(Math.random()*theGame.gemFactor);
  if(roll==0){roll = 1;}
  this.gemLimit = this.gemLimit + roll;
  playerStats.gemLimit.value = this.gemLimit;
  document.getElementById("playerGemLimit").innerHTML = this.gemLimit;
}


function makeLifeForm(type,id){

 if(type != 'player'){ 
 
 switch(type){
 
 case 'worm':
  theObject = new lifeForm(id,type,7,7,4,4,6,10,1,Math.round(Math.random()*6),0,7,null,null,0,0,0,0,1,0,0);
 break;
 
 case'guard':
  theObject = new lifeForm(id,type,10,10,6,6,8,10,1,Math.round(Math.random()*12),0,10,null,null,0,0,0,0,1,0,0);
 break;
 
 case 'dragon':
  theObject = new lifeForm(id,type,8,20,12,6,8,10,1,Math.round(Math.random()*100),0,10,null,null,0,0,0,0,1,1,0);
 break;
 
 case 'centurion':
  theObject = new lifeForm(id,type,8,15,10,6,8,10,1,Math.round(Math.random()*50),0,10,null,null,0,0,0,0,1,0,0);
 break;
 }
 
  enemyStats.name.value = theObject.name;
  document.getElementById("enemyName").innerHTML = theObject.name;
  enemyStats.energy.value = theObject.energy;
  document.getElementById("enemyEnergy").innerHTML = theObject.energy;
  enemyStats.attack.value = theObject.attack;
  document.getElementById("enemyAttack").innerHTML = theObject.attack;
  enemyStats.money.value = theObject.money;
  enemyStats.points.value = theObject.points;
  enemyStats.gems.value = theObject.gems;

}

if(type=='player'){
  theObject = new lifeForm(id,type,4,20,6,0,6,10,1,Math.round(Math.random()*20),0,0,20,100,0,0,3,3,1,0,25);
  
  playerStats.name.value = theObject.name;
  playerStats.lives.value = theObject.lives;
  document.getElementById("playerLives").innerHTML = theObject.lives;
  document.getElementById("playerName").innerHTML = theObject.name;
  playerStats.energy.value = theObject.energy;
  document.getElementById("playerEnergy").innerHTML = theObject.energy;
  playerStats.money.value = theObject.money;
  document.getElementById("playerMoney").innerHTML = theObject.money;
  playerStats.points.value = theObject.points;
  document.getElementById("playerPoints").innerHTML = theObject.points;
  playerStats.gems.value = theObject.gems;
  document.getElementById("playerGems").innerHTML = theObject.gems;
  
  playerStats.pointLimit.value = theObject.pointLimit;
  document.getElementById("playerPointLimit").innerHTML = theObject.pointLimit;
  playerStats.gemLimit.value = theObject.gemLimit;
  document.getElementById("playerGemLimit").innerHTML = theObject.gemLimit;
  
  playerStats.ringOfProtection.value = theObject.ringOfProtection;
  document.getElementById("playerRingOfProtection").innerHTML = theObject.ringOfProtection;
 } 
 return theObject;
}
//END// lifeForm object


//determine the outcome of a battle
function battle(attacker,defender){
  attack = attacker.attackRoll();
  defend = defender.attackRoll() + theGame.bonus;
  if ( attack > defend){
   difference = (attack-defend);
   defender.energy = defender.energy - difference;
   enemyStats.energy.value = defender.energy;
   document.getElementById("enemyEnergy").innerHTML = defender.energy;
   phrase = attacker.name+' beat '+defender.name+' by '+difference+' points.';
   if(defender.energy <= 0){attacker.winner(defender); phrase += defender.death();}
   
  }
  
  if ( attack < defend){
   difference = (defend-attack);
   attacker.energy = attacker.energy - difference;
   playerStats.energy.value = attacker.energy;
   document.getElementById("playerEnergy").innerHTML = attacker.energy;
   phrase = defender.name+' beat '+attacker.name+' by '+difference+' points.';
   if(attacker.energy <= 0){defender.winner(attacker); phrase += attacker.death();}
  }
  if ( attack == defend){ phrase = 'Tie';}
  EventRemark(phrase);
  //alert(phrase);
  return phrase;
}

function showBC(blackCurse){
   changeNum3 = 1;
   while(changeNum3<=20){

    if(changeNum3 <= blackCurse){
     eval('document.getElementById("bcNum'+changeNum3+'").className="chanceNum2";');
	 eval('document.getElementById("bcNum'+changeNum3+'").innerHTML="X";');
    }
    if(changeNum3 > blackCurse){
     eval('document.getElementById("bcNum'+changeNum3+'").className="chanceNum";');
	 eval('document.getElementById("bcNum'+changeNum3+'").innerHTML=" ";');
    }
   changeNum3++
  }
}

function showPhase(phase){
   changeNum4 = 1;
   while(changeNum4<=20){
    if(changeNum4 <= phase){
     eval('document.getElementById("phaseNum'+changeNum4+'").className="chanceNum2";');
	 eval('document.getElementById("phaseNum'+changeNum4+'").innerHTML="X";');
    }
    if(changeNum4 > phase){
     eval('document.getElementById("phaseNum'+changeNum4+'").className="chanceNum";');
	 eval('document.getElementById("phaseNum'+changeNum4+'").innerHTML=" ";');
    }
   changeNum4++
  }

}


 var ringCost = 15;
 var gemCost = 20;
 switch (theGame.diffuculty){
 case 'medium':
  ringCost = 15;
  break;
 }

function merchantMenu(showHide){
 var merch;
 switch(showHide){
   case 'hide':
     merch='';
	 break;
   case 'show':
     merch = '<div class="playerBox"><h4>Merchant</h4><form id="merchantForm"><select name="merchantItems"><option value="gem">Magical Shard ('+gemCost+')</option><option value="ringOfProtection">Ring of Protection ('+ringCost+')</option></select><br/><button onclick="buyItem()">Buy</button></form></div>';
     break;
   default:
     merch = '';
     break;
}
 document.getElementById("merchantMenu").innerHTML = merch;
 
}

function buyItem(){
  var cost;
  var name;
  var action;
  switch(merchantForm.merchantItems.value){
   case 'ringOfProtection':
      cost = ringCost;
	  name = 'Ring of Protection';
	  action = 'thePlayer.addRing(1)';
	  break;
   case 'gem':
      cost = gemCost;
	  name = 'Magical Shard';
	  action = 'addGem()';
	  break;
  }
  if(thePlayer.money < cost){alert('You don\'t have enough money to buy this item.');}
  
  if(thePlayer.money >= cost){
   eval(action);
   getMoney(cost * -1)
   
   purchase = 'You purchased a '+name+'.';
   EventRemark(purchase);
  }
  
}

function startGame(){
thePlayer = makeLifeForm('player'); showBC(thePlayer.blackCurse); showPhase(thePlayer.phaseForce); theEnemy = new lifeForm(); rollAgain(); theMap.renderMap();
}

function winGame(){
 theGame.paused=true;
 document.getElementById("moveButton").innerHTML = '<div color="background-color: black"><h1>You Win!!</h1><p>The prize is yet to come (That part hasn\'t been programmed yet!)</p></div>';
 document.getElementById("merchantMenu").innerHTML = '';
 
 alert('You Win!');

}
function EventRemark(remark){
 if(remark == null){document.getElementById("eventRemark").innerHTML = '';}
 if(remark != null){document.getElementById("eventRemark").innerHTML = remark;}
}

function helpWindow(target){
 page = '/bc_icon_legend.html';
 if (target != null){
  page += '#'+target;
 }
  window.open(page,'legend','width=450,height=350, scrollbars=Yes');
}