_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 == 2 or aState == 5 or aState == 8 then
      if not pObj.mAlpha:AncIsChanging() then
         pObj:StartFadeRects(PhFadeAnotherTimeShort);
   		pObj.mState		 = aState        + 1;
   		pObj.mCurCell   = pObj.mCurCell - 1;
      end

   elseif aState == 3 or aState == 6 then
      if not pObj.mAlpha:AncIsChanging() then
   		pObj.mState		 = aState + 1;
   		cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
   		pObj:SetActive(0, 1, true);
   		pObj:StopParticleEmission(false);
      end

   elseif aState == 9 then
      if not pObj.mAlpha:AncIsChanging() then
   		pObj.mState		 = aState + 1;
   		cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
   		cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
   		pObj:StopParticleEmission(false);
      end

   end

end


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

   if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
      cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
      if pName ~= "" then
         pObj.mPdata:UnMouseObject();
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
      end
   	return true;
   end

   local aState = 0 + pObj.mState;

	pObj.mDiscovered = true;
	if pName == "" then

      if aState == 0 then
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");

      elseif cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag2") then
         pObj:StartFadeRects(PhFadeAnotherTimeShort);
   		pObj.mState		 = aState        + 1;
   		pObj.mCurCell   = pObj.mCurCell + 1;
   		pObj:SetActive(0, pPolyNum, false);
   		pObj:StopParticleEmission(false);
   		IcPlaySample("PH_SFX_LEVEROPERATED2");

      else
         cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
      end
      return true;

	elseif pName == pObj.mCsData:GetString("KeyObject1") then

      pObj:StartFadeRects(PhFadeAnotherTimeShort);
		pObj.mState		 = aState        + 1;
		pObj.mCurCell   = pObj.mCurCell + 1;
		pObj:SetActive(0, pPolyNum,     false);
		pObj:SetActive(0, pPolyNum + 1, true);
		pObj:StopParticleEmission(false);
		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
		return true;

	else
		cm.ObjDoesNotWork(pObj);
		return false;
	end

   return false;
end



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

	local aRpos = pObj.mSmPos;
	local aImg  = IcImageWrap(pObj.mSmallImage);
	assert(aImg);
	local aRect = aImg:GetCelRect(pObj.mCurCell);
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end


