_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.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].StartGlow(pObj);
	end

end


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

	pObj.mCsData:SetVal("Used", true);
	pObj.mPdata.mBar:RemoveSlot(pObj.mId);
	pObj.mAlpha:AncSetupChange(255.0, 0.0,
      PhKeyVanishAfterUse, IcAutoNumChgr.IC_ANCM_LINEAR);
   pObj:DisablePolys();
	IcPlaySample("PH_SFX_OBJ_USED");

	if pObj.mCsData:IfExistsString("SpawnObject") then
      cm.SpawnObjectNoCheck(pObj, 0, pObj.mSmPos, "SpawnObject");
	end
	if pObj.mCsData:IfExistsString("SpawnGlyphObject") then
      cm.SpawnObjectNoCheck(pObj, 0, pObj.mSmPos, "SpawnGlyphObject");
	end

end


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

	if pObj.mCsData:GetBool("MovingToSlot") then

      if not pObj:SmIsMoving() then
			pObj.mDiscovered = false;
			pObj.mZ	= pObj.mCsData:GetInt("PrevZ");
			pObj:StopParticleEmission(false);
			pObj.mCsData:SetVal("MovingToSlot", false);
			IcPlaySample("PH_SFX_OBJ_IN_INV");
		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
		assert(pObj.mCurImg == 0);

      if pObj.mState == 1 then
			pObj.mState = 2;
			local aFlagName = pObj.mCsData:GetString("SetLevelFlag");
			pObj.mPdata.mCsData:SetVal(aFlagName, true);
			_G[pObj.mScriptTag].OnUse(pObj);
			cm.DisableObject(pObj);
			return true;

		elseif pObj.mState == 0 then
			cm.ShowInfo(pObj, "The scroll is blank. Maybe a spell needs to be written on it", 260);
			return true;
		end

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

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

		if pObj.mCsData:IfExistsString("SpawnOnWriteObject") then
	      cm.SpawnObjectNoCheck(pObj, 0, pObj.mSmPos, "SpawnOnWriteObject");
		end

      _G[aTmpVal].StartGlow(pObj);
		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;

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

	pObj.mDiscovered = true;
   pObj:SetupParticleSystem("PH_PSYS_GEN_SCROLL_SPARKLE");
   pObj:StartParticleEmission(1);

end
