function ExitGame()
{
   endGame();
   if ($runWithEditors)
   {
      toggleLevelEditor();
   } else {
      quit();      
   }
}
          
function NewGame()
{
   $cleared = 0;
   %cardWidth =  124;
   %cardHeight = 176;
   %rows = 3;
   %cardsPerRow = 6;
   %startPositionX = -317;
   %startPositionY = -179;
   %spacing = 2;
   %cards = %rows * %cardsPerRow;
   %imageMap = "CardBackImageMap";
   if (isObject($gameBoard))
   {
        for (%r=0;%r<%rows;%r++){
           %row = $gameBoard.row[%r];
         for(%i=0;%i<%cardsPerRow;%i++)
         {
            %card = %row.cards[%i];
            if(isObject(%card))
            {
               %card.safeDelete();
            }
         }
        }
        //Delete our Gameboard.
        $gameBoard.delete();
   }
   for(%i=0;%i<%cards;%i++)
   {
      %m=$match[%i];
      %id=%m.getId();
      if (%id)
      {
         if(%matches)
         {
            %matches = %matches SPC %id;
         } else {
            %matches=%id;
         }
      }
   }
   %boardMatch=shuffle(%matches);
   $gameBoard = new SimSet(GameBoard){};
   %mit=0;
   for(%r=0;%r<%rows;%r++)
   {
      %row = new SimSet("Row" @ %r){};
      $gameBoard.row[%r] = %row;
      %startPositionY = %startPositionY + %spacing;
      for(%i=0;%i<%cardsPerRow;%i++)
      {
         %positionx = %startPositionX + ((%cardWidth + %spacing) * %i);
         %positiony = %startPositionY + (%cardHeight * %r);
         %card =new t2dStaticSprite() {
                  imageMap = %imageMap;
                  position = "0 0";
                  size = %cardWidth SPC %cardHeight;
                  isCard  = "1";
                  row     = %r;
                  card    = %i;
                  placement = %positionx SPC %positiony;
                  match     = getWord(%boardMatch,%mit);
            };
         addCard(%card);
         %row.cards[%i]=%card;
         %mit++;
      }
   }
}

function shuffle(%list)
{
   //Rough Shuffler, *Do not use for Accuracy needed Games.
   %objCount = getWordCount(%list);
   for (%i=0;%i<%objCount;%i++)
   {
      %r=getRandom(1000);
      while(%temp[%r])
      {
         %r=getRandom(1000);         
      }
      %temp[%r]=getWord(%list,%i);
   }
   for(%i=0;%i<1000;%i++)
   {
         if(%temp[%i])
         {    
              if(%sorted)
              {
               %sorted=%sorted SPC %temp[%i];            
              } else {
               %sorted=%temp[%i];
              }
         }
   }
   return %sorted;
}

function addCard(%object)
{
      %m=%object.match;
      %m.setLayer(2);
      %object.setLayer(1);
      t2dScene.add(%object);      
      %m.setVisible(0);
      t2dScene.add(%m);
      %object.moveTo(%object.placement,275);
      %m.setFlipX(0);
      %m.moveTo(%object.placement,1000);
}


function sceneWindow2D::onRightMouseDown(%this,%modifier,%worldPosition,%mouseClicks)
{
      cheatOn();
}

function sceneWindow2D::onRightMouseUp(%this,%modifier,%worldPosition,%mouseClicks)
{
      cheatOff();
}



function sceneWindow2D::onMouseDown(%this,%modifier,%worldPosition,%mouseClicks)
{
   if ($cantMove)
   {
      return;      
   }
   %sceneGraph = sceneWindow2D.getSceneGraph();
   %objects = %sceneGraph.pickPoint(%worldPosition);
   %objCount = getWordCount(%objects);
   for(%i=0;%i<%objCount;%i++)
   {
       %obj = getWord(%objects, %i);
       if (isObject(%obj))
       {
          if (%obj.isCard)
          {
             %m=%obj.match;
             %m.setVisible(1);
             checkMove(%obj);         
          }
       }
   }
}

function checkMove(%obj)
{
   //We must have a card, since they got one in the index
   %obj.setVisible(false);
   if ($move)
   {
         //First Move Exists. This is the second move.
         %ms=$move.match;
         %mss=%obj.match;
         %msi=%ms.matchset;
         %msj=%mss.matchset;
         if (%msi $= %msj)
         {
            %ms.setLayer(0);
            %mss.setLayer(0);
            %ms.moveTo("500" SPC %ms.getPositionY(),100);
            %mss.moveTo("500" SPC %mss.getPositionY(),100);
            $cantMove=1;
            schedule(3000,0,"resetMouse");
            $move=0;            
            $cleared++;
            if ($cleared $= 9)
            {
               $cleared=0;
               schedule(1000,0,"showWin");               
            }
         } else {
            schedule(3000,0,"resetCards",%obj,$move,%ms,%mss);
            $cantMove=1;
            schedule(3000,0,"resetMouse");
         }
   } else {
      $move = %obj;      
   }
}

function resetMouse()
{
   $cantMove = 0;   
}

function showWin()
{
      for(%i=0;%i<18;%i++)
      {
         %m=$match[%i];
         %m.setVisible(true);
         %m.setFlipX(1);
         %m.moveTo("-500" SPC getRandom(%m.getPositionY()),getRandom(50,300));
      }
}

function cheatOn()
{
   for(%i=0;%i<18;%i++)
   {
      %m=$match[%i];
      %m.setLayer(0);
      %m.setVisible(1);
   }
   //schedule(1000,0,"cheatOff");
}

function cheatOff()
{
   for(%i=0;%i<18;%i++)
   {
      %m=$match[%i];
      %m.setLayer(2);
      %m.setVisible(0);
   }
}

function resetCards(%card1,%card2,%m1,%m2)
{
   %card1.setVisible(true);
   %card2.setVisible(true);
   %m1.setVisible(false);
   %m2.setVisible(false); 
   $move=0;  
}