_G[gTable] = {};

local aTmpVal = gTable;

-- Particle system colors
local aCols    = {};
aCols[0] = { Color(77, 80,  5, 20), Color(239, 243, 58, 200), Color(87, 89, 11, 20) } -- Yellow
aCols[1] = { Color( 2, 83,  9, 20), Color(60,  242, 64, 255), Color(19, 94,  6, 20) } -- Green
aCols[2] = { Color(79,  0,  0, 20), Color(230,   0,  0, 240), Color(79,  0,  0, 20) } -- Red
aCols[3] = { Color(13,  7, 71, 20), Color(44,   69,245, 255), Color(6,   3, 75, 20) } -- Blue
aCols[4] = { Color(98,  4, 96, 20), Color(243,  56,243, 255), Color(95,  5, 92, 20) } -- Violet
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 >= 2 then
      local aFlameIndex = pObj.mCsData:GetInt("FlameIndex");
      _G[aTmpVal].StartPsys(pObj, aFlameIndex);
   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, pActingObj)

   pObj.mPdata:UnMouseObject();
   local aFlameIndex = pActingObj.mCsData:GetInt("FlameIndex");
	pObj.mCsData:SetVal("FlameIndex", aFlameIndex);
	pObj:SetEmitterColors(aCols[aFlameIndex][1], aCols[aFlameIndex][2], aCols[aFlameIndex][3]);
	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);
         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;

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

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

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

      if pObj.mState == 1 then

   		pObj.mState   = pObj.mState   + 1;
   		pObj.mCurCell = pObj.mCurCell + 1;
   		pObj:StopParticleEmission(false);
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
         cm.SetGlobalValIndirect(pObj, "SetFlag1", true);

         local aFlameIndex = pObj.mCsData:GetInt("FlameIndex");
         _G[aTmpVal].StartPsys(pObj, aFlameIndex);
   		return true;
		else

         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
         pObj.mPdata:UnMouseObject();
   		return true;
   	end
	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, pIndex)

   local aPsysName = pObj.mCsData:GetString("ParticleSystemName");
   local aPos1 = IcVector2d(30, 13) + pObj.mSmPos;
   local aPos2 = IcVector2d(0, 0) + pObj.mSmPos;

   pObj.mSmPos = aPos1;
   pObj:SetupParticleSystem(aPsysName);
	pObj:SetEmitterColors(aCols[pIndex][1], aCols[pIndex][2], aCols[pIndex][3]);
   pObj:StartParticleEmission(0);

   pObj.mSmPos = aPos2;
   pObj.mDiscovered = true;

end

