// maze7easu.js
// Script to create charlemagne mazes, copied orig from maze8hard.js on 15 Oct. 07
// copyright (C) G. Patton 2007, All rights reserved
// 10/15/07 took out harder and easier buttons and code to handle them
// 10/15/07 made percent 35 initially
// 10/15/07 took out code to change energy levels and reset them and set initial to 0

var ver=parseInt(navigator.appVersion)
var ie4=(ver>3  && navigator.appName!="Netscape" && navigator.appName!="Opera")?1:0
var op= navigator.appName=="Opera"?1:0
var ns4=(ver>3  && navigator.appName=="Netscape")?1:0
var ns3=(ver==3 && navigator.appName=="Netscape")?1:0
//alert("appName =" + navigator.appName + " ver=" + ver)

var snd = new Array()
   snd[0]= "soundstarttopfunny.wav"
   snd[1]= "soundstartmarimba.wav"
   snd[2]= "soundwinmarimba.wav"
   snd[3]= "soundplantsmarimba.wav"
   snd[4]= "soundwallcymbol.wav"

var stat = new Array()
   stat[0]= "wclickcharlemagne440x77.gif"
   stat[1]= "wwon440x77.gif"
   stat[2]= "wstarttop440x77.gif"
   stat[3]= "wweeds440x77.gif"
   stat[4]= "wwall440x77.gif"

var sndObj= new Object
var imgObj= new Object
var energy = 0 // energy value to blast plants
var button=0 // is mouse over a button 1 is yes 0 is no

// function to preload the sound called on load of body in html
function initialize()
{
for (i=0; i <=snd.length; i++)
  {
  sndObj=snd[i]
  }
for (i=0; i <=stat.length; i++)
  {
  imgObj=snd[i]
  }
} // end initialize

//  ******************** play sound clip input is file
function playsound(a) 
  {
//alert("op = " + op)
  if (op)
    {
    // skip playing for now if opera
    }
  else
    { 
//alert("document.Player = " + document.Player) 
    document.Player.URL = a

    // not opera so try sound
    try
      {
      document.Player.controls.play();
      }
    catch(err)
      {
      // problem with netscape and player controls so catch error and continue
      }
    } // end else not opera
  } // end playsound

// *************  function to stop sound not needed for ie
//function stopSound() 
//  {
//  document.Player.controls.stop();
//  }

// set a flag if over a button so don't move charlemagne to button
function buttonin()
  {
// alert("in buttonin")
  button=1
  }

// reset the flag if out of button
function buttonout()
  {
  button=0
  }

