function MazeMinigame::onWake(%this)
{
   if (isFile("~/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/MazeConfig.cs"))
       exec("~/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/MazeConfig.cs");
       
   %this.isPlaying = true;

   MazeMinigame.itemStartPos = MazeMinigame.itemStartPosX SPC MazeMinigame.itemStartPosY; 
   MazeMinigame.itemExtent = MazeMinigame.itemExtentW SPC MazeMinigame.itemExtentH;
   MazeMinigame.itemToReachStartPos = MazeMinigame.itemToReachStartPosX SPC MazeMinigame.itemToReachStartPosY; 
   MazeMinigame.itemToReachExtent = MazeMinigame.itemToReachExtentW SPC MazeMinigame.itemToReachExtentH;
   MazeMinigame.visibleItemExtent = MazeMinigame.visibleItemExtentW SPC MazeMinigame.visibleItemExtentH;
   MazeMinigame.visibleItemTDBExtent = MazeMinigame.visibleItemTDBExtentW SPC MazeMinigame.visibleItemTDBExtentH;
   MazeMinigame.sliderFacePos = MazeMinigame.sliderFacePosX SPC MazeMinigame.sliderFacePosY; 
   MazeMinigame.sliderFaceExtent = MazeMinigame.sliderFaceExtentW SPC MazeMinigame.sliderFaceExtentH;

   if (MazeMinigame.gameMode $= "")
      MazeMinigame.gameMode = "Painter";

   %this.gameCtrl = new GuiContainer() {
      canSaveDynamicFields = "1";
      Enabled = "1";
      className = "Maze"@(MazeMinigame.gameMode);    
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = "0 0";
      Extent = MazeFace.getExtent();
      MinExtent = "1 1";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      lockMouse = "0";
      Margin = "0 0 0 0";
      Padding = "0 0 0 0";
      AnchorTop = "1";
      AnchorBottom = "0";
      AnchorLeft = "1";
      AnchorRight = "0";
      useMouseEvents = "1";
   };
   
   %this.gameCtrl.init();   

   MinigameRefCenter.add(%this.gameCtrl);

   %this.gameFinished = false;

   %this.initSpecificGui();

   MinigameRefCenter.pushToBack(%this.gameCtrl);
   MinigameRefCenter.pushToBack(MazeSkipButton);
   
   // Force resizing
   %this.setExtent(getWord(Canvas.extent,0), getWord(Canvas.extent,1));
   Canvas.setWidescreen(MinigameRefCenter, 0, false);
}

// Callback for resizing events
function MazeMinigame::onResize(%this, %width, %height)
{
   Canvas.setWidescreen(MinigameRefCenter, 2000, true);
}

// Called by miniGame.cs
function MazeMinigame::start(%this)
{
 
   %this.ConfettiIsPlaying = false;
   
   MazeMinigame.canPlayBuggyStartSound = true;
   
   %this.lastClickX = 0;
   %this.lastClickY = 0;
   %this.currentClickX = 110;
   %this.currentClickY = 110;
   
   %this.diffClickX = 100;
   %this.diffClickY = 100;
   
   %this.scoreCollision = 1; //begins with 5 stars, if there is a collision, loses 1 star
   
   %this.onStart();
}

function MazeMinigame::getBoardX(%this, %faceX)
{
   return mRound(%faceX*getWord(MazeBoard.getExtent(), 0)/ getWord(MazeFace.getExtent(), 0));
}

function MazeMinigame::getBoardY(%this, %faceY)
{
   return mRound(%faceY*getWord(MazeBoard.getExtent(), 1) / getWord(MazeFace.getExtent(), 1));
}

function MazeMinigame::getFaceX(%this, %boardX)
{
   return mRound(%boardX*getWord(MazeFace.getExtent(), 0)/ getWord(MazeBoard.getExtent(), 0));
}

function MazeMinigame::getFaceY(%this, %boardY)
{
   return mRound(%boardY*getWord(MazeFace.getExtent(), 1) / getWord(MazeBoard.getExtent(), 1));
}

function MazeMinigame::getColorType(%this, %color)
{
   %r = getWord(%color, 0);
   %g = getWord(%color, 1);
   %b = getWord(%color, 2);

   //White 
   if ((%r >= 0.5) && (%g >= 0.5) && (%b >= 0.5))
      return "ROAD";
   //Red
   else if ((%r >= 0.5) && (%g < 0.5) && (%b < 0.5))
      return "END";
   //green
   else if ((%r < 0.5) && (%g >= 0.5) && (%b < 0.5))
      return "CLICK_POINT";
   //Blue
   else if ((%r < 0.5) && (%g < 0.5) && (%b >= 0.5))
      return "PATH";
   //Black
   else if ((%r < 0.5) && (%g < 0.5) && (%b < 0.5))
      return "WALL";
}

function MazeMinigame::getTypeColor(%this, %type)
{
   switch$ (%type)
   {
      case "ROAD":
         return "1 1 1";
      case "END":
         return "1 0 0";
      case "PATH":
         return "0 0 1";
      case "WALL":
         return "0 0 0";
   }
}

