_G[gTable] = {};

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

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


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

   aState = 0 + pObj.mState;
	if aState >= 2 then
	   _G[aTmpVal].SetupParticleSystem(pObj, 1);
	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 == 1 and cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then

      -- Update object state
      pObj:StartFadeRects(PhFadeAnotherTimeMedium);
      aState         = aState + 1
      pObj.mState    = aState;
		pObj.mCurCell	= pObj.mCurCell + 1;
		pObj:StopParticleEmission(false);
		_G[aTmpVal].SetupParticleSystem(pObj, 1);

   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 == 2 then
   		local aNextLevel = pObj.mCsData:GetInt("NextLevel");
   		pObj.mPdata:SetNextLevel(aNextLevel);
   		pObj.mPdata:LevelComplete(true);
			pObj.mPdata:SetupCutScene(9);
   		cm.DisableObject(pObj);
      else
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime");
      end

		return true;

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

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

      -- Update game flags
		cm.SetGlobalValIndirect(pObj, "SetFlag1", true);

		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

-- Called on draw
_G[gTable].SetupParticleSystem =  function(pObj, pIndex)

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

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

end




