var COLUMNS        = 9;
var FIELD_SIZE     = 36;
var INITIAL_PUZZLE = 4;
var ROWS           = 4;

var cantMove           = true;
var fieldContainsPiece = new Array(FIELD_SIZE + 5);
var first              = 0;
var foundNumberOfPairs = 0;
var moves              = 0;
var numberOfPairs      = FIELD_SIZE / 2;
var puzzle             = INITIAL_PUZZLE;
var running            = false;
var second             = 0;
var variables          = '';

var a = new Image(100,100);
var b = new Image(100,100);
var c = new Image(100,100);
var d = new Image(100,100);
var e = new Image(100,100);
var f = new Image(100,100);
var g = new Image(100,100);
var h = new Image(100,100);
var i = new Image(100,100);
var j = new Image(100,100);
var k = new Image(100,100);
var l = new Image(100,100);
var m = new Image(100,100);
var n = new Image(100,100);
var o = new Image(100,100);
var p = new Image(100,100);
var q = new Image(100,100);
var r = new Image(100,100);
var s = new Image(100,100);

for(cell=1; cell<(FIELD_SIZE+1); cell++) {
   fieldContainsPiece[cell] = Math.floor(cell/2 + 0.5);
}

function changePuzzle(newPuzzle) {
   var element  = null;
   var photoSet = '';

   puzzle = newPuzzle;

   for(cell=0; cell<=36; cell++) {
      if((element = document.getElementById('p' + cell)) != null) {
         element.src = puzzle + '/0.gif';
      }
   }

   if((element = document.getElementById('photoSet')) != null) {
      while(element.hasChildNodes()) {
         try {
            element.removeChild(element.firstChild);
         }
         catch(e) {
         }
      }

      if(puzzle == 1) {
         photoSet = '2005 DLS Photo Contest';
      }
      else if(puzzle == 2) {
         photoSet = '2006 DLS Photo Contest';
      }
      else if(puzzle == 3) {
         photoSet = '2007 DLS Photo Contest';
      }
      else if(puzzle == 4) {
         photoSet = '2008 DLS Photo Contest';
      }
      else if(puzzle == 5) {
         photoSet = 'Steam';
      }
      else if(puzzle == 6) {
         photoSet = 'Diesel';
      }
      else if(puzzle == 7) {
         photoSet = 'Kids';
      }
      else if(puzzle == 8) {
         photoSet = 'Mixed';
      }
      element.appendChild(document.createTextNode(photoSet));
   }

   for(var p=1; p<=8; p++) {
      if((element = document.getElementById('ps' + p)) != null) {
         if(p == puzzle) {
            element.disabled = true;
         }
         else {
            element.disabled = false;
         }
      }
   }

   setStart(true);
}

function clickOn(field) {
   if(running == false) {
      alert('Please click the start button to begin playing');
   }
   if(cantMove) {
      return;
   }
   if(picIs(field) != '0.gif') {
      return;
   }
   displayPiece(field);
   if(first == 0 || field == first) {
      first = field;
      return;
	}
   cantMove = true;
   second   = field;
   moves++;
   if(fieldContainsPiece[first] == fieldContainsPiece[second]) {
      first = second = 0;
      foundNumberOfPairs++;
      if(foundNumberOfPairs == numberOfPairs) {
         alert('You have found all of the matches in ' + moves + 
               ' moves.  Congratulations!');
         setStart(true);
      }
      cantMove = false;
   }
   else {
   	setTimeout("hide()",1500);
	}
}

