_G[gTable] = {};
local aTmpVal = gTable;

local aMovingToSlot = false;
local aDownsizing   = false;
local aRuneInfo		= { {mActive = false, mDone = false, mAlpha = 0, mX = 494, mY = 54},
						{mActive = false, mDone = false, mAlpha = 0, mX = 576, mY = 102},
						{mActive = false, mDone = false, mAlpha = 0, mX = 577, mY = 197},
						{mActive = false, mDone = false, mAlpha = 0, mX = 494, mY = 246},
						{mActive = false, mDone = false, mAlpha = 0, mX = 411, mY = 198},
						{mActive = false, mDone = false, mAlpha = 0, mX = 416, mY = 95}
					  };
local aFilledSlots = 0;
local aNotLevelDone= true;
cm.SetupTable(_G[aTmpVal]);



-- Called on Init
_G[gTable].OnInit = function(pObj)
	for i = 1, 6 do
		aRuneInfo[i].mActive = false;
		aRuneInfo[i].mDone	 = false;
		aRuneInfo[i].mAlpha  = 0;
	end
	aMovingToSlot = false;
	aDownsizing   = false;
	aFilledSlots  = 0;
	aNotLevelDone = true;

	_G[aTmpVal].LoadInfo(pObj);
	_G[aTmpVal].SaveInfo(pObj);
	pObj.mAlpha:AncSetCurrent(255);
	pObj.mDiscovered = true;
	_G[aTmpVal].MoveToSlot(pObj, PhKeyToSlotTime, true);
end


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


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

	if aMovingToSlot then
		if not pObj:SmIsMoving() then
			aMovingToSlot	 = false;
			pObj.mDiscovered = false;
			pObj.mZ			 = pObj.mCsData:GetInt("PrevZ");
			pObj:StopParticleEmission(false);
			_G[aTmpVal].SaveInfo(pObj);
			IcPlaySample("PH_SFX_OBJ_IN_INV");

	      if not pObj.mCsData:GetBool("HelpShown1") then
	         local aImg = IcImageWrap(pObj.mSmallImage);
	         local aPos = IcVector2d(aImg:GetCelWidth()/2, aImg:GetCelHeight()/2) + pObj.mSmPos;
				local aInfoStr  = pObj.mCsData:GetString("InfoStr3");
				pObj.mPdata:ShowHelp(pObj.mGobj.mDataManager:GetString(aInfoStr),
					aPos, true, false);
				pObj.mCsData:SetVal("HelpShown1", true);
			end

		end
	end

	if aDownsizing then
		if not pObj.mAlpha:AncIsChanging() then
			aDownsizing = false;
			pObj.mCurImg= 0;
			pObj.mAlpha:AncSetCurrent(255);
			_G[aTmpVal].SaveInfo(pObj);
		end
	end

   local aNumFrames = 60;
   local aCallFactor= 2;
   local aAdd = (255/aNumFrames)*aCallFactor;

	for i = 1, 6 do
		if (not aRuneInfo[i].mDone) and aRuneInfo[i].mActive then
			aRuneInfo[i].mAlpha = aRuneInfo[i].mAlpha + aAdd;

			if aRuneInfo[i].mAlpha >= 255 then
				aRuneInfo[i].mAlpha = 255;
				aRuneInfo[i].mDone = true;
				_G[aTmpVal].SaveInfo(pObj);
			end

		end
	end
	
	if aFilledSlots == 6 then

		if pObj.mCurImg == 1 and pObj.mState == 0 then

		   pObj.mState = pObj.mState + 1;
			_G[aTmpVal].NormalSize(pObj);
			pObj.mCb:Disappear();
			cm.DisableObject(pObj);

		elseif aNotLevelDone then
     		cm.SetGlobalValIndirect(pObj, "SetFlag2", true);
     		aNotLevelDone = false;
		end

	end

