_G[gTable] = {};

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


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

-- Called after data being loaded from disk
_G[gTable].OnLoad = 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 >= 8 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(PhFadeAnotherTimeMedium);
      	pObj.mState   = aState + 1;
      	pObj.mCurCell = pObj.mCurCell + 1;
		   pObj:StopParticleEmission(false);
			pObj:SetActive(0, 0, false);
      end

   elseif aState == 1 then

      if not pObj.mAlpha:AncIsChanging() then
      	pObj.mState = aState + 1;
         pObj:SetActive(0, 1, true);
		end

   elseif aState == 3 then

      if not pObj.mAlpha:AncIsChanging() then
      	pObj.mState = aState + 1;
		end

   elseif aState == 4 then

      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag2") then
         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
      	pObj.mState   = aState + 1;
      	pObj.mCurCell = pObj.mCurCell + 1;
      	pObj:SetActive(0, 1, false);
		   pObj:StopParticleEmission(false);
         IcPlaySample("PH_SFX_ICESLABSLIDES2");
      end

   elseif aState == 5 then

      if not pObj.mAlpha:AncIsChanging() then
         if pObj.mCurCell < pObj.mCsData:GetInt("MoveEndCell") then
         	pObj:StartFadeRects(PhFadeAnotherTimeMedium);
         	pObj.mCurCell = pObj.mCurCell + 1;
         	pObj:StopParticleEmission(false);
         else
            pObj.mState = aState + 1;
            pObj:SetActive(0, 2, true);
            cm.SetGlobalValIndirect(pObj, "SetFlag2", true);
         end
      end

   elseif aState == 6 then

      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag3") then
         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
      	pObj.mState   = aState + 1;
      	pObj.mCurCell = pObj.mCurCell + 1;

			pObj:StopParticleEmission(false);
			local aNum = pObj:GetFirstActive(0);
			pObj:SetActive(0, aNum, false);
         IcPlaySample("PH_SFX_PRESSUREPLATEDOWN");
      end

   elseif aState == 7 then

      if not pObj.mAlpha:AncIsChanging() then
         pObj:SetActive(0, 3, true);
         pObj.mState = aState + 1;
		end

	end


end


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

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

	if pName == "" then

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

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

      elseif aState == 0 then
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");

      elseif aState == 2 then
         cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");

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

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

      if aState == 2 then

         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
   		pObj.mState		= aState + 1;
   		pObj.mCurCell	= pObj.mCurCell + 1;
   		pObj:StopParticleEmission(false);
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
         cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
         return true;

      end
      return _G[aTmpVal].MaybeLater(pObj);

	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


-- Show some info
_G[gTable].MaybeLater =  function(pObj)

   pObj.mPdata:UnMouseObject();
   cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
   IcPlaySample("PH_SFX_DOES_NOT_WORK");
   return true;
end
