_G[gTable] = {};

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


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


-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)
	aState   = pObj.mState;
	aCurCell = pObj.mCurCell;
end


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

   if aState == 3 then

      -- Update object state
      aState            = aState + 1
      pObj.mState       = aState;
      cm.DisableObject(pObj);

      -- Update game flags
      cm.SetGlobalValIndirect(pObj, "SetFlag1", true);

		-- Make some noise
		if pObj.mCsData:IfExistsString("ObjSfxId") then
		   local aStr = pObj.mCsData:GetString("ObjSfxId");
		   IcPlaySample(aStr);
		end

	end

end


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


	pObj.mDiscovered = true;

	if pName == "" then

      cm.ShowInfo(pObj, "A heavy steel plate");
		return true;

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

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

		pObj:StopParticleEmission(false);
		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
		return true;

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

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

		pObj:StopParticleEmission(false);
		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
		return true;

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

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

		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