// Quit minigame
function MazeMinigame::quit(%this)
{
   lockMouse(false);
   Canvas.showCursor();
   
   %this.isPlaying = false;
   
   sfxStopAll();
      
   // Widescreen support
   if(isObject(MinigameCenter))
   {
      Minigame.bringToFront(%this);
      MinigameCenter.setVisible(true);
      Minigame.pushToBack(MinigameCenter);   
   }

   if(MazeMinigame.gameMode $= "Clicker") 
      Minigame.hintsGained = MazeMinigame.meter.getHintsGained();
   else if(MazeMinigame.gameMode $= "Dragger") 
      Minigame.hintsGained = %this.getHintsGained();

   if(isObject(PlayMinigameDlg))
   {
      PlayMinigameDlg.bringToFront(PlayMinigameDlg.curMinigame);
   } 
      
   if(mFloor(Minigame.hintsGained) > 0 &&  %this.gameFinished /*&& PlayMinigameDlg.isPlayingMinigame !$= true*/)
   {      
      if(!%this.ConfettiIsPlaying)
      {
         if(isObject(PlayMinigameDlg))
         {            
            PlayMinigameDlgCenter.pushToBack(MinigameCircles);
            PlayMinigameDlgCenter.pushToBack(MinigameParticleFx);
            PlayMinigameDlgCenter.pushToBack(WinText);
            PlayMinigameDlgCenter.pushToBack(HintNumber);
            PlayMinigameDlgCenter.pushToBack(PMClapper);
            PlayMinigameDlgCenter.pushToBack(PMBoard);
            PlayMinigameDlgCenter.pushToBack(PMClapperStart);   
                     
            PlayMinigameDlg.bringToFront(%this);
            
            PlayMinigameDlg.setVisible(true);            
            PlayMinigameDlgCenter.setVisible(true);
         }
            
         // Play confetti
         MinigameParticleFx.ConfettiFx(15); // Play confetti for 15 secs
         %this.ConfettiIsPlaying = true;
         
         if(PlayMinigameDlg.isPlayingMinigame $= true)
            scheduleObjLCA(playMinigameDlg, "Minigame", 15*1000, "quitMinigame", 0);
         else
            // Quit a bit earlier
            scheduleObjLCA(playMinigameDlg, "Minigame", 12*1000, "quitMinigame", 0);
      }
   }   
   else
   {
      Minigame.hintsGained = 0;
      
      if(isObject(PlayMinigameDlg))
         PlayMinigameDlg.bringToFront(PlayMinigameDlg.curMinigame);
      
      playMinigameDlg.quitMinigame();
   }
}

function MazePainter::init(%this)
{
   %this.painting = false;
   if (MazeMinigame.brushRadius $= "")
      MazeMinigame.brushRadius = 3; 

   %this.lastX = 0;
   %this.lastY = 0;
   
   %this.circleIter = CirclePixelIterator::NewCirclePixelIterator();
   %this.lineIter = LinePixelIterator::NewLinePixelIterator();
}

function MazePainter::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks )
{
   %x = getWord(%worldPosition, 0) - getWord(%this.Position, 0);
   %y = getWord(%worldPosition, 1) - getWord(%this.Position, 1);
   
   // Compensate widescreen offset
   %rect2 = MinigameRefCenter.getPosition();
   %x -= getWord(%rect2,0);
   %y -= getWord(%rect2,1);   

   %bX = MazeMinigame.getBoardX(%x);
   %bY = MazeMinigame.getBoardY(%y);   

   if (MazeMinigame.getColorType(MazeBoard.getPixelColor(%bX, %bY)) $= "PATH")
   {
      %this.painting = true;
      %this.paintCircle(%bX, %bY);
      %this.lastX = %bX;
      %this.lastY = %bY;
   }
}

function MazePainter::onMouseDragged(%this, %modifier, %worldPosition, %mouseClicks )
{
   if (!%this.painting)
      return;

      
   %x = getWord(%worldPosition, 0) - getWord(%this.Position, 0);
   %y = getWord(%worldPosition, 1) - getWord(%this.Position, 1);
   
   // Compensate widescreen offset
   %rect2 = MinigameRefCenter.getPosition();
   %x -= getWord(%rect2,0);
   %y -= getWord(%rect2,1);      

   %bX = MazeMinigame.getBoardX(%x);
   %bY = MazeMinigame.getBoardY(%y);

   %lineIter = %this.lineIter;   

   %lineIter.init(%this.lastX, %this.lastY, %bX, %bY);
   while (true)
   {
      %x = %lineIter.x;
      %y = %lineIter.y;
            
      %colorType = MazeMinigame.getColorType(MazeBoard.getPixelColor(%x, %y));

      switch$ (%colorType)
      {
         case "ROAD":
            %this.paintCircle(%x, %y);
         case "PATH":
            %this.paintCircle(%x, %y);
         case "END":
            %this.paintCircle(%x, %y);
            %this.painting = false;
            MazeMinigame.gameFinished = true;
            MazeMinigame.quit();
            
         case "WALL":
            %this.painting = false;
            sfxPlay(AudioWrongObject);
            break;
      }
      
      if (!%lineIter.next())
         break;
   }

   %this.lastX = %bX;
   %this.lastY = %bY;
}

function MazePainter::onMouseUp(%this, %modifier, %worldPosition, %mouseClicks )
{
   %this.painting = false;
}

function MazePainter::onMouseLeave(%this, %modifier, %worldPosition, %mouseClicks )
{
   %this.painting = false;
}

function MazePainter::paintCircle(%this, %x0, %y0)
{
   %color = MazeMinigame.getTypeColor("PATH");
   %r = getWord(%color, 0);
   %g = getWord(%color, 1);
   %b = getWord(%color, 2);

   %circleIter = %this.circleIter;

   %circleIter.init(%x0, %y0, MazeMinigame.brushRadius, true);
   while (true)
   {
      %x = %circleIter.x;
      %y = %circleIter.y;
      
      if (MazeMinigame.getColorType(MazeBoard.getPixelColor(%x, %y)) $= "ROAD")
      {
         MazeBoard.setPixelColor(%x, %y, %r, %g, %b, 1);
         MazeFace.setPixelColor(%x, %y, %r, %g, %b, 1);
      }
      
      if (!%circleIter.next())
         break;
   }
   
   MazeFace.refreshBitmap();
}

function MazeDragger::init(%this)
{
   %this.dragging = false;

   %this.circleIter = CirclePixelIterator::NewCirclePixelIterator();
   %this.lineIter = LinePixelIterator::NewLinePixelIterator();
   
   %this.curSliderFacePosX = MazeMinigame.sliderFacePosX;
   %this.curSliderFacePosY = MazeMinigame.sliderFacePosY;

   %this.initFog();
   %this.initItemToDrag();

   MinigameRefCenter.pushToBack(%this.border);
   MinigameRefCenter.pushToBack(MazeSkipButton);
   
   %this.curLevel = 0;

   %this.curAnim = "";
   
   %this.draggingBack = false;
   
   //sfxPlay(MazeMinigame.ambiantSFX);
}

