_G[gTable] = {};

local aTmpVal = gTable;
cm.SetupTable(_G[aTmpVal]);


-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState = 0;
end

-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)

end


-- Called on a update
_G[gTable].OnUpdate = function(pObj)

   local aState = 0 + pObj.mState;

   if aState == 0 then
      if cm.GetGlobalBoolIndirect(pObj, "PuzzleFlag") then

         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
         pObj.mState   = aState        + 1;
			pObj.mCurCell = pObj.mCurCell + 1;

			pObj:SetActive(0, 1, false);
			pObj:SetActive(0, 2, true);
         pObj:StopParticleEmission(false);
         
         cm.SetGlobalValIndirect(pObj, "SelectedMachine", pObj.mCurCell);
         IcPlaySample("PH_SFX_CHESTUNLOCKED");

      end
   end

end


-- Called on a succesful click
_G[gTable].OnClick =  function(pObj, pName, pActorObj, pPolyNum, pX, pY)

   local aState = 0 + pObj.mState;

	if pName == "" then
	
      if pPolyNum == 0 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");

      elseif pPolyNum == 1 then
         cm.LaunchPuzzle(pObj);

      else

         local aImg  = IcImageWrap(pObj.mSmallImage);
         local aCell = 1 + pObj.mCurCell;
         if aCell >= aImg:GetNumCells() then
            aCell = 1;
         end

         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
         pObj.mCurCell = aCell;
         cm.SetGlobalValIndirect(pObj, "SelectedMachine", aCell);
         IcPlaySample("PH_SFX_KNOBTURNEDON");

      end
		return true;

	else

      pObj.mPdata:UnMouseObject();
		cm.ObjDoesNotWork(pObj);
		return true;

   end

	return true;

end


-- Called on draw
_G[gTable].OnDraw =  function(pObj, pG)

	local aImg  = IcImageWrap(pObj.mSmallImage);
	assert(aImg);
	local aRpos = pObj.mSmPos;
	local aRect = aImg:GetCelRect(pObj.mCurCell);

	if (aState == 3 or aState == 1) then
		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));
	end
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end


