_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 >= 2 then
      _G[aTmpVal].SetupParticleSystem(pObj);
   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].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;
   local aState = 0 + pObj.mState;

	if pName == "" then

      if aState == 0 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");

      elseif aState == 1 then
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");

      else
         cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
      end
		return true;

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

      if aState == 0 then

         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
   		pObj.mState		  = aState        + 1;
   		pObj.mCurCell	  = pObj.mCurCell + 1;
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
   		return true;
		end

      _G[aTmpVal].MaybeLater(pObj);
		return true;

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

      if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         pObj.mPdata:UnMouseObject();
         if aState == 1 then
            cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
         else
            cm.GetShowInfo(pObj, "InfoStr6", "InfoTime6");
         end
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         return true;
      end

      if aState == 1 then

         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
   		pObj.mState		  = aState        + 1;
   		pObj.mCurCell	  = pObj.mCurCell + 1;
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
   		_G[aTmpVal].SetupParticleSystem(pObj);
   		cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
   		pObj.mGobj.mSoundPlayer:PlayLoopingSfx("PH_SFX_BURNINGFIRE");
   		return true;
		end

      _G[aTmpVal].MaybeLater(pObj);
		return true;

	else
      pObj.mPdata:UnMouseObject();
		cm.ObjDoesNotWork(pObj);
		return true;
	end

	return true;

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 (aState == 3 or aState == 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



-- Tell the player that this will not work
_G[gTable].MaybeLater =  function(pObj)
   pObj.mPdata:UnMouseObject();
   IcPlaySample("PH_SFX_DOES_NOT_WORK");
   cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
end

-- Called on draw
_G[gTable].SetupParticleSystem =  function(pObj)

   local aStr = pObj.mCsData:GetString("ParticleSystemName");
   pObj:SetupParticleSystem(aStr);
   pObj:StartParticleEmission(1);

end

