_G[gTable] = {};

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

-- Called on Init
_G[gTable].OnInit = function(pObj)
   aState = pObj.mState;
	_G[aTmpVal].StartParticleSystem(pObj, 2);
end


-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)
   aState = pObj.mState;
   if aState == 0 then
      _G[aTmpVal].StartParticleSystem(pObj, 2);
	end

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

         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);
         pObj.mGobj.mSoundPlayer:PlaySfx("PH_SFX_WINDOWCLOSED");

      elseif aState == 1 then

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

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

			cm.DisableObject(pObj);
			cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
			IcPlaySample("PH_SFX_OBJ_PICKUP");

		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);
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end


-- Called to start the particle system
_G[gTable].StartParticleSystem = function(pObj, pIndex)

   pObj.mDiscovered = true;
   local aPsysName = pObj.mCsData:GetString("ParticleSystemName");
   pObj:SetupParticleSystem(aPsysName);
   pObj:StartParticleEmission(pIndex);

end

