_G[gTable] = {};

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

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


-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)
	aState = pObj.mState;
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

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

      elseif aState == 1 then

         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
         aState         = aState + 1;
   		pObj.mState		= aState;
   		pObj.mCurCell	= pObj.mCurCell + 1;
			pObj:SetActive(0, pPolyNum, false);
			pObj:SetActive(0, pPolyNum + 1, true);
			IcPlaySample("PH_SFX_CUPBOARDOPENS");

      elseif aState == 2 then

         if not cm.SpawnObject(pObj, 0, pObj.mSmPos, "SpawnObject") then
            return true;
         end

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

			pObj:SetActive(0, pPolyNum, false);
			pObj:SetActive(0, pPolyNum - 1, true);
   		IcPlaySample("PH_SFX_OBJ_PICKUP");

      elseif aState == 4 then

         pObj:StartFadeRects(PhFadeAnotherTimeReallyShort);
         aState         = aState + 1;
   		pObj.mState		= aState;
   		pObj.mCurCell	= 1;
   		cm.DisableObject(pObj);
			IcPlaySample("PH_SFX_WINDOWCLOSED");

   		local aVal = cm.GetGlobalIntIndirect(pObj, "SetFlag1") + 1;
   		cm.SetGlobalValIndirect(pObj, "SetFlag1", aVal);
      else
         cm.GetShowInfo(pObj, "InfoStr3", "InfoTime1");
      end
		return true;

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

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

		cm.ObjDoesNotWork(pObj);
		return false;

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

      if aState == 3 then
         pObj:StartFadeRects(PhFadeAnotherTimeShort);
         aState         = aState + 1;
   		pObj.mState		= aState;
   		pObj.mCurCell	= pObj.mCurCell + 1;
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
   		return true;
		end

		cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
		return false;

	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



