_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)

end


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

	pObj.mDiscovered = true;

	if pName == "" then

      -- Initial case
      if pObj.mState == 0 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         return true;

      -- Lever Oiled
      elseif pObj.mState == 1 then

         if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
            cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
            IcPlaySample("PH_SFX_DOES_NOT_WORK");
            return true;
         end

         pObj:StartFadeRects(PhFadeAnotherTimeShort);
   		pObj.mState     = pObj.mState + 1;
   		pObj.mCurCell   = pObj.mCurCell + 1;
   		cm.DisableObject(pObj);
         
         cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
         IcPlaySample("PH_SFX_LEVEROPERATED2");
         return true;

      end
      return true;

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

      -- Oil the jammed lever
      if pObj.mState == 0 then

         pObj:StartFadeRects(PhFadeAnotherTimeShort);
         cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
   		pObj.mState     = pObj.mState + 1;
   		pObj.mCurCell   = pObj.mCurCell + 1;
   		pObj:StopParticleEmission(false);
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
   		return true;

      -- Lever already oiled
		else
         cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
         return false;
		end

	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



