
_G[gTable] = {};

local aTmpVal  = gTable;
local aState   = 0;
local aCurCell = 0;
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 on a update
_G[gTable].OnUpdate = function(pObj)

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

         pObj:StartFadeRects(PhFadeAnotherTimeShort);
         aState          = aState + 1;
   		pObj.mState     = aState;
         aCurCell        = aCurCell + 1;
   		pObj.mCurCell   = aCurCell;
   		pObj.mPdata:SetupScreenShake(3, 100);
   		IcPlaySample("PH_SFX_ICESLABSLIDES");

      end
   elseif aState > 0 and aState < 3 then

      if not pObj.mAlpha:AncIsChanging() then
         pObj:StartFadeRects(PhFadeAnotherTimeShort);
			aState          = aState + 1;
   		pObj.mState     = aState;
         aCurCell        = aCurCell + 1;
   		pObj.mCurCell   = aCurCell;
   		
   		if aState == 3 then
	   		pObj:SetActive(0, 0, true);
	   		pObj:SetActive(0, 0 + 1, true);
	   		pObj:SetActive(0, 0 + 2, true);
	   		cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
   		end
		end
	end

end


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


	if pName == "" then

      if pPolyNum == 0 then

         pObj:StartFadeRects(PhFadeAnotherTimeLong);
         local aImg = IcImageWrap(pObj.mSmallImage);
         
         aCurCell = aCurCell + 1;
         if aCurCell == aImg:GetNumCells() then
            aCurCell = 3;
         end

   		pObj.mCurCell   = aCurCell;
         IcPlaySample("PH_SFX_ICEPILLARROTATES");

      elseif pPolyNum == 1 then

         local aFillStr    = pObj.mCsData:GetString("FillStr");
         local aFillCount  = pObj.mCsData:GetInt("FillCount");
         local aSolution   = pObj.mCsData:GetString("Solution");
         aFillCount = aFillCount + 1;
         aFillStr = aFillStr..string.format("%d", aCurCell);

         -- Check if the puzzle has been solved
         if aFillCount >= #aSolution then

            if aFillStr == aSolution then
               IcPlaySample("PH_SFX_LEVEROPERATED");
               cm.DisableObject(pObj);
               cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
               cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
            else
               aFillStr = "";
               aFillCount = 0;
               cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
               pObj.mCsData:SetVal("FillStr",   aFillStr);
               pObj.mCsData:SetVal("FillCount", aFillCount);
               IcPlaySample("PH_SFX_DOES_NOT_WORK");
            end

         else
            IcPlaySample("PH_SFX_MACHINEBUTTONPRESSED");
            pObj.mCsData:SetVal("FillStr",   aFillStr);
            pObj.mCsData:SetVal("FillCount", aFillCount);
         end

      elseif pPolyNum == 2 then
         aFillStr = "";
         aFillCount = 0;
         pObj.mCsData:SetVal("FillStr",   aFillStr);
         pObj.mCsData:SetVal("FillCount", aFillCount);
         cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
         IcPlaySample("PH_SFX_MACHINEBUTTONPRESSED2");
      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




