_G[gTable] = {};

local aTmpVal = gTable;
cm.SetupTable(_G[aTmpVal]);


-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState 	= 0;
	local aImg  	= IcImageWrap(pObj.mSmallImage);
	pObj.mCurCell	= aImg:GetNumCells() - 1;
end

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


end

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

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 == 2 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

      local aLeverValue = cm.GetGlobalIntIndirect(pObj, "LeverSum");
      if aLeverValue == pObj.mCsData:GetInt("Solution") then
         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
         pObj.mState 	= aState + 1;
         pObj.mCurCell	=  pObj.mCurCell - 1;
         cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
         IcPlaySample("PH_SFX_PRESSUREPLATEDOWN");
         IcPlaySample("PH_SFX_ICESLABSLIDES2");
      end

	elseif aState == 1 then

		if not pObj.mAlpha:AncIsChanging() then
		   if pObj.mCurCell > 0 then
		      pObj:StartFadeRects(PhFadeAnotherTimeMedium);
		      pObj.mCurCell	=  pObj.mCurCell - 1;
			else
	         pObj.mState = aState + 1;
	         pObj:SetActive(0, 0, true);
	         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
	         IcPlaySample("PH_SFX_GOOD_JOB");
         end
		end

	elseif aState == 3 then

		if not pObj.mAlpha:AncIsChanging() then

			local aImg = IcImageWrap(pObj.mSmallImage);
		   if pObj.mCurCell < aImg:GetNumCells() - 1 then
		      pObj:StartFadeRects(PhFadeAnotherTimeMedium);
		      pObj.mCurCell	=  pObj.mCurCell + 1;
			else
         	pObj.mState = aState + 1;
				local aNextLevel = pObj.mCsData:GetInt("NextLevel");
				pObj.mPdata:SetNextLevel(aNextLevel);

		      local aNextStage = pObj.mCsData:GetBool("NextStage");
		      pObj.mPdata:LevelComplete(aNextStage);
			end
		end

	elseif aState == 4 then

		if not pObj.mAlpha:AncIsChanging() then

		   if pObj.mCurCell > 0 then
		      pObj:StartFadeRects(PhFadeAnotherTimeMedium);
		      pObj.mCurCell	=  pObj.mCurCell - 1;
			else
		      pObj.mState		= 2;
		      pObj:SetActive(0, 0, true);
         end

		end

	end

end


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

   pObj.mDiscovered = true;
	if pName == "" then

      pObj:StartFadeRects(PhFadeAnotherTimeMedium);
      pObj.mCurCell	=  pObj.mCurCell + 1;
	   pObj.mState		= 3;
	   pObj:SetActive(0, 0, false);
      pObj:StopParticleEmission(false);
      IcPlaySample("PH_SFX_ICESLABSLIDES2");

		return true;

	else
		cm.ObjDoesNotWork(pObj);
		return false;
	end

	return false;

end


-- Called on draw
_G[gTable].OnDraw =  function(pObj, pG)

   local aState = pObj.mState;
   if aState < 1 then
      return;
   end

	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