end


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

	if aDownsizing then
		return true;
	end

	if pName == "" then

		if pObj.mCurImg == 0 then
			_G[aTmpVal].GetBigger(pObj, PhGetBigTime);
			return true;
		else

		   if pPolyNum == 6 then
		      cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
		   else
		      cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
			end
		end
		return true;

	elseif pObj.mCurImg == 0 then

		cm.ObjDoesNotWork(pObj);
		IcPlaySample("PH_SFX_DOES_NOT_WORK");
		return false;

	else

		if pName == "InvRune00" then

			if pPolyNum == 0 then
				aRuneInfo[pPolyNum + 1].mActive = true;
				pObj:SetActive(1, pPolyNum, false);
				pObj:StopParticleEmission(false);
				aFilledSlots = aFilledSlots + 1;
				_G[aTmpVal].SaveInfo(pObj);
				_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
				return true;
			end
			cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
			return true;

		elseif pName == "InvRune01" then

			if pPolyNum == 1 then
				aRuneInfo[pPolyNum + 1].mActive = true;
				pObj:SetActive(1, pPolyNum, false);
				pObj:StopParticleEmission(false);
				aFilledSlots = aFilledSlots + 1;
				_G[aTmpVal].SaveInfo(pObj);
				_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
				return true;
			end
			cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
			return true;

		elseif pName == "InvRune02" then

			if pPolyNum == 2 then
				aRuneInfo[pPolyNum + 1].mActive = true;
				pObj:SetActive(1, pPolyNum, false);
				pObj:StopParticleEmission(false);
				aFilledSlots = aFilledSlots + 1;
				_G[aTmpVal].SaveInfo(pObj);
				_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
				return true;
			end
			cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
			return true;

		elseif pName == "InvRune03" then

			if pPolyNum == 3 then
				aRuneInfo[pPolyNum + 1].mActive = true;
				pObj:SetActive(1, pPolyNum, false);
				pObj:StopParticleEmission(false);
				aFilledSlots = aFilledSlots + 1;
				_G[aTmpVal].SaveInfo(pObj);
				_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
				return true;
			end
			cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
			return true;

		elseif pName == "InvRune04" then

			if pPolyNum == 4 then
				aRuneInfo[pPolyNum + 1].mActive = true;
				pObj:SetActive(1, pPolyNum, false);
				pObj:StopParticleEmission(false);
				aFilledSlots = aFilledSlots + 1;
				_G[aTmpVal].SaveInfo(pObj);
				_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
				return true;
			end
			cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
			return true;

		elseif pName == "InvRune05" then

			if pPolyNum == 5 then
				aRuneInfo[pPolyNum + 1].mActive = true;
				pObj:SetActive(1, pPolyNum, false);
				pObj:StopParticleEmission(false);
				aFilledSlots = aFilledSlots + 1;
				_G[aTmpVal].SaveInfo(pObj);
				_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
				return true;
			end
			cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
			return true;

		end
		cm.ObjDoesNotWork(pObj);
		IcPlaySample("PH_SFX_DOES_NOT_WORK");
	end
	return false;

end


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


	if pObj.mCurImg == 0 then

		local aImg  = IcImageWrap(pObj.mSmallImage);
		assert(aImg);
		local aRpos = pObj.mSmPos;
		local aRect = aImg:GetCelRect(pObj.mCurCell);
		pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

	else

		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, 255 - pObj.mAlpha:AncGetVal()));

		local aImg  = IcImageWrap(pObj.mSmallImage);
		assert(aImg);
		local aRpos = pObj.mSmPos;
		local aRect = aImg:GetCelRect(pObj.mCurCell);
		pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));
		aImg  = IcImageWrap(pObj.mBigImage);
		assert(aImg);
		aRpos = pObj.mBigPos;
		aRect = aImg:GetCelRect(pObj.mCurCell);
		pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);	

		local aTmpAlpha = pObj.mAlpha:AncGetVal()/255.0;
		aImg = IcImageWrap("PH_IMG_STG0_RUNES_FULL");
		assert(aImg);
		for i = 1, 6 do
			if aRuneInfo[i].mActive then
				pG:SetColor(Color(255, 255, 255, aRuneInfo[i].mAlpha*aTmpAlpha));
				aRpos = IcVector2d(aRuneInfo[i].mX, aRuneInfo[i].mY);
				aRpos = pObj.mBigPos + aRpos;
				aRect = aImg:GetCelRect(i - 1);
				pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);	
			end
		end

	end

end


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

end

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

end

-- Called to fetch mouse over info
_G[gTable].GetParticleSystemInfo = function(pObj, pPolyNum, pName)

   local aParticleSystemTable = {};
   aParticleSystemTable[0] = true;
   aParticleSystemTable[1] = "PH_PSYS_UNDYING_SPARKLE";
   
   if pPolyNum == 6 then
      aParticleSystemTable[0] = false;
   end
   return aParticleSystemTable;
end


-- Called to reduce the size
_G[gTable].NormalSize =  function(pObj)
	aDownsizing = true;
	pObj.mAlpha:AncSetupChange(255.0, 0.0, PhGetBigTime, IcAutoNumChgr.IC_ANCM_LINEAR);
	IcPlaySample("PH_SFX_CLOSER_LOOK");
end


