_G[gTable] = {};

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


-- Called on Init
_G[gTable].OnInit = function(pObj)

	pObj.mState		 = 0;
	pObj.mDiscovered = true;

	pObj.mCsData:SetVal("Used", false);
	pObj.mCsData:SetVal("NumUsed", 0);
	pObj.mAlpha:AncSetupChange(0, 255.0, PhMaterializeTime,
      IcAutoNumChgr.IC_ANCM_LINEAR);
	_G[aTmpVal].MoveToSlot(pObj, PhKeyToSlotTime, true);

end


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

   if pObj.mState >= 1 then
      _G[aTmpVal].StartPsys(pObj);
   end

end

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

   if pObj.mState >= 1 then
	   if not pObj:ParticleEmitterOn() then
	   	_G[aTmpVal].StartPsys(pObj);
		end
   end

end

-- Called on use
_G[gTable].OnUse = function(pObj, pActingObj)

   if pActingObj.mName == pObj.mCsData:GetString("ActivatorObject") then

      _G[aTmpVal].StartBurn(pObj);

   else
   	pObj.mCsData:SetVal("Used", true);
   	pObj.mPdata.mBar:RemoveSlot(pObj.mId);
   	pObj.mPdata:UnMouseObject();
   	pObj.mAlpha:AncSetupChange(255.0, 0.0,
         PhVanish100, IcAutoNumChgr.IC_ANCM_LINEAR);
      cm.DisableObject(pObj);
      pObj:StopParticleEmission(false);
      IcPlaySample("PH_SFX_OBJ_USED");
   end
	

end


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

	local aKey = "MovingToSlot";
	if pObj.mCsData:GetBool(aKey) then
		if not pObj:SmIsMoving() then

	      pObj.mCsData:SetVal(aKey, false);
			pObj.mZ = pObj.mCsData:GetInt("PrevZ");
			IcPlaySample("PH_SFX_OBJ_IN_INV");

			if pObj.mState == 0 then
			   pObj.mDiscovered = false;
     			pObj:StopParticleEmission(false);
     			-- TEMPORARY!
     			--_G[aTmpVal].StartBurn(pObj);
         end

		end
	end

	if pObj.mCsData:GetBool("Used") then
		if not pObj.mAlpha:AncIsChanging() then
			pObj.mIsDone = true;
		end
	end

end


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

	if pName == "" then

      pObj.mPdata:MouseTheObject(pObj);
		IcPlaySample("PH_SFX_OBJ_PICKUP");
		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);

	if pObj.mCsData:GetBool("Used") or pObj.mCsData:GetBool("MovingToSlot") then
		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));
	end
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end



-- Called on moused
_G[gTable].OnMoused =  function(pObj)

end


-- Called on Unmoused
_G[gTable].OnUnMoused =  function(pObj)

	if not pObj.mCsData:GetBool("Used") then
		_G[aTmpVal].MoveToSlot(pObj, PhKeyToSlotTime, false);
	end

end



-- Moves the key to a slot
_G[gTable].MoveToSlot = cm.MoveToSlot;

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

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

end

-- Start to burn the torch
_G[gTable].StartBurn =  function(pObj)
   pObj.mState = pObj.mState + 1;
   _G[aTmpVal].StartPsys(pObj);
   cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
   IcPlaySample("PH_SFX_FIRESTART");
end