var gcp2event
var fishin = 2 // checks if fish is in maze=1 out=2 or start, finish, or wall = 3
var oldfishin = 2 // previous fishin state
// *********** function to handle mouseover all parts of maze
// it's argument is the object getting the event
function over(gcp2event,thisobj)
  {
  // this function is called whenever the mouse is over an img
  // first find out what kind of image using alt (water, plant, border), then handle it
  kind = thisobj.getAttribute("alt")
  oldfishin = fishin // save the old state
  // get new fishin state
  if (kind == "water" || kind == "plant") {fishin = 1}
  else if (kind == "wall" ) {fishin = 3}
  else if ( kind == "start" || kind == "finish") {fishin = 4}
  else if ( kind == "white" || kind == "starttitle" ) {fishin = 2}
  else {fishin = 2} // never get this except set it on warp

// alert("kind = " + kind + "  fishin = " + fishin )
  // **** check for going across start with no mouse and warn user to click charlemagne
  if (fishmoveflag==false && kind == "start" && oldfishin != 1  )
    {
      playsound(snd[0])
      document.getElementById('status').src="blank200x35.gif"
      document.getElementById("statustable").style.borderColor="blue"
      // take status off playing surface
      largestatusup(stat[0]) // must click charlemagne
      return
    } // end fishmoveflag== false and kind == start


  // *** check for starting, is mouse moving true so we did click on the fish
  if (fishmoveflag==true)
    {

// alert("oldfishin = " + oldfishin)
    if (kind == "start")
      {
      // check if we are already started and thus in maze and then went out start
      if (start==1 && oldfishin == 1) 
        {
        start=0
        document.getElementById('status').src="blank200x35.gif"
        document.getElementById("statustable").style.borderColor="blue"
        largestatusdown()
        return
        } // end of we went out start hole
      else if (start==0 )
        {
        // if we werent started and we were in wall or outside so start
        playsound(snd[1])
        document.getElementById('status').src="gamestarted200x35.gif"
        document.getElementById("statustable").style.borderColor="blue"
        largestatusdown()
        start=1
        return
        }
      else
        {
        // we were in start before so do nothing
        return
        }
      } // end start

    if (oldfishin == 2 || kind=="starttitle" || kind=="white")
      { 
      // we are outside the aquarium so stop game
      start=0
      document.getElementById('status').src="blank200x35.gif"
      document.getElementById("statustable").style.borderColor="blue"
      largestatusdown()
      return
      }


    // *** now check for end of game
    if (kind == "finish"){
      // we are at end but did we start
      if (start==1 && oldfishin == 1) 
        {
        // we are ok start is 1 we came from inside and fishmoveflag is true
        playsound(snd[2])
        document.getElementById('status').src="youwon200x35.gif"
        document.getElementById("statustable").style.borderColor="orange"
        largestatusup(stat[1])
        } // end if started
      else if (start == 0 || oldfishin == 2 || oldfishin == 3)
        {
        // start is 0 or we came from outside

        playsound(snd[0])
        document.getElementById('status').src="starttop200x35.gif"
        document.getElementById('statustable').style.borderColor="red"
        largestatusup(stat[2])
        warp() // move charlemagne to beginning
        } // end else
      else
        {
        // do nothing
        }
      start=0
      return
      } // end finish

    // check on hitting wall from inside or outside if fish moving
    if (kind == "wall" )
      {
      playsound(snd[4])
      document.getElementById('status').src="hitborder200x35.gif"
      document.getElementById("statustable").style.borderColor="red"
      largestatusup(stat[4])
      warp() // move charlemagne to beginning
      return
      } // end of wall


    } // end fishmoveflag true *************************************

  // *****  check if charlemagne is moving and the game is started
  if (start==1 && fishmoveflag==true)
    {
    if (kind == "water")
      {
      // do nothing
      return
      }

    if (kind == "plant")
      {
      // check if there is energy to blast the plant
      if (energy >0)
        {
        // blast plant
        energy=energy-1
        document.getElementById('energy').innerHTML=energy
        // now change plant to water for this round
        thisobj.setAttribute("alt","water")
        thisobj.setAttribute("src","aquapix.gif")
        return
        }
      else
        {       
        playsound(snd[3])
        document.getElementById('status').src="youlose200x35.gif"
        document.getElementById("statustable").style.borderColor="red"
        largestatusup(stat[3])
        warp() // move charlemagne to beginning
        return
        }
      } // end of plant

   

    // if we got here we have a kind we don't know about so abort
    alert("Error 1, you must reload page")
    } // end of start true and fish move true

  } // end of function over()

// ******** code for the trailing cursor ****************************
var arx = new  Array() // arrays to hold trail values
var ary = new  Array()
var newind // indices for trail
var curind 
var fishmoveflag=false
var delx
var dely

// ************* this function warps Charlemagne to top, stops trailing but leaves cursor
// user must go click on charlemagne again to try again.
function warp()
  {
  fishmoveflag=false
  start=0
  oldfishin=2
  // now move charlemagne with some offsets
  document.getElementById('charlemagne').style.left=firsttimex
  document.getElementById('charlemagne').style.top=firsttimey 
  } //end warp function


var firsttimex = 0 //hold place where charlemagne first clicked
var firsttimey = 0 