function MazeDragger::initFog(%this)
{
   %this.sliderFace = new GuiBitmapCtrl() {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = MazeMinigame.sliderFacePos;
      Extent = MazeMinigame.sliderFaceExt;
      MinExtent = "8 8";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      readWriteTex = "1"; //MUST come anywhere before bitmap attribute
      bitmap = "scriptsAndAssets/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/maze_slider_face";
      wrap = "0";
   };
   
   %this.sliderFog = new GuiBitmapCtrl() {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = MazeMinigame.sliderFacePos;
      Extent = MazeMinigame.sliderFaceExtent;
      MinExtent = "8 8";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      readWriteTex = "1"; //MUST come anywhere before bitmap attribute
      bitmap = "scriptsAndAssets/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/maze_fog";
      wrap = "0";
   };

   MinigameRefCenter.add(%this.sliderFog);

   %this.fogMask = new GuiBitmapCtrl() {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = "0 0";
      Extent = MazeMinigame.fogMaskSize SPC MazeMinigame.fogMaskSize;
      MinExtent = "8 8";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      readWriteTex = "1"; //MUST come anywhere before bitmap attribute
      bitmap = "scriptsAndAssets/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/fog_mask";
      wrap = "0";
   };

   %this.fogMaskInv = new GuiBitmapCtrl() {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = "0 0";
      Extent = MazeMinigame.fogMaskSize SPC MazeMinigame.fogMaskSize;
      MinExtent = "8 8";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      readWriteTex = "1"; //MUST come anywhere before bitmap attribute
      bitmap = "scriptsAndAssets/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/fog_mask_inv";
      wrap = "0";
   };

   %this.litMaze = new GuiBitmapCtrl() {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = "0 0";
      Extent = MazeMinigame.fogMaskSize SPC MazeMinigame.fogMaskSize;
      MinExtent = "8 8";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      readWriteTex = "1"; //MUST come anywhere before bitmap attribute
      bitmap = "scriptsAndAssets/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/litMazeDest";
      wrap = "0";
   };
   
   MinigameRefCenter.add(%this.litMaze);

   %this.border = new GuiBitmapCtrl() {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = MazeFace.getPosition();
      Extent = MazeFace.getExtent();
      MinExtent = "8 8";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      readWriteTex = "1"; //MUST come anywhere before bitmap attribute
      bitmap = "scriptsAndAssets/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/maze_border";
      wrap = "0";
   };

   MinigameRefCenter.add(%this.border);
}

function MazeDragger::initItemToDrag(%this)
{
   %this.item = new GuiBitmapCtrl() {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = MazeMinigame.itemStartPos;
      Extent = MazeMinigame.itemExtent;
      MinExtent = "2 2";
      canSave = "1";
      Visible = "0";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      bitmap = "scriptsAndAssets/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/item_to_drag";
      wrap = "0";
   };
   
   MinigameRefCenter.add(%this.item);

   %this.lastX = %this.item.getCenterX();
   %this.lastY = %this.item.getCenterY();

   %this.castLight(%this.lastX, %this.lastY);
   
   %this.visibleItem = new GuiBitmapCtrl() {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = "0 0";
      Extent = MazeMinigame.visibleItemExtent;
      MinExtent = "2 2";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      bitmap = "scriptsAndAssets/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/item_to_drag";
      wrap = "0";
   };
   
   %this.visibleItem.setCenterPos(%this.lastX, %this.lastY);
   
   MinigameRefCenter.add(%this.visibleItem);

   if (isFile("~/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/item_to_reach.JPG") 
       || isFile("~/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/item_to_reach.PNG"))
   {
      %this.itemToReach = new GuiBitmapCtrl() {
         canSaveDynamicFields = "0";
         Enabled = "1";
         isContainer = "0";
         HorizSizing = "relative";
         VertSizing = "relative";
         Position = MazeMinigame.itemToReachStartPos;
         Extent = MazeMinigame.itemToReachExtent;
         MinExtent = "2 2";
         canSave = "1";
         Visible = "0";
         hovertime = "1000";
         alpha = "1";
         color = "255 255 255 255";
         unitClip = "0 0 1 1";
         bitmap = "scriptsAndAssets/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/item_to_reach";
         wrap = "0";
      };
   }

   MinigameRefCenter.add(%this.itemToReach);

   %this.handCursor = new GuiBitmapCtrl() {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = "0 0";
      Extent = "50 50";
      MinExtent = "2 2";
      canSave = "1";
      Visible = "0";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      bitmap = "scriptsAndAssets/data/ui/minigame/hand_cursor";
      wrap = "0";
   };
   
   MinigameRefCenter.add(%this.handCursor);
}

function MazeDragger::loadNextLevel(%this)
{
   //lose dragging
   %this.handCursor.setBitmap("scriptsAndAssets/data/ui/minigame/hand_cursor");
   %this.dragging = false;
   
   %this.curLevel++;

   %this.curAnim = "1 0";
   scheduleObjLCA(%this, "Minigame", 10, "slideAnim", 0);

   MazeBoard.setBitmap("scriptsAndAssets/data/ui/minigame/Maze/"@playMinigameDlg.currentLevel@"/maze"@%this.curLevel);
}

function MazeDragger::loadPrevLevel(%this)
{
   %this.dragging = false;
   %this.curLevel--;

   %this.curAnim = "-1 0";
   scheduleObjLCA(%this, "Minigame", 10, "slideAnim", 0);

   MazeBoard.setBitmap("scriptsAndAssets/data/ui/minigame/Maze/"@playMinigameDlg.currentLevel@"/maze");
}

