_G[gTable] = {};

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

-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState = 0;
	aCounter    = 0;
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 a update
_G[gTable].OnUpdate = function(pObj)

	if pObj.mState == 1 and (not cm.GetGlobalBoolIndirect(pObj, "SetFlag2")) then
	   aCounter = aCounter + 1;
	   if aCounter > 150 then
	      aCounter = 0;
	      _G[aTmpVal].ShowHelp(pObj);
		end
	end

end


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

	pObj.mDiscovered = true;

	if pName == "" then

   	if pObj.mState == 1 then

         if not cm.SpawnObject(pObj, 0, pObj.mSmPos, "SpawnObject") then
            return true;
         end

         pObj:StartFadeRects(PhFadeAnotherTimeShort);
   		pObj.mState		 = 0;
   		pObj.mCurCell	 = 0;
   		pObj:SetActive(0, pPolyNum,     false);
   		pObj:SetActive(0, pPolyNum - 1, true);
   		pObj:StopParticleEmission(true);

   		IcPlaySample("PH_SFX_OBJ_PICKUP");
   		return true;
   	end

      cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
   	return true;

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


      if pObj.mState == 0 and cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then

         pObj:StartFadeRects(PhFadeAnotherTimeShort);
   		pObj.mState		 = 1;
   		pObj.mCurCell   = 1;

   		pObj:SetActive(0, pPolyNum,       false);
   		pObj:SetActive(0, pPolyNum + 1,   true);
   		pObj:StopParticleEmission(false);
   		
   		_G[aTmpVal].StartGlow(pObj);
   		cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
   		return true;
		end

		pObj.mPdata:UnMouseObject();
		cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
		IcPlaySample("PH_SFX_DOES_NOT_WORK");
		return true;

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

	return false;
end



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

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

end


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

-- Shows help
_G[gTable].ShowHelp = function(pObj)

	local aPpos		= IcVector2d(pObj.mCsData:GetInt("ParticleX"), pObj.mCsData:GetInt("ParticleY"));
	local aFPos		= aPpos + pObj.mSmPos;
	local aImg		= IcImageWrap("PH_IMG_CMN_HELP_BOX");
	local aFlip    = false;
	local aInfoStr	= pObj.mCsData:GetString("InfoStr2");

	if aFPos.x - aImg:GetCelWidth() < 0 then
		aFlip = true;
	end

	pObj.mPdata:ShowHelp(pObj.mGobj.mDataManager:GetString(aInfoStr),
		aFPos, aFlip, false);
   cm.SetGlobalValIndirect(pObj, "SetFlag2", true);

end