_G[gTable] = {};

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

-- Called on Init
_G[gTable].OnInit = function(pObj)
	aState = pObj.mState;
	pObj.mCsData:SetVal("DisappearEnd", 6);
end


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

-- Called after a level is loaded
_G[gTable].OnLevelLoad = function(pObj)
	aState = pObj.mState;
end

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

   if aState == 0 then
      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         aState =  aState + 1;
   		pObj.mState = aState;
      end

   elseif aState == 5 then
      if not pObj.mAlpha:AncIsChanging() then
         aState =  aState + 1;
   		pObj.mState = aState;
		end
	end

end


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

	pObj.mDiscovered = true;
	if aState == 0 then
      cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
      if pName == "" then
         return true;
      end
      return false;
   end

	if pName == "" then


   	if aState == 2 then

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

         pObj:StartFadeRects(PhFadeAnotherTimeReallyShort);
         aState           = aState + 1;
   		pObj.mState		  = aState;
   		pObj.mCurCell	  = pObj.mCurCell + 1;
   		pObj:SetActive(0, pPolyNum, false);
   		pObj:SetActive(0, pPolyNum + 1, true);
   		pObj:StopParticleEmission(false);
   		IcPlaySample("PH_SFX_OBJ_PICKUP");
   		return true;

   	elseif aState == 3 then

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

         pObj:StartFadeRects(PhFadeAnotherTimeReallyShort);
         aState           = aState + 1;
   		pObj.mState		  = aState;
   		pObj.mCurCell	  = pObj.mCurCell + 1;
   		pObj:SetActive(0, pPolyNum, false);
   		pObj:SetActive(0, pPolyNum + 1, true);
   		pObj:StopParticleEmission(false);
   		IcPlaySample("PH_SFX_OBJ_PICKUP");
   		return true;

   	elseif aState == 4 then

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

         aState           = aState + 1;
   		pObj.mState		  = aState;
   		pObj.mCurCell	  = pObj.mCurCell + 1;

			pObj.mDrawer	  = "DrawFadeAppearDisappear";
   		pObj.mAlpha:AncSetupChange(0.0, 255.0, PhFadeAnotherTimeReallyShort,
            IcAutoNumChgr.IC_ANCM_LINEAR);

   		cm.DisableObject(pObj);
   		IcPlaySample("PH_SFX_OBJ_PICKUP");
   		return true;
   	end

      cm.GetShowInfo(pObj, "LockedInfo", "LockedInfoTime");
   	return true;

   -- The chest is unlocked
	elseif pName == pObj.mCsData:GetString("KeyObject") then

      if aState == 1 then

         pObj:StartFadeRects(PhFadeAnotherTimeLong);
         aState           = aState + 1;
   		pObj.mState		  = aState;
   		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);
   		IcPlaySample("PH_SFX_CUPBOARDOPENS");
   		return true;
		end

		cm.ObjDoesNotWork(pObj);
		return false;

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

	return false;
end



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

	pG:PushState();
	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);

   pG:PopState();
end



-- Called on moused
_G[gTable].OnMoused =  function(pObj)
	print(2+2);
end

-- Called on Unmoused
_G[gTable].OnUnMoused =  function(pObj)
	print(2+2);
end

-- Zooms Out
_G[gTable].NormalSize = cm.ZoomOut;


-- Zooms In
_G[gTable].GetBigger = cm.ZoomIn;