function MazeDragger::slideAnim(%this)
{
   %dir = getWord(%this.curAnim, 0);
   %step = getWord(%this.curAnim, 1) + 1;

   %levelOffset = -(%this.curLevel - %dir)*MazeMinigame.slideOffset;
   %offsetFactor = - %dir*%step/MazeMinigame.stepsPerSlide;

   if (MazeMinigame.verticalSlide)
   {
      %this.curSliderFacePosY = MazeMinigame.sliderFacePosY + %levelOffset + %offsetFactor*MazeMinigame.slideOffset;

      %y = MazeMinigame.getFaceY((%dir == -1? MazeMinigame.itemStartPosY : MazeMinigame.itemEndPosY) + 
                                 %offsetFactor*(MazeMinigame.itemEndPosY - MazeMinigame.itemStartPosY));
   
      %this.item.setPosition(getWord(%this.item.getPosition(), 0), %y);
   }
   else
   {
      %this.curSliderFacePosX = MazeMinigame.sliderFacePosX + %levelOffset + %offsetFactor*MazeMinigame.slideOffset;

      %x = MazeMinigame.getFaceX((%dir == -1? MazeMinigame.itemStartPosX : MazeMinigame.itemEndPosX) + 
                                 %offsetFactor*(MazeMinigame.itemEndPosX - MazeMinigame.itemStartPosX));
   
      %this.item.setPosition(%x, getWord(%this.item.getPosition(), 1));
   }
   
   %this.lastX = %this.item.getCenterX();
   %this.lastY = %this.item.getCenterY();
   
   //for region image is almost the same during slide, so we update it less time and be faster
   if (%step % 4 == 1)
      %this.castLight(%this.lastX, %this.lastY);
   else
      %this.litMaze.setCenterPos(%this.lastX, %this.lastY);
   
   %this.sliderFog.setPosition(MazeMinigame.getFaceX(%this.curSliderFacePosX),
                               MazeMinigame.getFaceY(%this.curSliderFacePosY));

   %this.visibleItem.setCenterPos(%this.lastX, %this.lastY);

   if (%step < MazeMinigame.stepsPerSlide)
   {
      %this.curAnim = %dir SPC %step;
      scheduleObjLCA(%this, "Minigame", 50, "slideAnim", 0);
   }
   else
   {
      if (%step % 4 != 1)
         %this.castLight(%this.lastX, %this.lastY);
      %this.curAnim = "";
   }
}

function MazeDragger::isInsideMaze(%this, %x, %y)
{
   return (%x >= MazeMinigame.getFaceX(MazeMinigame.sliderFacePosX) 
            && %y >= MazeMinigame.getFaceY(MazeMinigame.sliderFacePosY)
            && %x < MazeMinigame.getFaceX(MazeMinigame.sliderFacePosX + (MazeMinigame.verticalSlide? MazeMinigame.sliderFaceExtentW : MazeMinigame.slideOffset))
            && %y < MazeMinigame.getFaceY(MazeMinigame.sliderFacePosY + (MazeMinigame.verticalSlide? MazeMinigame.slideOffset : MazeMinigame.sliderFaceExtentH)));
}

function MazeDragger::onMouseEnter(%this, %modifier, %worldPosition, %mouseClicks )
{
   lockMouse(true);
}

function MazeDragger::onMouseMove(%this, %modifier, %worldPosition, %mouseClicks )
{
   %x = getWord(%worldPosition, 0) - getWord(%this.Position, 0);
   %y = getWord(%worldPosition, 1) - getWord(%this.Position, 1);
   
   // Compensate widescreen offset
   %rect2 = MinigameRefCenter.getPosition();
   %x -= getWord(%rect2,0);
   %y -= getWord(%rect2,1);   

   if (%this.isInsideMaze(%x, %y))
   {
      %this.handCursor.setCenterPos(%x, %y);

      if (%this.handCursor.visible == 0)
      {
         %this.handCursor.setVisible(true);
         Canvas.hideCursor();
      }
   }
   else if (%this.handCursor.visible == 1)
      {
         %this.handCursor.setVisible(false);
         lockMouse(true);
         Canvas.showCursor();
      }
}

function MazeDragger::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks )
{
   if (%this.curAnim !$= "")
      return;

   %this.handCursor.setBitmap("scriptsAndAssets/data/ui/minigame/hand_cursor_clicked");

   %x = getWord(%worldPosition, 0) - getWord(%this.Position, 0);
   %y = getWord(%worldPosition, 1) - getWord(%this.Position, 1);
   
   // Compensate widescreen offset
   %rect2 = MinigameRefCenter.getPosition();
   %x -= getWord(%rect2,0);
   %y -= getWord(%rect2,1);      
   
   %distX = %x - %this.visibleItem.getCenterX();
   %distY = %y - %this.visibleItem.getCenterY();
   %rad = MazeMinigame.getFaceX(MazeMinigame.grabRadius);
   
   if (%distX*%distX + %distY*%distY <= %rad*%rad)
   {
      %this.dragging = true;
      sfxPlay(MazeMinigame.pickUpSound);
      %this.lastCursorX = %x;
      %this.lastCursorY = %y;      

      %this.posInItemX = getWord(%this.item.getExtent(), 0)/2;
      %this.posInItemY = getWord(%this.item.getExtent(), 1)/2;
      
      %this.handCursor.setCenterPos(%this.visibleItem.getCenterX(), %this.visibleItem.getCenterY());
   }
}

function MazeDragger::onMouseDragged(%this, %modifier, %worldPosition, %mouseClicks )
{
   if (%this.dragging)
   {
      %x = getWord(%worldPosition, 0) - getWord(%this.Position, 0);
      %y = getWord(%worldPosition, 1) - getWord(%this.Position, 1);
      
      // Compensate widescreen offset
      %rect2 = MinigameRefCenter.getPosition();
      %x -= getWord(%rect2,0);
      %y -= getWord(%rect2,1);   

      echo(%x SPC %y);
	  
      %worldPosition = (%this.item.getCenterX() + MazeMinigame.dragSpeedScale*(%x - %this.lastCursorX))
                       SPC (%this.item.getCenterY() + MazeMinigame.dragSpeedScale*(%y - %this.lastCursorY));

      %this.onItemDragged(%modifier, %worldPosition, %mouseClicks );

      %this.handCursor.setCenterPos(%this.item.getCenterX(), %this.item.getCenterY());

      if (%this.isInsideMaze(%x, %y))
      {
         %this.lastCursorX = %x;
         %this.lastCursorY = %y;
      }
      else
      {
         %this.lastCursorX = MazeMinigame.getCenterX() - getWord(%rect2,0);
         %this.lastCursorY = MazeMinigame.getCenterY() - getWord(%rect2,1);    
         echo("center" SPC MazeMinigame.getCenterX() SPC MazeMinigame.getCenterY());  
         Canvas.setCursorPos(MazeMinigame.getCenterX(), MazeMinigame.getCenterY());
         echo("cursor at" SPC Canvas.getCursorPos());
      }
   }
   else
      %this.onMouseMove(%modifier, %worldPosition, %mouseClicks);
}

