_G[gTable] = {};

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

-- Called on Init
_G[gTable].OnInit = function(pObj)
	aState     = pObj.mState;
	aIsActive  = pObj.mCsData:GetBool("IsActivated");
end


-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)
	aState     = pObj.mState;
	aIsActive  = pObj.mCsData:GetBool("IsActivated");

	if aState >= 2 then
	   _G[aTmpVal].StartParticleSystem(pObj, 2);
	end

end

-- Called after a level is loaded
_G[gTable].OnLevelLoad = function(pObj)
	if pObj.mState >= 2 then
   	pObj.mGobj.mSoundPlayer:PlayLoopingSfx("PH_SFX_BURNINGFIRE");
   	if not pObj:ParticleEmitterOn() then
   	   _G[aTmpVal].StartParticleSystem(pObj, 2);
		end
	end
end

-- Called after a level is exited
_G[gTable].OnLevelExit = function(pObj)
   pObj.mGobj.mSoundPlayer:FadeoutLoopingSfx(40);
end


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

	-- See if the activation condition is met
   if not aIsActive then
      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         pObj.mCsData:SetVal("IsActivated", true);
         aIsActive = true;
      end
   end

end


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


	if pName == "" then
      if aState < 1 then
         pObj.mDiscovered = true;
      end
      
      if aState < 2 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
      else
         cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
      end
		return true;

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

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

	elseif pName == pObj.mCsData:GetString("KeyObject2") then
	
      if not aIsActive then
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
         return false;

		elseif aState == 1 then

			pObj:StartFadeRects(PhFadeAnotherTimeShort);
			pObj.mCurCell	= pObj.mCurCell + 1;
         aState         = aState + 1;
   		pObj.mState		= aState;
         _G[aTmpVal].StartParticleSystem(pObj, 2);

			pObj:SetActive(0, pPolyNum, false);
			pObj:SetActive(0, pPolyNum + 1, true);
			pObj.mGobj.mSoundPlayer:PlayLoopingSfx("PH_SFX_BURNINGFIRE");
			

   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
		   cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
		   return true;

      else
         -- Tell the player he's made a mistake
         cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
         return false;
      end

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

      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag2") then
         _G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
         return true;
      end
      cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
      return false;

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

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

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

      _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

-- 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


-- Returns the sparkle info
_G[gTable].GetParticleSystemInfo = function(pObj, pPolyNum, pName)

   local aParticleSystemTable	= {};
   aParticleSystemTable[0]		= true;
   aParticleSystemTable[1]		= "PH_PSYS_UNDYING_SPARKLE_BG";
   return aParticleSystemTable;

end


