_G[gTable] = {};

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

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

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

	if pObj.mState == 1 then
      pObj.mDiscovered = true;
      _G[aTmpVal].SetupParticleSystem(pObj);
   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)

   -- See if the teleporter has been activated
	if pObj.mState == 0 then
      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
			pObj.mState   = pObj.mState + 1;
         pObj:StopParticleEmission(true);
         pObj.mDiscovered = true;
         _G[aTmpVal].SetupParticleSystem(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

		-- The teleporter is inactive
      if pObj.mState == 0 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
         return true;

      else

         -- Warp to next stage
  			local aNextLevel = pObj.mCsData:GetInt("NextLevel");
			pObj.mPdata:SetNextLevel(aNextLevel);
			pObj.mPdata:LevelComplete(pObj.mCsData:GetBool("NextStage"));
			pObj.mPdata:SetupCutScene(9);

			-- Update states
			cm.DisableObject(pObj);
			IcPlaySample("PH_SFX_PLAYERTELEPORTS");
			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);
	aRect = aImg:GetCelRect(pObj.mCurCell);
	pG:DrawImage(aImg:GetImage(), pObj.mSmPos.x, pObj.mSmPos.y, aRect);

end

-- Sets up the particle system
_G[gTable].SetupParticleSystem =  function(pObj)

   local aStart = Color(51, 9, 62, 10);
	local aMid   = Color(255, 255, 255, 60);
	local aEnd   = Color(40, 6, 21, 10);

   pObj.mDiscovered = true;
   local aPos1 = IcVector2d(0, 0)    + pObj.mSmPos;
   local aPos2 = IcVector2d(0, -300) + pObj.mSmPos;
   pObj.mSmPos = aPos2;
   pObj:SetupParticleSystem("PH_PSYS_LIGHT_STREAK_STATIONARY_BRIGHT");
   pObj:SetEmitterColors(aStart, aMid, aEnd);
   pObj:StartParticleEmission(1);
   pObj.mSmPos = aPos1;
   
   pObj.mGobj.mSoundPlayer:PlayLoopingSfx("PH_SFX_TELEPORTERHUM");

end