function MazeDragger::onItemDragged(%this, %modifier, %worldPosition, %mouseClicks)
{
   %itemPosX = getWord(%this.item.getPosition(), 0);   
   %itemPosY = getWord(%this.item.getPosition(), 1);   
   
   %fX = getWord(%worldPosition, 0) - getWord(%this.Position, 0);
   %fY = getWord(%worldPosition, 1) - getWord(%this.Position, 1);

   %lastBX = MazeMinigame.getBoardX(%itemPosX);
   %bX = MazeMinigame.getBoardX(%fX - %this.posInItemX);

   %lastBY = MazeMinigame.getBoardY(%itemPosY);
   %bY = MazeMinigame.getBoardY(%fY - %this.posInItemY);

   %cornerDistX = MazeMinigame.getBoardX(getWord(%this.item.getExtent(), 0));
   %cornerDistY = MazeMinigame.getBoardY(getWord(%this.item.getExtent(), 1));

   %lineIter = %this.lineIter;   

   %lineIter.init(%lastBX, %lastBY, %bX, %bY);
   
   %xCol = false;
   %yCol = false;
   %lastBX = %lineIter.x;
   %lastBY = %lineIter.y;
   
   while (%lineIter.next())
   {
      for (%i = 0; %i < 2; %i++)
      {
         %x = %lineIter.x + %i * %cornerDistX; 
         for (%j = 0; %j < 2; %j++)
         {
            %y = %lineIter.y + %j * %cornerDistY;
   
            %colorType = MazeMinigame.getColorType(MazeBoard.getPixelColor(%x, %y));
            switch$ (%colorType)
            {
               case "PATH":
                  if (%this.draggingBack)
                  {
                     if (%this.curLevel > 0)
                     {
                        %this.loadPrevLevel();               
                        return;
                     }
                     else
                     {
                        %this.dragging = false;
                        MazeMinigame.gameFinished = true;
                        MazeMinigame.quit();
                     }
                  }

                  
               case "END":
                  if (!%this.draggingBack)
                  {
                     if (%this.curLevel + 1 < MazeMinigame.numOfLevels)
                     {
                        %this.loadOnceNext = "1";
                        %this.loadNextLevel();               
                        return;
                     }
                     else
                     {
                        if (MazeMinigame.draggerWinPoint $= "Start")
                        {
                           %this.itemToReach.setVisible(false);
                           %this.visibleItem.setExtent(MazeMinigame.getFaceX(MazeMinigame.visibleItemTDBExtentW),
                                                       MazeMinigame.getFaceY(MazeMinigame.visibleItemTDBExtentH));
                                                       
                           %this.visibleItem.setPosition(getWord(%this.visibleItem.getPosition(), 0)
                                 - MazeMinigame.getFaceX(MazeMinigame.visibleItemTDBExtentW - MazeMinigame.visibleItemExtentW)/2,
                              getWord(%this.visibleItem.getPosition(), 1) 
                                 - MazeMinigame.getFaceY(MazeMinigame.visibleItemTDBExtentH - MazeMinigame.visibleItemExtentH)/2);

                           if (isFile("scriptsAndAssets/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/item_to_drag_back.png"))
                              %this.visibleItem.setBitmap("scriptsAndAssets/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/item_to_drag_back");
                                      
                           if (isFile("scriptsAndAssets/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/maze_slider_face_return.jpg")
                               || isFile("scriptsAndAssets/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/maze_slider_face_return.png"))
                              %this.sliderFace.setBitmap("scriptsAndAssets/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/maze_slider_face_return");

                           MazeMinigame.visibleItemExtentW = MazeMinigame.visibleItemTDBExtentW;                        
                           MazeMinigame.visibleItemExtentH = MazeMinigame.visibleItemTDBExtentH;                        
                           
                           %this.draggingBack = true;

                           MazeMinigame.onItemToReachFound();
                        }
                        else
                        {
                           %this.dragging = false;
                           MazeMinigame.gameFinished = true;
                           MazeMinigame.quit();
                        }
                     }
                  }
               case "WALL":
                  if (MazeMinigame.getColorType(MazeBoard.getPixelColor(%x, %lastBY + %j * %cornerDistY)) $= "WALL")
                     %xCol = true;                     
  
                  if (MazeMinigame.getColorType(MazeBoard.getPixelColor(%lastBX + %i * %cornerDistX, %y)) $= "WALL")
                     %yCol = true;
            }
         }
         
         if (%j < 2)
            break;
      }

      if (%i < 2)
         break;

      if (!%xCol)
         %lastBX = %lineIter.x;
      if (!%yCol)
         %lastBY = %lineIter.y;
   }

   %this.item.setPosition(MazeMinigame.getFaceX(%lastBX), MazeMinigame.getFaceY(%lastBY));
   
   %this.lastX = %this.item.getCenterX();
   %this.lastY = %this.item.getCenterY();

   %this.visibleItem.setCenterPos(%this.lastX, %this.lastY);

   %this.castLight(%this.lastX, %this.lastY);

   if (%this.curLevel + 1 == MazeMinigame.numOfLevels && %this.itemToReach !$= "" && !%this.draggingBack)
   {
      %itemToReachPos = %this.itemToReach.getPosition();
      %itemToReachExt = %this.itemToReach.getExtent();
      %distX = %this.lastX - (getWord(%itemToReachPos, 0) + getWord(%itemToReachExt, 0)/2);
      %distY = %this.lastY - (getWord(%itemToReachPos, 1) + getWord(%itemToReachExt, 1)/2);
      %maxDist = (MazeMinigame.getFaceX(MazeMinigame.fogMaskSize/2) + getWord(%itemToReachExt, 0)/2); //assume its almost a square   
      
      %visible = %distX*%distX + %distY*%distY < %maxDist*%maxDist;

      if (%this.itemToReach.visible != %visible)
         %this.itemToReach.setVisible(%visible);
   }
}