function displayPage() {
   var cell,column,row;
   w('<table cellpadding="0" cellspacing="0" class="pieces">' + 
     '<tr>' + 
     '<td bgcolor="white" colspan="' + ((COLUMNS*2)+1) + '">' + 
     '<center class="text18">' + 
     '<b>Discover Live Steam Railroad Photo Memory Game</b></center></td></tr>' + 
     '<tr><td bgcolor="white" colspan="' + ((COLUMNS*2)+1) + 
     '" height="5" nowrap="1"/></tr>' + 
     '<tr>' + 
     '<td class="border-1-1" nowrap="1"/>');
   for(column=0; column<COLUMNS; column++) {
      w('<td class="border-1-2" nowrap="1"/>');
      if(column == COLUMNS-1) {
         w('<td class="border-1-4" nowrap="1"/>');
      }
      else {
         w('<td class="border-1-3" nowrap="1"/>');
      }
   }
   w('</tr>');

   cell = 1;
   for(row=0; row<ROWS; row++) {
      w('<tr>' + 
        '<td class="border-2-1" nowrap="1"/>');
      for(column=0; column<COLUMNS; column++) {
         w("<td width=100><a href='javascript:clickOn(" + cell + 
           ")'><img class='cell' id='p" + cell + "' name='p" + cell + 
           "'></a></td>");
         if(column == COLUMNS-1) {
            w('<td class="border-2-4" nowrap="1"/>');
         }
         else {
            w('<td class="border-2-3" nowrap="1"/>');
         }
         cell++;
      }
      w('</tr>');

      if(row < ROWS-1) {
         w('<tr>' + 
           '<td class="border-3-1" nowrap="1"/>');
         for(column=0; column<COLUMNS; column++) {
            w('<td class="border-3-2" nowrap="1"/>');
            if(column == COLUMNS-1) {
               w('<td class="border-3-4" nowrap="1"/>');
            }
            else {
               w('<td class="border-3-3" nowrap="1"/>');
            }
         }
         w('</tr>');
      }
   }

   w('<tr>' + 
     '<td class="border-4-1" nowrap="1"/>');
   for(column=0; column<COLUMNS; column++) {
      w('<td class="border-4-2" nowrap="1"/>');
      if(column == COLUMNS-1) {
         w('<td class="border-4-4" nowrap="1"/>');
      }
      else {
         w('<td class="border-4-3" nowrap="1"/>');
      }
   }
   w('</tr>');

   w('<tr>' + 
     '<td class="border-5-1" nowrap="1"/>' + 
     '<td colspan="' + ((COLUMNS*2)-1) + '">' + 
     '<table cellpadding="0" cellspacing="0" width="100%">' + 
     '<tr>' + 
     '<td align="left" class="text12" nowrap="1" width="200">' + 
     'This photo set is:</td>' + 
     '<td align="center" width="100%">' + 
     '<input id="startButton" onClick="startGame();" type="button"' + 
     ' value="Start"/></td>' + 
     '<td align="right" class="text12" id="photoSet" nowrap="1" width="200">');
   w('</td></tr>' + 
     '<tr>' + 
     '<td height="4" nowrap="1"/></tr>' + 
     '</table></td>' + 
     '<td class="border-5-4" nowrap="1"/></tr>');

   // Photo Sets
   w('<tr>' + 
     '<td class="border-6-1" nowrap="1"/>' + 
     '<td class="border-6-2" colspan="' + ((COLUMNS*2)-1) + '"/>' + 
     '<td class="border-6-4" nowrap="1"/></tr>' + 
     '<tr>' + 
     '<td class="border-2-1" nowrap="1"/>' + 
     '<td bgcolor="white" colspan="' + ((COLUMNS*2)-1) + '">' + 
     '<table cellpadding="0" cellspacing="0">' + 
     '<tr>' + 
     '<td class="text12">Other photo sets: ' + 
     '<input id="ps1" onclick="changePuzzle(1);"' + 
     ' title="2005 DLS Photo Contest Photo Set" type="button"' + 
     ' value="2005"/>&nbsp;' + 
     '<input id="ps2" onclick="changePuzzle(2);"' + 
     ' title="2006 DLS Photo Contest Photo Set" type="button"' + 
     ' value="2006"/>&nbsp;' + 
     '<input id="ps3" onclick="changePuzzle(3);"' + 
     ' title="2007 DLS Photo Contest Photo Set" type="button"' + 
     ' value="2007"/>&nbsp;' + 
     '<input id="ps4" onclick="changePuzzle(4);"' + 
     ' title="2008 DLS Photo Contest Photo Set" type="button"' + 
     ' value="2008"/>&nbsp;' + 
     '<input id="ps5" onclick="changePuzzle(5);" title="Steam Photo Set"' + 
     ' type="button" value="Steam"/>&nbsp;' + 
     '<input id="ps6" onclick="changePuzzle(6);" title="Diesel Photo Set"' + 
     ' type="button" value="Diesel"/>&nbsp;' + 
     '<input id="ps7" onclick="changePuzzle(7);" title="Kids Photo Set"' + 
     ' type="button" value="Kids"/>&nbsp;' + 
     '<input id="ps8" onclick="changePuzzle(8);" title="Mixed Photo Set"' + 
     ' type="button" value="Mixed"/>&nbsp;' + 
     '</td></tr>' + 
     '</table></td>' + 
     '<td class="border-2-4" nowrap="1"/></tr>' + 
     '<tr>' + 
     '<td class="border-3-1" nowrap="1"/>' + 
     '<td class="border-3-2" colspan="' + ((COLUMNS*2)-1) + '"/>' + 
     '<td class="border-3-4" nowrap="1"/></tr>');

   // Instructions
   w('<tr>' + 
     '<td class="border-2-1" nowrap="1"/>' + 
     '<td bgcolor="white" colspan="' + ((COLUMNS*2)-1) + '">' + 
     '<table cellpadding="0" cellspacing="0">' + 
     '<tr>' + 
     '<td class="text12">To play:<ol>' + 
     '<li>Click the "Start" button.</li>' + 
     '<li>Click on one of the squares to turn over and view the picture.</li>' + 
     '<li>Click on an unchosen square to turn it over and match the previous ' + 
     'picture.</li>' + 
     '<li>When a match is made, the pictures will remain viewable.' + 
     '&nbsp;&nbsp;If a match is NOT made, the squares turn back over.</li>' + 
     '<li>To win, match all 18 pictures.</li>' + 
     '</ol>Good luck and have fun!</td></tr>' + 
     '</table></td>' + 
     '<td class="border-2-4" nowrap="1"/></tr>' + 
     '<tr>' + 
     '<td class="border-7-1" nowrap="1"/>' + 
     '<td class="border-7-2" colspan="' + ((COLUMNS*2)-1) + '"/>' + 
     '<td class="border-7-4" nowrap="1"/></tr>' + 
//   '<tr>' + 
//   '<td align="right" bgcolor="white" class="text12" colspan="' + 
//   ((COLUMNS*2)+1) + '">&nbsp;<br/>You are visitor # ' + 
//   '<!--webbot bot="HitCounter" u-custom="odometer.gif" i-image="5"' + 
//   ' i-resetvalue="894089" PREVIEW="&lt;strong&gt;[Hit Counter]&lt;/strong&gt;"' + 
//   ' i-digits="0" startspan -->' + 
//   '<img src="http://discoverlivesteam.com/_vti_bin/fpcount.exe/?Page=puzzle/index.htm|Custom=odometer.gif"' + 
//   ' alt="Hit Counter"><!--webbot bot="HitCounter" i-checksum="48926"' + 
//   ' endspan --></td></tr>' + 
     '</table></td></tr>');

   changePuzzle(puzzle);
}

