_G[gTable] = {};

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


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

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

	if pName == "" then

      if aState == 0 then

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

			pObj:StopParticleEmission(false);
         IcPlaySample("PH_SFX_CUPBOARDOPENS");

      elseif aState == 1 then
      
         local aSpawnObject = "SpawnObject1";
         local aSpawnPt     = 0;
         local aCellIncr    = 2;

         if pPolyNum == 1 then
            aState       = aState + 1;
         else
            aState       = aState + 2;
            aCellIncr	 = 1;
            aSpawnObject = "SpawnObject2";
            aSpawnPt     = 1;
         end

         if not cm.SpawnObject(pObj, aSpawnPt, pObj.mSmPos, aSpawnObject) then
            return true;
         end

         pObj:StartFadeRects(PhFadeAnotherTimeShort);
			pObj.mCurCell  = pObj.mCurCell + aCellIncr;
         pObj.mState    = aState;
         pObj:SetActive(0, pPolyNum, false);
         pObj:StopParticleEmission(false);
   		IcPlaySample("PH_SFX_OBJ_PICKUP");

      elseif aState == 2 then

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

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

         pObj:SetActive(0, pPolyNum, false);
         pObj:SetActive(0, 0, true);
         pObj:StopParticleEmission(false);
         IcPlaySample("PH_SFX_OBJ_PICKUP");
 
      elseif aState == 3 then

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

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

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

      elseif aState == 4 then

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

   		local aVal = cm.GetGlobalIntIndirect(pObj, "SetFlag1") + 1;
   		cm.SetGlobalValIndirect(pObj, "SetFlag1", aVal);
   		IcPlaySample("PH_SFX_WINDOWCLOSED");
      end

      return true;

	else
		cm.ObjDoesNotWork(pObj);
		return false;
	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);

	if (pObj.mState == 1) then
		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));
	end
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end