function MazeDragger::onMouseUp(%this, %modifier, %worldPosition, %mouseClicks )
{
   if (%this.dragging)
   {
      ////MISTERIOUS BUG... :(
      ////%x = %this.visibleItem.getCenterX();
      ////%y = %this.visibleItem.getCenterY();
      ////Canvas.setCursorPos(%x, %y);
      ////%this.handCursor.setCenterPos(%x, %y);
      //
      ////Canvas.setCursorPos(%this.getCenterX(), %this.getCenterY());
      //%this.dragging = false;
	  sfxPlay(MazeMinigame.dropSound);
   }

   %this.handCursor.setBitmap("scriptsAndAssets/data/ui/minigame/hand_cursor");
   %this.dragging = false;
}

function MazeDragger::onMouseLeave(%this, %modifier, %worldPosition, %mouseClicks )
{
   if (%this.dragging)
   {
      %this.lastCursorX = %this.getCenterX();
      %this.lastCursorY = %this.getCenterY();      
      Canvas.setCursorPos(%this.lastCursorX, %this.lastCursorY);
   }
}

function MazeDragger::castLight(%this, %x0, %y0)
{
   %cornerDist = MazeMinigame.fogMaskSize/2;
   //texture keeps its original size internally...
   %bX0 = MazeMinigame.getBoardX(%x0) - %cornerDist - %this.curSliderFacePosX;
   %bY0 = MazeMinigame.getBoardY(%y0) - %cornerDist - %this.curSliderFacePosY;
   
   %this.litMaze.maskCopy(%this.fogMask, %this.sliderFace, %bX0, %bY0, 0, 0);
   %this.litMaze.maskCopy(%this.fogMaskInv, %this.sliderFog, %bX0, %bY0, 0, 0);

   //if (%this.updateEventID > 0)
   //{
      //cancelScheduleLCA(%this.updateEventID);
      //%this.updateEventID = 0;
   //}

   %this.updateLitMaze(%x0, %y0);

   //%this.updateEventID = scheduleObjLCA(%this, "Minigame", 50, "updateLitMaze", 2, %x0 - MazeMinigame.getFaceX(%cornerDist), %y0 - MazeMinigame.getFaceY(%cornerDist));
}

function MazeDragger::updateLitMaze(%this, %x, %y)
{
   %this.litMaze.refreshBitmap();
   %this.litMaze.setCenterPos(%x, %y); 
   //%this.updateEventID = 0;
}

function MazeClicker::init(%this)
{
   %this.auxLineIter = LinePixelIterator::NewLinePixelIterator();
   
   for (%i = 0; %i < MazeMinigame.numOfItems; %i++)
   {
     %this.lineIter[%i] = LinePixelIterator::NewLinePixelIterator();
   }

   %this.lastDest = -1;   
   
   %this.initFollowingItems();

   MinigameRefCenter.pushToBack(MazeSkipButton);
   
   %this.flagSound = new SFXProfile()
   {
      filename = "scriptsAndAssets/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/audio/Plant_the_flag";
      description = "AudioGui50";
      preload = true;
   };
   
   %this.buggyDriveSound = new SFXProfile()
   {
      filename = "scriptsAndAssets/data/ui/minigame/maze/Common/Car-drive";
      description = "AudioGui50Loop";
      preload = true;
   };
   
   %this.buggyStartSound = new SFXProfile()
   {
      filename = "scriptsAndAssets/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/audio/start_the_golf_buggy";
      description = "AudioGui";
      preload = true;
   };
   
   %this.buggyStopSound = new SFXProfile()
   {
      filename = "scriptsAndAssets/data/ui/minigame/maze/"@playMinigameDlg.currentLevel@"/audio/stop_the_golf_buggy";
      description = "AudioGui";
      preload = true;
   };
}

function MazeClicker::initFollowingItems(%this)
{
   if (MazeMinigame.numOfItems $= "")
      MazeMinigame.numOfItems = 1;   
   
   for (%i = 0; %i < MazeMinigame.numOfItems; %i++)
   {
      if (MazeMinigame.itemSpeed[%i] $= "")
         MazeMinigame.itemSpeed[%i] = 50;
         
      MazeMinigame.itemSpeed[%i] = mRound(MazeMinigame.itemSpeed[%i]/10);

      %this.item[%i] = new GuiBitmapCtrl() {
         canSaveDynamicFields = "0";
         Enabled = "1";
         isContainer = "0";
         HorizSizing = "relative";
         VertSizing = "relative";
         Position = "0 0"; 
         Extent = "3 3";
         MinExtent = "2 2";
         canSave = "1";
         Visible = "1";
         hovertime = "1000";
         alpha = "1";
         color = "255 255 255 255";
         unitClip = "0 0 1 1";
         bitmap = "scriptsAndAssets/data/ui/minigame/maze/item";
         wrap = "0";
      };

      if (%i == 0)
         %this.item[%i].setCenterPos(MazeMinigame.getFaceX(MazeMinigame.itemStartPosX[%i]), MazeMinigame.getFaceY(MazeMinigame.itemStartPosY[%i]));
      MinigameRefCenter.add(%this.item[%i]);
      
      %this.visibleItem[%i] = new GuiPlasticBitmapCtrl(){
            canSaveDynamicFields = "0";
            Enabled = "1";
            isContainer = "0";
            Profile = "GuiContentProfile";
            HorizSizing = "relative";
            VertSizing = "relative";
            Position = "0 0";
            Extent = MazeMinigame.getFaceX(MazeMinigame.visibleItemExtentX[%i]) SPC MazeMinigame.getFaceY(MazeMinigame.visibleItemExtentY[%i]);
            MinExtent = "2 2";
            canSave = "1";
            Visible = "1";
            hovertime = "1000";
            alpha = "1";
            color = "255 255 255 255";
            unitClip = "0 0 1 1";
            useVariable = "0";
            tile = "0";      
            rotation = 0;
            deltaRotation = "0";
            noClip = "0";
            DML = MazeMinigame.visibleItemBitmap[%i];
         };

      if (%i == 0)
         %this.visibleItem[%i].setCenterPos(MazeMinigame.getFaceX(MazeMinigame.itemStartPosX[%i]), MazeMinigame.getFaceY(MazeMinigame.itemStartPosY[%i]));

      MinigameRefCenter.add(%this.visibleItem[%i]);

      %pathPos = MazeMinigame.itemPath[%i, 1];
      if (%pathPos $= "")      
         %this.itemNextDest[0] = 0;
      else
      {
         %this.itemOnCollision[%i] = false;         
       
         %posX = MazeMinigame.getFaceX(getWord(%pathPos, 0));
         %posY = MazeMinigame.getFaceY(getWord(%pathPos, 1));
         %this.item[%i].setCenterPos(%posX, %posY);
         %this.visibleItem[%i].setCenterPos(%posX, %posY);
         MazeMinigame.itemPathDir[%i] = -1;
         %this.itemNextDest[%i] = -2;
         %this.moveItem(%i);
      }
   }

   %this.flag = new GuiBitmapCtrl() {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      HorizSizing = "relative";
      VertSizing = "relative";
      Position = "-100 -100";
      Extent = "47 45";
      MinExtent = "2 2";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      bitmap = "scriptsAndAssets/data/ui/minigame/maze/flag";
      wrap = "0";
   };
   
   MinigameRefCenter.add(%this.flag);
}


