_G[gTable] = {};

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


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


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

   if pObj.mState > 0 then
      _G[aTmpVal].SetupParticleSystem(pObj);
   end

end


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

   if pObj.mState > 0 then
   	pObj.mGobj.mSoundPlayer:PlayLoopingSfx("PH_SFX_BURNINGFIRE");
   	if not pObj:ParticleEmitterOn() then
   	   _G[aTmpVal].SetupParticleSystem(pObj);
		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)


end


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

	pObj.mDiscovered = true;
	if pName == "" then

      if pObj.mState == 0 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
      elseif pObj.mState == 1 then
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
      elseif pObj.mState >= 2 then
         cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
      end
      return true;

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

      if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         pObj.mPdata:UnMouseObject();
         cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         return true;
      end

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

		cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
      cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
		_G[aTmpVal].SetupParticleSystem(pObj, 0);
		return true;

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

      if pObj.mState ~= 1 then
         pObj.mPdata:UnMouseObject();
         cm.GetShowInfo(pObj, "InfoStr6", "InfoTime6");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         return true;
      end

      pObj:StartFadeRects(PhFadeAnotherTimeShort);
      pObj.mState    = pObj.mState + 1;
      pObj.mCurCell  = pObj.mCurCell + 1;
      pObj.mCsData:SetVal("ParticleSystemIndex", 1);
      pObj:StopParticleEmission(false);

      cm.GetShowInfo(pObj, "InfoStr7", "InfoTime7");
		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
		_G[aTmpVal].SetupParticleSystem(pObj, 1);
		return true;

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

      if pObj.mState ~= 2 then
         pObj.mPdata:UnMouseObject();
         
         if pObj.mState == 3 then
            if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag2") then
               cm.GetShowInfo(pObj, "InfoStr8", "InfoTime8");
            else
               cm.GetShowInfo(pObj, "InfoStr10", "InfoTime10");
            end
         elseif pObj.mState == 1 then
            cm.GetShowInfo(pObj, "InfoStr9", "InfoTime9");
         else
            cm.GetShowInfo(pObj, "InfoStr6", "InfoTime6");
         end
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         return true;
      end

      pObj.mState = pObj.mState + 1;
		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
		return true;

	elseif pName == pObj.mCsData:GetString("KeyObject4") 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 setup & start the particle system
_G[gTable].SetupParticleSystem =  function(pObj, pIndex)

   local aIndex = pObj.mCsData:GetInt("ParticleSystemIndex");
   if pIndex then
      aIndex = pIndex;
   end

   local aStr  = string.format("PH_PSYS_S05_L01_COAL_FIRE_0%d", aIndex);
   pObj:SetupParticleSystem(aStr);
   pObj:StartParticleEmission(1);
	pObj.mGobj.mSoundPlayer:PlayLoopingSfx("PH_SFX_BURNINGFIRE");

end

