
_G[gTable] = {};

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

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

-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)
	aState   = pObj.mState;
	aCurCell = pObj.mCurCell;
end

--Called after level being reloaded
_G[gTable].OnLevelLoad = function(pObj)
	aState   = pObj.mState;
	aCurCell = pObj.mCurCell;
end


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

   if aState > 3 then
      aCursorTable[1] = IcGobj.CURSOR_FOOT;
	end
   return aCursorTable;

end


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

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

         pObj:StartFadeRects(aFadeTime);
         aState         = aState + 1;
   		pObj.mState    = aState;
         aCurCell       = aCurCell + 1;
   		pObj.mCurCell  = aCurCell;
   		pObj:StopParticleEmission(false);

			local aNum = pObj:GetFirstActive(0);
   		pObj:SetActive(0, aNum, false);
   		pObj.mGobj.mSoundPlayer:PlaySfx("PH_SFX_CRYSTALSHATTERS");
      end

	elseif aState == 1 then

      local aEndCell = 3;
      if not pObj.mAlpha:AncIsChanging() then

         aCounter = 0;
       	if pObj.mCurCell < aEndCell then
	         pObj:StartFadeRects(aFadeTime);
	         aCurCell       = aCurCell + 1;
	   		pObj.mCurCell  = aCurCell;
	         pObj:StopParticleEmission(false);
	         
			else
	         aState         = aState + 1;
	   		pObj.mState    = aState;
	   		pObj:StopParticleEmission(false);
	   		pObj:SetActive(0, 1, true);
	   		pObj:SetActive(0, 3, false);
   			pObj.mDiscovered = true;

			end

		else

		   if pObj.mCurCell == aEndCell then
		   	aCounter = aCounter + 2;
		      if aCounter >= aFadeTime/2 then
		         aCounter = -aFadeTime;
	            pObj.mPdata:SetupScreenShake(4, aFadeTime/3);
	            pObj.mGobj.mSoundPlayer:PlaySfx("PH_SFX_ICECONESHITFLOOR");
				end
			end

		end

   end

end

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

	if pName == "" then

      if pPolyNum == 3 then
         cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
         return true;

      elseif aState < 2 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
         return true;

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

      elseif aState == 3 then
         pObj:StartFadeRects(aFadeTime);
         aState         = aState + 1;
   		pObj.mState    = aState;
         aCurCell       = aCurCell + 1;
   		pObj.mCurCell  = aCurCell;
   		pObj:StopParticleEmission(false);
         IcPlaySample("PH_SFX_ICESLABSLIDES");
         return true;

      elseif aState == 4 then

         -- Warp to next stage or level
   		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

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

	      if aState < 2 then
	         pObj.mPdata:UnMouseObject();
	         cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
	         IcPlaySample("PH_SFX_DOES_NOT_WORK");
	         return true;

			elseif aState == 2 then
	         pObj:StartFadeRects(aFadeTime);
	         aState         = aState + 1;
	   		pObj.mState    = aState;
	         aCurCell       = aCurCell + 1;
	   		pObj.mCurCell  = aCurCell;

	   		pObj:StopParticleEmission(false);
	   		pObj:SetActive(0, pPolyNum, false);
	   		pObj:SetActive(0, pPolyNum + 1, true);

				_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
	         return true;
			end

   		cm.ObjDoesNotWork(pObj);
   		return false;

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

         if aState > 1 then
      		cm.ObjDoesNotWork(pObj);
      		return false;
         end

         pObj.mPdata:UnMouseObject();
         cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         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);
	aRect = aImg:GetCelRect(pObj.mCurCell);
	pG:DrawImage(aImg:GetImage(), pObj.mSmPos.x, pObj.mSmPos.y, aRect);

end


