
_G[gTable] = {};

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


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

   aUsed            = false;
	pObj.mState      = 0;
   aState           = pObj.mState;
	pObj.mDiscovered = true;
	pObj.mCsData:SetVal("Used", aUsed);
	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)
   aUsed    = pObj.mCsData:GetBool("Used");
   aState   = pObj.mState;
   if aState >= 2 then
      _G[aTmpVal].StartGlow(pObj);
   end
end

-- Called after exiting a level
_G[gTable].OnLevelExit = function(pObj)

   if pObj.mPdata:GetNextLevel() == pObj.mCsData:GetInt("EndLevel") then
      pObj.mIsDone = true;
   	pObj.mCsData:SetVal("Used", true);
   	pObj.mPdata.mBar:RemoveSlot(pObj.mId);
   	pObj.mAlpha:AncSetupChange(255.0, 0.0, PhKeyVanishAfterUse,
         IcAutoNumChgr.IC_ANCM_LINEAR);
   end

end

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

   aUsed = true;
	pObj.mCsData:SetVal("Used", aUsed);

	pObj.mPdata.mBar:RemoveSlot(pObj.mId);
	pObj.mPdata:UnMouseObject();
	pObj.mAlpha:AncSetupChange(255.0, 0.0,
      PhKeyVanishAfterUse, IcAutoNumChgr.IC_ANCM_LINEAR);
   pObj:StopParticleEmission(false);
   pObj:DisablePolys();
	IcPlaySample("PH_SFX_OBJ_USED");

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 < 2 then
			   pObj.mDiscovered = false;
     			pObj:StopParticleEmission(false);
         else
            _G[aTmpVal].StartGlow(pObj);
			end

		end
	end

	if aUsed 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;

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

      if aState == 0 then
         aState         = aState + 1;
         pObj.mState    = aState;
         pObj:StopParticleEmission(false);

      	_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
   	else
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         pObj.mPdata:UnMouseObject();
   	end
   	return true;

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

      if aState == 1 then
         aState         = aState + 1;
         pObj.mState    = aState;
         pObj.mCurCell  = pObj.mCurCell + 1;
         pObj:StopParticleEmission(false);

         _G[aTmpVal].StartGlow(pObj);
      	_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
      	cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
   	else
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         pObj.mPdata:UnMouseObject();
   	end
		return true;

	else
		cm.ObjDoesNotWork(pObj);
		return false;
	end

	return false;

end


-- Called on draw
_G[gTable].OnDraw =  function(pObj, pG)

	if pObj.mIsDone then
		return;
	end
	pG:PushState();

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

	pG:PopState();

end


-- Called on moused
_G[gTable].OnMoused =  function(pObj)
   pObj.mDiscovered = false;
   pObj:StopParticleEmission(true);
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;

-- Starts particle system glow
_G[gTable].StartGlow = function(pObj)

	pObj.mDiscovered = true;
	local aOldPos = IcVector2d(0, 0) + pObj.mSmPos;
	local aPpos   = IcVector2d(pObj.mCsData:GetInt("ParticleX"), pObj.mCsData:GetInt("ParticleY"));
	pObj.mSmPos	  = aPpos + pObj.mSmPos;
   pObj:SetupParticleSystem("PH_PSYS_UNDYING_TORCH_GLOW");
   pObj:StartParticleEmission(0);
   pObj.mSmPos   = aOldPos;

end
