_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 == 1 then

      if not pObj.mAlpha:AncIsChanging() then
         pObj:StartFadeRects(PhFadeAnotherTimeShort);
         pObj.mState    = aState        + 1;
         pObj.mCurCell  = pObj.mCurCell - 1;
      end

   elseif aState == 2 then

      if not pObj.mAlpha:AncIsChanging() then
         pObj.mState = 0;
      end

   end

end


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

   if pName ~= "" then
      cm.ObjDoesNotWork(pObj);
      pObj.mPdata:UnMouseObject();
      return true;
   end

   -- See if the crank has been activated
   if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
      cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
      IcPlaySample("PH_SFX_DOES_NOT_WORK");
      return true;

   elseif (not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag2"))
      or (not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag3")) then
      cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
      IcPlaySample("PH_SFX_DOES_NOT_WORK");
      return true;
   end

   local aState     = 0 + pObj.mState;
	pObj.mDiscovered = true;

	if pName == "" then

      if aState == 0 then

         pObj:StartFadeRects(PhFadeAnotherTimeShort);
         pObj.mState    = aState        + 1;
         pObj.mCurCell  = pObj.mCurCell + 1;
         
         local aSelectedMachine = cm.GetGlobalIntIndirect(pObj, "SelectedMachine");

         local aStr = string.format("SetFlag%d", aSelectedMachine);

         if cm.GetGlobalBoolIndirect(pObj, aStr) then
            cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
            IcPlaySample("PH_SFX_DOES_NOT_WORK");
         else
            cm.SetGlobalValIndirect(pObj, aStr, true);
            IcPlaySample("PH_SFX_LEVEROPERATED");
         end

         pObj:StopParticleEmission(false);
         return true;

      end
		return true;

	end

	return false;

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);
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end