// *************** come here when click anywhere in body
function arm(gcpevent)
  {

var gcpevent // event passed in
// alert("after click arm event.button = " + gcpevent )
  // first check if we are in a button and if so exit
  if (button == 1) 
    { 
    largestatusdown()
    return 
    }

  if  (firsttimex==0 ) 
    {
    firsttimex= document.getElementById('charlemagne').offsetLeft
    firsttimey= document.getElementById('charlemagne').offsetTop
//alert("firsttime x = " + firsttimex + "  firsttime y = " + firsttimey)
    }

  // take out any text over the screen
  document.getElementById('status').src="blank200x35.gif"
  document.getElementById("statustable").style.borderColor="blue"
  largestatusdown()

  // check for left mouse button and mouse moving flag
  if (fishmoveflag==true)
    {
    // if mouse was moving stop it
    fishmoveflag=false
    }

  // if left click turn on trailing and move charlemagne to mouse
  else
    {
    fishmoveflag=true // show mouse moving
    delx=-10 // offsets to get charlemagne lined up
    dely=-15
    xcoor=gcpevent.clientX // get coordinates
    ycoor=gcpevent.clientY
    // prime the trailing array with points
    for (i=0; i < 29; i++) 
      {
      arx[i] = xcoor+delx
      ary[i] = ycoor+dely
      }
    newind=0  // index to put new coords
    curind=15 // index to remove old coords
    gcpmove(gcpevent) // reset initially
    }
  } // end arm



// ********  function to actually do the moving
function gcpmove(gcp1event)
  {
var gcp1event
// alert("in gcpmove gcp1event= " + gcp1event)
  if (fishmoveflag==true)
    {
    // get cursor position and save it at new cursor
    arx[newind]=gcp1event.clientX+delx
    ary[newind]=gcp1event.clientY+dely
    // set new coordinates
    document.getElementById('charlemagne').style.left=arx[curind]
    document.getElementById('charlemagne').style.top=ary[curind]  

    newind++  // increment the indexs
    curind++
    if (newind >= 29) {newind = 0} // loop the indexs
    if (curind >= 29) {curind = 0}	
    return false
    } // end of is fish moveing flag on
  }  // end of gcpmove 


//-------------------------------------end of fish --------------------
//-------------------------------------start of maze ------------------
var w = 25  
var percent = 30 // make percent filled initially 45 for hard maze

var start = 0 
//--------------------------------------------
function makemaze(x,y,indenttop,indentbottom)
  {
  // x is the number of rows, y is the number of cols, and indents are where start
  // and finish openings go. 

  start=0
  // the next line puts in code to reset the maze and it must have the same row and col
  // as the original maze.  Be careful here
  document.write( '<input type="button" value="New Maze"  onmouseup="resetmaze(' + x + ',' + y +  ',' + indenttop + "," + indentbottom + ',this)" onmouseover="buttonin()" onmouseout="buttonout()"  onmousedown="buttoncolor(this)" /> ')

  // put charlemagne image and give it charlemagne id
  document.write( '<img src="whitepix.gif" alt="white" width="25" height="22" onmouseover="over(event,this)" > ')
  document.write( '<img id="charlemagne"  style="position:absolute; cursor:pointer" src="charlemagne20x22.gif" alt="charlemagne" width="20" height="22" border="0" > ')

  //  some white space  then start text
  document.write( '<img src="whitepix.gif" alt="white" width="15" height="12"  onmouseover="over(event,this)" > ')
  document.write( '<img id="start" src="start24x12.gif" alt="starttitle" width="24" height="12" onmouseover="over(event,this)" style="cursor:pointer" > ')

  // white space then harder button white space easier button
  document.write( '<img src="whitepix.gif" alt="white" onmouseover="over(event,this)" width="15" height="12"> ')

  //document.write( '<input type="button" value="Harder Maze"  onmouseup="hardermaze(' + x + ',' + y +  ',' + indenttop + "," + indentbottom + ',this )"  onmouseover="buttonin()" onmouseOut="buttonout()" onmousedown="buttoncolor(this)" > ')

  //document.write( '<img src="whitepix.gif" alt="white" width="15" height="12"> ')
  //document.write( '<input type="button" value="Easier Maze"  onmouseup="easiermaze(' + x + ',' + y +  ',' + indenttop + "," + indentbottom + ',this)"  onmouseover="buttonin()" onmouseOut="buttonout()" onmousedown="buttoncolor(this)" >  ')

  // now the energy level
//  document.write( '&nbsp;Energy=')
//  document.write( '<span id="energy" > 1</span>')
//  document.write( '&nbsp;&nbsp;Fill=')
//  document.write( '<span id="fill" >45</span>')
    document.write( '<br>') // must leave this for simple game
  // make top of maze
  maketopborder(x+2,indenttop)

  // make body of maze
  for (j=1; j<=y; j++)
    {
    makerow(x,j,indenttop,indentbottom,y)
    }

  // make bottom of maze
  makebottomborder(x+2,indentbottom)

  // some white space  then the words finish
  document.write( '<img src="whitepix.gif" alt="white" width="375" height="12" > ')
  document.write( '<img src="finish34x12.gif" alt="finishtitle" width="34" height="12" style="cursor:pointer"> <br> ')
  } // end makemaze

