_G[gTable] = {};

local aTmpVal   = gTable;
local aNumCells = 0;
local aFallTime = 25;
cm.SetupTable(_G[aTmpVal]);


-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState		= 0;
	pObj.mCurCell	= 1;
	aNumCells		= pObj.mCsData:GetInt("NumCells");
end

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

	aNumCells = pObj.mCsData:GetInt("NumCells");
	if pObj.mState == 3 then
      _G[aTmpVal].SetupParticleSystem(pObj);
   end

end

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

	aNumCells = pObj.mCsData:GetInt("NumCells");
   if pObj.mState == 3 then
   	pObj.mGobj.mSoundPlayer:PlayLoopingSfx("PH_SFX_BURNINGFIRE");
   	if not pObj:ParticleEmitterOn() then
   	   _G[aTmpVal].SetupParticleSystem(pObj);
		end
   end

end

-- Called after a level is exited
_G[gTable].OnLevelExit = function(pObj)
   pObj.mGobj.mSoundPlayer:FadeoutLoopingSfx(40);
end


-- Called to fetch cursor data
_G[gTable].GetCursorInfo = function(pObj, pPolyNum, pName)
	local aCursorTable	= {};
   aCursorTable[0]		= true;
   aCursorTable[1]		= IcGobj.CURSOR_POINTER;

   if pObj.mState == 4 then
      aCursorTable[1] = IcGobj.CURSOR_FOOT;
	end
   return aCursorTable;

end


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

   local aState = 0 + pObj.mState;
   if aState == 0 then

      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         pObj:StartFadeRects(aFallTime);
         pObj.mState    = aState + 1;
         pObj.mCurCell  = pObj.mCurCell + 1;
         IcPlaySample("PH_SFX_ICESLABSLIDES2");
      end

	elseif aState == 1 then
		if not pObj.mAlpha:AncIsChanging() then
   		local aCell = 1 + pObj.mCurCell;
   		if aCell <= aNumCells then
            pObj:StartFadeRects(aFallTime);
            pObj.mCurCell  = aCell;
            IcPlaySample("PH_SFX_ICESLABSLIDES2");
   		else
            pObj.mState = aState + 1;
            pObj.mPdata:SetupScreenShake(6, 40);
            pObj.mGobj.mSoundPlayer:PlaySfx("PH_SFX_STONESLABSFALLS");
         end
		end

	elseif aState == 2 then
		if not pObj.mAlpha:AncIsChanging() then
         pObj.mState = aState + 1;
         
         local aNum = pObj:GetFirstActive(0);
         pObj:SetActive(0, aNum,     false);
         pObj:SetActive(0, aNum + 2, true);
         _G[aTmpVal].SetupParticleSystem(pObj);
   		cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
         pObj.mGobj.mSoundPlayer:PlayLoopingSfx("PH_SFX_BURNINGFIRE");
		end

   elseif aState == 3 then

      local aLeverValue = cm.GetGlobalIntIndirect(pObj, "ButtonSum");
      if aLeverValue == pObj.mCsData:GetInt("Solution") then
         cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
         pObj.mState = aState + 1;
         pObj:StopParticleEmission(false);
         IcPlaySample("PH_SFX_GOOD_JOB");
         pObj.mGobj.mSoundPlayer:FadeoutLoopingSfx(40);
      end
	end

end


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

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

   local aState = 0 + pObj.mState;
   pObj.mDiscovered = true;

	if pName == "" then

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

      elseif aState == 3 then
         cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         return true;

      elseif aState == 4 then
         local aNextLevel = pObj.mCsData:GetInt("NextLevel");
   		pObj.mPdata:SetNextLevel(aNextLevel);

         local aNextStage = pObj.mCsData:GetBool("NextStage");
         pObj.mPdata:LevelComplete(aNextStage);
         pObj:StopParticleEmission(false);
         IcPlaySample("PH_SFX_FOOTSTEPSONGROUND");
         return true;
      end
		return true;

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

	if (aState == 3 or aState == 1) then
		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));
	end
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end

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

   pObj.mDiscovered = true;
   local aStr = pObj.mCsData:GetString("ParticleSystemName");
   pObj:StopParticleEmission(false);
   pObj:SetupParticleSystem(aStr);
   pObj:StartParticleEmission(1);

end



