_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;
	_G[aTmpVal].FireParticleSystem(pObj);
   pObj.mDiscovered = true;

end



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

	aState = pObj.mState;

	if aState < 5 then
		_G[aTmpVal].FireParticleSystem(pObj);
	elseif aState >= 7 then
	   _G[aTmpVal].StartTeleporterEffect(pObj);
	end

end

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

	if aState < 5 then
		--pObj.mGobj.mSoundPlayer:PlayLoopingSfx("PH_SFX_MAGICALFIRE");
	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 == 5 then
      if 4 == cm.GetGlobalIntIndirect(pObj, "ActivatorFlag1") then
         aState      = aState + 1;
         pObj.mState = aState;
         cm.SetGlobalValIndirect(pObj, "SetFlag2", true);
      end

   elseif aState == 6 then
      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag2") then
         aState      = aState + 1;
         pObj.mState = aState;
         _G[aTmpVal].StartTeleporterEffect(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 aState < 5 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         return true;

      elseif aState == 5 then
      
         if 4 ~= cm.GetGlobalIntIndirect(pObj, "ActivatorFlag1") then
            cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
            IcPlaySample("PH_SFX_DOES_NOT_WORK");
            return true;
         end

      elseif aState >= 6 then

         if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag2") then
            cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
            IcPlaySample("PH_SFX_DOES_NOT_WORK");
            return true;
         end

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

			-- Update states
         aState      = aState + 1;
         pObj.mState = aState;
			cm.DisableObject(pObj);
			IcPlaySample("PH_SFX_PLAYERTELEPORTS");
			return true;
      end
		return false;

	else

      if aState >= 5 then
   		cm.ObjDoesNotWork(pObj);
   		pObj.mPdata:UnMouseObject();
   		return true;
      end

      for aI = 1, 4 do

         local aKeyObj = string.format("KeyObject%d", aI);
         if pName == pObj.mCsData:GetString(aKeyObj) then

            if aState == aI then
         		cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
         		pObj.mPdata:UnMouseObject();
         		return true;

            else
               aState         = aI;
               pObj.mState    = aState;
               pObj:StopParticleEmission(false);
               _G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);

               if _G[aTmpVal].UpdateSolution(pObj, aState) then
                  aState      = 5;
                  pObj.mState = aState;
                  cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
                  cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
                  pObj.mGobj.mSoundPlayer:FadeoutLoopingSfx(40);
                  IcPlaySample("PH_SFX_GOOD_JOB");
                  return true;
               end

               _G[aTmpVal].FireParticleSystem(pObj);
               return true;
            end
         end

      end

		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

-- Used to fire particle system
_G[gTable].FireParticleSystem = function(pObj)

   local aStr        = string.format("ParticleSystemName%d", aState);
   local aPsysName   = pObj.mCsData:GetString(aStr);
   pObj:SetupParticleSystem(aPsysName);
   pObj:StartParticleEmission(0);

end

-- Used to update the solution string
_G[gTable].UpdateSolution = function(pObj, pIndex)

   local aSolution   = pObj.mCsData:GetString("SolutionString");
   local aSequence   = pObj.mCsData:GetString("SequenceString");
   local aStr        = string.format("%d", pIndex);
   aSequence = aSequence..aStr;
   if aSolution == aSequence then
      return true;
   end

   if #aSequence == 4 then
      aSequence   = "";
      aState      = 0;
      pObj.mState = aState;
      cm.GetShowInfo(pObj, "InfoStr6", "InfoTime6");
      IcPlaySample("PH_SFX_DOES_NOT_WORK");
   end
   pObj.mCsData:SetVal("SequenceString", aSequence);
   return false;

end

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

end

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

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(0);
   pObj.mSmPos = aPos1;
	pObj.mGobj.mSoundPlayer:PlayLoopingSfx("PH_SFX_TELEPORTERHUM");

end