// *************  function to make the top of the maze
function maketopborder(z,indenttop)
  {
  // z = width
  // indenttop=indent to opening on top
  for (i=1; i<=indenttop; i++)
    {
    // left side
    document.write( '<img src="darkbluepix.gif" alt="wall" width="' + w + '" height="' + w + '" onmouseover="over(event,this)"   onmouseMove="gcpmove(event)">' )
    }
  // middle notice it starts the game by mouseover
  document.write( '<img src="aquapix.gif" alt="start" width="' + w + '" height="' + w + '" style="cursor:pointer"  onmouseover="over(event,this)"  onmouseMove="gcpmove(event)">' )

  // right side
  for (i=indenttop+2; i<=z; i++)
    {
    document.write( '<img src="darkbluepix.gif" alt="wall" width="' + w + '" height="' + w + '" onmouseover="over(event,this)"   onmouseMove="gcpmove(event)">' )
    }
  document.write( '<br>' )
  } // end maketopborder

// ***************  function to make bottom of maze
function makebottomborder(z,indentbottom)
  {
  // z = width
  // indentbottom = indent to bottom opening
  for (i=1; i<=indentbottom; i++)
    {
    document.write( '<img src="darkbluepix.gif" alt="wall" width="' + w + '" height="' + w + '" onmouseover="over(event,this)"   onmouseMove="gcpmove(event)" >' )
    }


  document.write( '<img id="finish" src="aquapix.gif" alt="finish" width="' + w + '" height="' + w + '" style="cursor:pointer"  onmouseover="over(event,this)"  onmouseMove="gcpmove(event)" >' )

  for (i=indentbottom+2; i<=z; i++)
    {
    document.write( '<img src="darkbluepix.gif" alt="wall" width="' + w + '" height="' + w + '" onmouseover="over(event,this)"  onmouseMove="gcpmove(event)" >' )
    }
  document.write( ' <br> ' )
  } // end makebottomborder

// **********************  function to make a maze row
function makerow(z,row,it,ib,lastrow)
  {
  // row = row number
  // z = row width
  // it = indenttop
  // ib = indentbottom
  // lastrow = number of the last row in the maze 

  document.write( '<img src="darkbluepix.gif" alt="wall" width="' + w + '" height=' + w + ' onmouseover="over(event,this)"  onmouseMove="gcpmove(event)" >' )

  // now make all colums
  for (i=1; i<=z; i++)
    {
    selid = "r" + row + "c" + i
    // check if this is the square right under the entrance or over the exit
    // if so make it not a plant
    if ((row==1&&i==it)||(row==lastrow&&i==ib))
      {
      document.write( '<img id="' + selid + '" src="aquapix.gif" alt="water" width="' + w + '" height="' + w + '" style="cursor:pointer" onmouseover="over(event,this)"  onmouseMove="gcpmove(event)" >' )
      }

    else
      {
      // put in water or plant using random numbers and the percent passed in
      gcprand = Math.random() * 100

      if (gcprand < percent)  
        {
        document.write( '<img  id="' + selid + '" src="darkgreenpix.gif" alt="plant" width="' + w + '" height="' + w + '" style="cursor:pointer" onmouseover="over(event,this)"   onmouseMove="gcpmove(event)" >' )
        }
      else
        {
        document.write( '<img  id="' + selid + '" src="aquapix.gif" alt="water" width="' + w + '" height="' + w + '" style="cursor:pointer"  onmouseover="over(event,this)"  onmouseMove="gcpmove(event)" >' )
        } // end of percent if
      } // end of not entrance or exit if
    } // end of for i 1-z

    // write block at end
    document.write( '<img src="darkbluepix.gif" alt="wall" width="' + w + '" height="' + w + '" onmouseover="over(event,this)"  onmouseMove="gcpmove(event)" >' )
    document.write( '<br>' )
  } // end of make row