function MazeClicker::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)
{
   %x = getWord(%worldPosition, 0) - getWord(%this.Position, 0);
   %y = getWord(%worldPosition, 1) - getWord(%this.Position, 1);
  
   // Compensate widescreen offset
   %rect2 = MinigameRefCenter.getPosition();
   %x -= getWord(%rect2,0);
   %y -= getWord(%rect2,1);      
     
   if (MazeMinigame.getColorType(MazeBoard.getPixelColor(MazeMinigame.getBoardX(%x), MazeMinigame.getBoardY(%y))) !$= "CLICK_POINT")
      return;
   
   if (%this.lastDest >= 0)
   {
      %lastBX = getWord(%this.destQueue[%this.lastDest], 0);
      %lastBY = getWord(%this.destQueue[%this.lastDest], 1);
   }
   else
   {
      %lastBX = MazeMinigame.getBoardX(%this.item[0].getCenterX());
      %lastBY = MazeMinigame.getBoardY(%this.item[0].getCenterY());
   }
   
   %bX = MazeMinigame.getBoardX(%x);
   %bY = MazeMinigame.getBoardY(%y);

   if (!%this.validMove(%lastBX, %lastBY, %bX, %bY))
      return;

   %this.destQueue[%this.lastDest++] = %bX SPC %bY;
   
   MazeMinigame.lastClickX = MazeMinigame.currentClickX;
   MazeMinigame.lastClickY = MazeMinigame.currentClickY;
   MazeMinigame.currentClickX = %x;
   MazeMinigame.currentClickY = %y;

   MazeMinigame.diffClickX = MazeMinigame.lastClickX - MazeMinigame.currentClickX;
   if(MazeMinigame.diffClickX < 0) MazeMinigame.diffClickX *= -1;
   
   MazeMinigame.diffClickY = MazeMinigame.lastClickY - MazeMinigame.currentClickY;
   if(MazeMinigame.diffClickY < 0) MazeMinigame.diffClickY *= -1;
   
   %this.flag.setPosition(%x , %y - MazeMinigame.getFaceY(39));
   
   sfxPlay(AudioButtonPress);

   for (%i = 0; %i < MazeMinigame.numOfItems; %i++)
   {
      if (%this.itemNextDest[%i] >= 0)
         %this.moveItem(%i);
   }
}

function MazeClicker::moveItem(%this, %i)
{
   //Verifying if the final destination is a different spot to play the BuggyStopSound
   if(%this.lastDest < %this.itemNextDest[%i]){          
      if((MazeMinigame.diffClickX >= 13)||(MazeMinigame.diffClickY >= 13)){ //13 = minimum distance betweent 2 spots
         //sfxStop(%this.buggyDriveSound);      
         sfxPlay(%this.buggyStopSound);
         MazeMinigame.canPlayBuggyStartSound = true;
		 if (%this.driveSfx !$= "")
		 {
			%this.driveSfx.stop();
		 }
      }
   }
   
   if (%this.anim[%i] || %this.lastDest < %this.itemNextDest[%i])
      return;
   
   if(%i == 0){ //just for the bug (item[0])
      if(MazeMinigame.canPlayBuggyStartSound){
         sfxPlay(%this.buggyStartSound);
         %this.driveSfx = sfxPlay(%this.buggyDriveSound);
         MazeMinigame.canPlayBuggyStartSound = false;
      }
   }
   
	if (%this.itemNextDest[%i] >= 0)
   {
      %destPos = %this.destQueue[%this.itemNextDest[%i]];

      %x = MazeMinigame.getFaceX(getWord(%destPos, 0));
      %y = MazeMinigame.getFaceY(getWord(%destPos, 1));
   }
   else
   {
      %destPos = MazeMinigame.itemPath[%i, -%this.itemNextDest[%i]];
      %destPos = MazeMinigame.getFaceX(getWord(%destPos, 0)) SPC MazeMinigame.getFaceY(getWord(%destPos, 1));

      %x = getWord(%destPos, 0);
      %y = getWord(%destPos, 1);
   }

   %itemPosX = getWord(%this.item[%i].getPosition(), 0);   
   %itemPosY = getWord(%this.item[%i].getPosition(), 1);   
   
   %fX = %x - mRound(getWord(%this.item[%i].getExtent(), 0) / 2);
   %fY = %y - mRound(getWord(%this.item[%i].getExtent(), 1) / 2);

   %lastBX = MazeMinigame.getBoardX(%itemPosX);
   %bX = MazeMinigame.getBoardX(%fX);

   %lastBY = MazeMinigame.getBoardY(%itemPosY);
   %bY = MazeMinigame.getBoardY(%fY);

   %this.lineIter[%i].init(%lastBX, %lastBY, %bX, %bY);
   
   if (%i == 0)
   {
      if (mAbs(%bX - %lastBX) > mAbs(%bY - %lastBY))
      {
         if (%bX > %lastBX)
            %this.visibleItem[0].rotation = -90;
         else
            %this.visibleItem[0].rotation = 90;
      }
      else
      {
         if (%bY > %lastBY)
            %this.visibleItem[0].rotation = 180;
         else
            %this.visibleItem[0].rotation = 0;
      }
   }
   
   %this.anim[%i] = true;
   scheduleObjLCA(%this, "Minigame", 100 + %i*50, "animMove", 1, %i);
}

