_G[gTable] = {};

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


-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState = 0;
	aState      = pObj.mState;
end


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

	aState  = pObj.mState;
	if aState >= 2 then
	   _G[aTmpVal].StartTeleporterEffect(pObj);
	end

end

-- Called after a level is loaded
_G[gTable].OnLevelLoad = function(pObj)

	if aState >= 2 then
		pObj.mGobj.mSoundPlayer:PlayLoopingSfx("PH_SFX_TELEPORTERHUM");
	end

end

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

   pObj.mGobj.mSoundPlayer:FadeoutLoopingSfx(40);

end



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

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

         cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
         pObj:StartFadeRects(PhFadeAnotherTimeShort);
         aState         = aState + 1;
         pObj.mState    = aState;
         pObj.mCurCell  = pObj.mCurCell + 1;
         
         pObj:StopParticleEmission(false);
			local aNum = pObj:GetFirstActive(0);
			pObj:SetActive(0, aNum, false);
			pObj:SetActive(0, aNum + 1, true);

         pObj.mDiscovered = true;
		   pObj:SetupParticleSystem("PH_PSYS_BURST_SCROLL");
		   pObj:StartParticleEmission(0);
   		IcPlaySample("PH_SFX_MAGICTAKESEFFECT");

      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 aState == 0 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
         return true;

		elseif aState == 1 then
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
         return true;

		elseif pObj.mState == 2 then
         pObj.mPdata:SetNextLevel(pObj.mCsData:GetInt("NextLevel"));
			pObj.mPdata:LevelComplete(false);
   		IcPlaySample("PH_SFX_PLAYERTELEPORTS");
         return true;
		end
		return true;

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

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

      	_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
         cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
         _G[aTmpVal].StartTeleporterEffect(pObj);
         cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
         return true;
   	else
   		cm.ObjDoesNotWork(pObj);
   		return false;
   	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);
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end

-- Called on teleporter activation
_G[gTable].StartTeleporterEffect =  function(pObj)

   local aPos1 = IcVector2d(0, 0)    + pObj.mSmPos;
   local aPos2 = IcVector2d(0, -300) + pObj.mSmPos;
   pObj.mSmPos = aPos2;
   pObj:SetupParticleSystem("PH_PSYS_LIGHT_STREAK_STATIONARY");
   pObj:StartParticleEmission(2);
   pObj.mSmPos = aPos1;
	pObj.mGobj.mSoundPlayer:PlayLoopingSfx("PH_SFX_TELEPORTERHUM");

end