// ********************** function to change button color
function buttoncolor(thisb)
  { 
  var thisb
  thisb.style.background="red"
  } 
  
// ********************* function to reload a new maze when button pushed
//function hardermaze(x,y,w,h,thisb)
//  {
//  var x, y, w, h, thisb
//  percent=percent+5
//  if (percent >= 95) {percent=95} // limit it to 95
//  resetmaze(x,y,w,h,thisb)
//  } // end of harder maze

//function easiermaze(x,y,w,h,thisb)
//  {
//  var x, y, w, h, thisb
//  percent=percent-5
//  if (percent<=35) {percent=35} // keep it above 34 percent
//  resetmaze(x,y,w,h,thisb)
//  } // end of easiermaze



function resetmaze(x,y,it,ib,thisb) 
  {
  var selid, i, j, x, y, thisb
  // Start rows
  for (i=1; i <= y; i++) 
    {
    for (j=1; j <= x ; j++) 
      {
      selid = "r" + i + "c" + j 

      selectedobj = document.getElementById(selid)
      // check if this is the square right under the entrance or over the exit
      // if so make it not a plant, note x is the lastrow
      if ((i==1&&j==it)||(i==y&&j==ib))
        {
        // sets alt and src attributes to water
        selectedobj.setAttribute('alt','water')
        selectedobj.setAttribute('src','aquapix.gif')
        }

      else
        {
        // put in water or plant using random numbers and the percent passed in
        gcprand = Math.random() * 100

        if (gcprand < percent)  
          {
          // sets alt and src attributes to plant
          selectedobj.setAttribute('alt','plant')
          selectedobj.setAttribute('src','darkgreenpix.gif')
          }
        else
          {
          // sets alt and src attributes to water
          selectedobj.setAttribute('alt','water')
          selectedobj.setAttribute('src','aquapix.gif')
          } // end of percent if
        } // end of not entrance or exit if
      } // end for col
 
    } // end for row
  // reset energy
//  if      (percent >=90) {energy=14}
//  else if (percent >=85) {energy=12}
//  else if (percent >=80) {energy=10}
//  else if (percent >=75) {energy=9}
//  else if (percent >=70) {energy=8}
//  else if (percent >=65) {energy=7}
//  else if (percent >=60) {energy=6}
//  else if (percent >=55) {energy=5}
//  else if (percent >=50) {energy=4}
//  else                   {energy=2}
//  document.getElementById('energy').innerHTML=energy  
//  document.getElementById('fill').innerHTML=percent
  // change button back 
  thisb.style.background="silver"
  } // end of resetmaze

// ***********  function to print up large status over screen
function largestatusup(file)
  {
  document.getElementById("largestatus").width="440"
  document.getElementById("largestatus").height="77"
  document.getElementById("largestatus").setAttribute("src",file)
  }

// ***********  function to take down large status over screen
function largestatusdown()
  {
  document.getElementById("largestatus").width="1"
  document.getElementById("largestatus").height="1"
  document.getElementById("largestatus").setAttribute("src","bonepix.gif")
  }