function MazeClicker::validMove(%this, %bX0, %bY0, %bX, %bY)
{
   %lineIter = %this.auxLineIter;
   %lineIter.init(%bX0, %bY0, %bX, %bY);
   
   while (%lineIter.next())
   {
      if (MazeMinigame.getColorType(MazeBoard.getPixelColor(%lineIter.x, %lineIter.y)) $= "WALL")
         return false;
   }
   
   return true;
}

function MazeClicker::animMove(%this, %item)
{
   %cornerBDistX = MazeMinigame.getBoardX(getWord(%this.item[%item].getExtent(), 0));
   %cornerBDistY = MazeMinigame.getBoardY(getWord(%this.item[%item].getExtent(), 1));

   %lineIter = %this.lineIter[%item];   

   %lastX = %lineIter.x;
   %lastY = %lineIter.y;

   for (%k = 0; %k < MazeMinigame.itemSpeed[%item]; %k++)
   {
      if (!%lineIter.next())
         break;

      for (%i = 0; %i < 2; %i++)
      {
         %x = %lineIter.x + %i * %cornerBDistX; 
         for (%j = 0; %j < 2; %j++)
         {
            %y = %lineIter.y + %j * %cornerBDistY;
   
            %colorType = MazeMinigame.getColorType(MazeBoard.getPixelColor(%x, %y));
            switch$ (%colorType)
            {
               case "END":
                  MazeMinigame.gameFinished = true;
                  MazeMinigame.quit();
               case "WALL":
                  //sfxPlay(AudioWrongObject);
                  break;
            }
         }
         
         if (%j < 2)
            break;
      }

      if (%i < 2)
         break;

      %lastX = %lineIter.x;
      %lastY = %lineIter.y;
   }
   
   %x = MazeMinigame.getFaceX(%lastX);
   %y = MazeMinigame.getFaceY(%lastY);


   %this.item[%item].setPosition(%x, %y);
   %this.visibleItem[%item].setCenterPos(%this.item[%item].getCenterX(), %this.item[%item].getCenterY());
   
   if (%item != 0)
   {
      if (%this.item[%item].cornersCollideWith(%this.visibleItem[0]))
      {     
         if (!%this.itemOnCollision[%item])
         {
			sfxPlay(MazeMinigame.caughtSFX);
            MazeMinigame.onPlayerGot();      
            %this.itemOnCollision[%item] = true;
         }
      }
      else
      {     
         if (%this.itemOnCollision[%item])
         {
            %this.itemOnCollision[%item] = false;
         }
      }
   }
   
   if (%k == MazeMinigame.itemSpeed[%item])
         scheduleObjLCA(%this, "Minigame", 100, "animMove", 1, %item);
   else
   {
      if (%this.itemNextDest[%item] >= 0)
         %this.itemNextDest[%item]++;
      else
      {
         %reachedDestPos = MazeMinigame.itemPath[%item, -%this.itemNextDest[%item]];

         //item 0 crossed the path?         
         if (getWordCount(%reachedDestPos) == 3)
            %this.itemNextDest[%item] = getWord(%reachedDestPos, 2);
         else
         {
            %nextDest = %this.itemNextDest[%item] + MazeMinigame.itemPathDir[%item];
            
            if (%nextDest == 0)
            {
               %nextDest = -2;
               MazeMinigame.itemPathDir[%item] = -1;
            }
            else if (MazeMinigame.itemPath[%item, -%nextDest] $= "")
            {
               %nextDest = %this.itemNextDest[%item] + 1;
               MazeMinigame.itemPathDir[%item] = 1;
            }
            
            %this.itemNextDest[%item] = %nextDest;
         }
      }      
      
      if (%item == 0)
      {
         for (%i = 1; %i < MazeMinigame.numOfItems; %i++)
         {
            %j = 1;
            while (true)
            {
               %pos = MazeMinigame.itemPath[%i, %j];
               if (%pos $= "")
                  break;               
               
               %deltaX = MazeMinigame.getFaceX(getWord(%pos, 0)) - %this.item[0].getCenterX();
               %deltaY = MazeMinigame.getFaceY(getWord(%pos, 1)) - %this.item[0].getCenterY();

               if (%deltaX*%deltaX + %deltaY*%deltaY < 100)
               {
                  MazeMinigame.itemPath[%i, %j] = %pos SPC %this.itemNextDest[%item];
                  break;
               }
               
               %j++;
            }
         }
      }

      %this.anim[%item] = false;
      %this.moveItem(%item);
   }
}

function MazeSkipButton::onMouseEnter(%this){
   %this.setBitmap("scriptsAndAssets/data/ui/minigame/skip_rollover.png");
}

function MazeSkipButton::onMouseLeave(%this){
   %this.setBitmap("scriptsAndAssets/data/ui/minigame/skip.png");
}

function MazeSkipButton::onMouseDown(%this){
   %this.setBitmap("scriptsAndAssets/data/ui/minigame/skip_pressed.png");
}

//method stubs
function MazeMinigame::initSpecificGui(%this)
{
}

function MazeMinigame::onStart(%this)
{
}