function displayPiece(piece) {
   var element;
   if((element = document.getElementById('p' + piece)) != null) {
      element.src = puzzle + '/' + fieldContainsPiece[piece] + '.gif';
   }
}

function hide() {
   cantMove = false;
   hidePiece(first);
   hidePiece(second);
   first = second = 0;
}

function hidePiece(piece) {
   var element;
   if((element = document.getElementById('p' + piece)) != null) {
      element.src = puzzle + '/0.gif';
   }
}

function parseURL() {
   var character = "";
   var element   = 0;
   var location  = String(parent.location);
   var position  = 0;
   var word      = "";

   if(location.length == 0) {
      return;
   }

   // Check if there are any variables
   if(location.indexOf('?') == -1) {
      return;
   }

   variables = 
      String(location.substring(location.indexOf('?') + 1,location.length));

   // Parse
   while(position < variables.length) {
      character = variables.substring(position,position + 1);

      if(character == "=") {
         names[element] = word;
         word           = "";
      }
      else if(character == "&") {
         values[element++] = word;
         word              = "";
      }
      else if(position == variables.length - 1) {
         word             += variables.substring(position,position + 1);
         values[element++] = word;
         word              = "";
      }
      else {
         word += character;
      }

      position++;
   }
}

function picIs(imgName) {
   if(!(document.images)) {
      return('Non-compatibile browser');
   }
   fn = document.images['p' + imgName].src;
   l  = fn.length;
   fileName = fn.substring(l-5,l);
   return(fileName);
}

function randomize() {
   var cell;
   for(cell=1; cell<(FIELD_SIZE+1); cell++) {
   	tmp  = Math.floor(Math.random() * FIELD_SIZE + 1);
   	swap = fieldContainsPiece[tmp];
    	fieldContainsPiece[tmp]  = fieldContainsPiece[cell];
    	fieldContainsPiece[cell] = swap;
	}
}

function setStart(enable) {
   var element = null;

   running = false;

   if((element = document.getElementById("startButton")) != null) {
      if(enable) {
         element.disabled = false;
         element.value    = 'Start';
      }
      else {
         element.disabled = true;
         element.value    = 'In Progress';

         running = true;
      }
   }
}

function startGame() {
   setStart(false);

   first = foundNumberOfPairs = moves = second = 0;

   for(n=1; n<(FIELD_SIZE+1); n++) {
   	fieldContainsPiece[n] = Math.floor(n/2 + 0.5);
   	hidePiece(n);
	}

   randomize();
   cantMove = false;
}

function w(text) {
   document.write(text);
}

if(top.location != self.location) {
   top.location = self.location;
}

try {
   parseURL();
   var variableList = variables.split("&");

   for(var x=0; x<variableList.length; x++) {
      var pair = variableList[x].split("=");

      if(pair[0] == '1') {
         puzzle = 1;
      }
   } 
}
catch(exception) {
   alert('Exception: ' + exception.toString());
}

a.src = puzzle + "/0.gif";
b.src = puzzle + "/1.gif";
c.src = puzzle + "/2.gif";
d.src = puzzle + "/3.gif";
e.src = puzzle + "/4.gif";
f.src = puzzle + "/5.gif";
g.src = puzzle + "/6.gif";
h.src = puzzle + "/7.gif";
i.src = puzzle + "/8.gif";
j.src = puzzle + "/9.gif";
k.src = puzzle + "/10.gif";
l.src = puzzle + "/11.gif";
m.src = puzzle + "/12.gif";
n.src = puzzle + "/13.gif";
o.src = puzzle + "/14.gif";
p.src = puzzle + "/15.gif";
q.src = puzzle + "/16.gif";
r.src = puzzle + "/17.gif";
s.src = puzzle + "/18.gif";