-- Called on draw
_G[gTable].GetBigger =  function(pObj, aTime1)

	pObj.mCurImg = 1;
	pObj.mAlpha:AncEndChange();
	pObj.mAlpha:AncSetupChange(0.0, 255.0, aTime1, IcAutoNumChgr.IC_ANCM_LINEAR);
	
	local aImg1 = IcImageWrap(pObj.mBigImage);
	pObj.mBigPos.x = (gGobj.mWidth - aImg1:GetCelWidth())/2 + 16;
	pObj.mBigPos.y = gGobj.mHeight - pObj.mPdata.mBar:GetWorkingHeight() - aImg1:GetCelHeight();
	pObj.mFrmPos = pObj.mBigPos;

	pObj:StopParticleEmission(false);
	
   local aImg3 = IcImageWrap("PH_IMG_CMN_ZOOM_BOX_CLOSE");
   local aRect3= aImg3:GetCelRect(0);
	local aCentrePos = IcVector2d(690, 211);
	local aCbPos = aCentrePos - IcVector2d(aRect3.mWidth/2, aRect3.mHeight/2);

	pObj.mCb:SetPos(aCbPos);
	pObj.mCb:StartAppearing(aTime1, aTime1);

	pObj.mCsData:SetValf("CbPosX", aCbPos.x);
	pObj.mCsData:SetValf("CbPosY", aCbPos.y);
	pObj.mCsData:SetVal("Time1", aTime1);
	IcPlaySample("PH_SFX_CLOSER_LOOK");

	if (not pObj.mCsData:GetBool("HelpShown2")) and pObj.mCsData:GetBool("HelpShown1") then
		local aInfoStr  = pObj.mCsData:GetString("InfoStr4");
		pObj.mPdata:ShowHelp(pObj.mGobj.mDataManager:GetString(aInfoStr),
			(pObj.mBigPos + aCentrePos), false, false);
		pObj.mCsData:SetVal("HelpShown2", true);
		cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
	end

end


-- Moves the object to its slot
_G[gTable].MoveToSlot = function(pObj, pTime, pShowParticles)

	local aSlot = pObj.mPdata.mBar:FindSlot(pObj.mId);
	if not aSlot then
		pObj.mPdata.mBar:AddSlot(pObj.mId);
		aSlot = pObj.mPdata.mBar:FindSlot(pObj.mId);
	end

	local aImg = IcImageWrap(pObj.mSmallImage);
	local aX = aSlot.mMover.mSmPos.x;
	local aY = aSlot.mMover.mSmPos.y;
	aX = aX + (pObj.mPdata.mBar:GetSlotWidth()  - aImg:GetCelWidth())/2;
	aY = aY + (pObj.mPdata.mBar:GetSlotHeight() - aImg:GetCelHeight())/2;
	pObj:SmSetupMove(IcVector2d(aX, aY), IcSimpleMovement.IC_SMM_DECCEL, pTime);
	
	if pShowParticles then
		pObj:SetupParticleSystem("PH_PSYS_UNDYING_TRAIL");
		pObj:StartParticleEmission(pObj:GetFirstActive(0));
	end

	aMovingToSlot    = true;
	aSlot.mOccupied  = true;
	aSlot.mPobj		 = pObj;
	pObj.mDiscovered = true;
	pObj.mCsData:SetVal("PrevZ", pObj.mZ);
	pObj.mZ = pObj.mZ + 10;
	_G[aTmpVal].SaveInfo(pObj);

end


-- Loads info from the object
_G[gTable].LoadInfo =  function(pObj)
	local aKey;

	aKey = "MovingToSlot";
	if pObj.mCsData:IfExistsBool(aKey) then
		aMovingToSlot = pObj.mCsData:GetBool(aKey);
	end

	aKey = "Downsizing";
	if pObj.mCsData:IfExistsBool(aKey) then
		aDownsizing = pObj.mCsData:GetBool(aKey);
	end

	aKey = "FilledSlots";
	if pObj.mCsData:IfExistsInt(aKey) then
		aFilledSlots = pObj.mCsData:GetInt(aKey);
	end

	aKey = "NotLevelDone";
	if pObj.mCsData:IfExistsBool(aKey) then
		aNotLevelDone = pObj.mCsData:GetBool(aKey);
	end

	for i = 1, 6 do
		aKey = string.format("Active%d", i);
		if pObj.mCsData:IfExistsBool(aKey) then
			aRuneInfo[i].mActive = pObj.mCsData:GetBool(aKey);
		end

		aKey = string.format("Done%d", i);
		if pObj.mCsData:IfExistsBool(aKey) then
			aRuneInfo[i].mDone = pObj.mCsData:GetBool(aKey);
		end

		aKey = string.format("Alpha%d", i);
		if pObj.mCsData:IfExistsInt(aKey) then
			aRuneInfo[i].mAlpha = pObj.mCsData:GetInt(aKey);
		end
	end
end

-- Saves info to the object
_G[gTable].SaveInfo =  function(pObj)
	local aKey;

	aKey = "MovingToSlot";
	pObj.mCsData:SetVal(aKey, aMovingToSlot);

	aKey = "Downsizing";
	pObj.mCsData:SetVal(aKey, aDownsizing);

	aKey = "FilledSlots";
	pObj.mCsData:SetVal(aKey, aFilledSlots);

	aKey = "NotLevelDone";
	pObj.mCsData:SetVal(aKey, aNotLevelDone);

	for i = 1, 6 do
		aKey = string.format("Active%d", i);
		pObj.mCsData:SetVal(aKey, aRuneInfo[i].mActive);

		aKey = string.format("Done%d", i);
		pObj.mCsData:SetVal(aKey, aRuneInfo[i].mDone);

		aKey = string.format("Alpha%d", i);
		pObj.mCsData:SetVal(aKey, aRuneInfo[i].mAlpha);
	end

end

