
_G[gTable] = {};

local aTmpVal  	= gTable;
local aState   	= 0;
local aCurCell 	= 0;
local aShowHint	= false;
local aHintTimer	= 0;
cm.SetupTable(_G[aTmpVal]);


-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState	= 0;
	aState      = pObj.mState;
	aCurCell    = pObj.mCurCell;
	aShowHint	= false;
	aHintTimer	= 0;
	pObj.mCsData:SetVal("DisappearEnd", 5);
   pObj.mCsData:SetVal("aShowHint", aShowHint);
   pObj.mCsData:SetVal("aHintTimer", aHintTimer);
end


-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)
	aState   	= pObj.mState;
	aCurCell		= pObj.mCurCell;
	aHintTimer	= 0;
	aShowHint	= pObj.mCsData:GetBool("aShowHint");
	aHintTimer	= pObj.mCsData:GetInt("aHintTimer");
end

--Called after level being reloaded
_G[gTable].OnLevelLoad = function(pObj)
	aState   	= pObj.mState;
	aCurCell		= pObj.mCurCell;
	aHintTimer	= 0;
	aShowHint	= pObj.mCsData:GetBool("aShowHint");
	aHintTimer	= pObj.mCsData:GetInt("aHintTimer");
end

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

	if aShowHint then
	   aHintTimer = aHintTimer + 1;
	   pObj.mCsData:SetVal("aHintTimer", aHintTimer);
	end

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

         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
         aState         = aState + 1;
   		pObj.mState    = aState;
         aCurCell       = aCurCell + 1;
   		pObj.mCurCell  = aCurCell;
   		pObj.mPdata:SetupScreenShake(2, 120);
   		pObj:StopParticleEmission(false);
   		IcPlaySample("PH_SFX_ICESLABSLIDES");

      end

	elseif aState == 1 then
      if not pObj.mAlpha:AncIsChanging() then

         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
         aState         = aState + 1;
   		pObj.mState    = aState;
         aCurCell       = aCurCell + 1;
   		pObj.mCurCell  = aCurCell;
   		
   		local aNum = pObj:GetFirstActive(0);
   		pObj:SetActive(0, aNum, false);
   		pObj:SetActive(0, aNum + 1, true);
   		pObj:SetActive(0, aNum + 2, true);
   		pObj:StopParticleEmission(false);
   		cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");

      end

	elseif aState == 2 then
      if cm.GetGlobalBoolIndirect(pObj, "PuzzleFlag") then

         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
         aState         = aState + 1;
   		pObj.mState    = aState;
         aCurCell       = aCurCell + 1;
   		pObj.mCurCell  = aCurCell;

         cm.DisableObject(pObj);
         pObj.mPdata:SetupScreenShake(2, 140);
     		IcPlaySample("PH_SFX_ICESLABSLIDES");

      end

	elseif aState == 3 then
      if not pObj.mAlpha:AncIsChanging() then
      
      	local aImg = IcImageWrap(pObj.mSmallImage);
       	local aNumCells = aImg:GetNumCells();

       	if aCurCell < aNumCells - 1 then
	         pObj:StartFadeRects(PhFadeAnotherTimeLong);
	         aCurCell       = aCurCell + 1;
	   		pObj.mCurCell  = aCurCell;
	         pObj:StopParticleEmission(false);
	         
			else
	         aState         = aState + 1;
	   		pObj.mState    = aState;
	   		cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
	   		cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
	   		
	   		pObj.mDrawer = "DrawFadeAppearDisappear";
	   		pObj.mAlpha:AncSetupChange(255.0, 0.0, PhSpawnerVanishAfterUse,
	   		   IcAutoNumChgr.IC_ANCM_LINEAR);

			end

      end

	elseif aState == 4 then
		if not pObj.mAlpha:AncIsChanging() then
         aState         = aState + 1;
   		pObj.mState    = aState;
			pObj.mIsDone 	= true;
		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 == 0 then
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
         return true;
      end
      
      if pPolyNum == 1 then
         cm.LaunchPuzzle(pObj);
         aShowHint = true;
         pObj.mCsData:SetVal("aShowHint", aShowHint);
      else
         _G[aTmpVal].ShowText(pObj);
      end
      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 to show text
local aClickCount = 0;
_G[gTable].ShowText =  function(pObj)

	if not aShowHint then
	   cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
	   return;
	elseif (aHintTimer < 4*60*50) or (aClickCount < 2) then
	   cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
	elseif aHintTimer < 8*60*50 then
	   cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
	else
	   cm.GetShowInfo(pObj, "InfoStr6", "InfoTime6");
	end
	aClickCount = aClickCount + 1;
	IcPlaySample("PH_SFX_DOES_NOT_WORK");

end



