_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 >= 1 then
      aCursorTable[1] = IcGobj.CURSOR_FOOT;
	end
   return aCursorTable;

end


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

   if pObj.mState == 0 then

      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         pObj:StartFadeRects(PhFadeAnotherTimeShort);
   		pObj.mState		= pObj.mState + 1;
         pObj.mCurCell	= pObj.mCurCell + 1;
         pObj:StopParticleEmission(false);

			if pObj.mCsData:IfExistsString("ObjSfxId") then
			   local aStr = pObj.mCsData:GetString("ObjSfxId");
			   IcPlaySample(aStr);
			else
		    	IcPlaySample("PH_SFX_OBJ_USED");
			end

      end

   elseif pObj.mState == 1 then

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

       	if pObj.mCurCell < aNumCells - 1 then
	         pObj:StartFadeRects(PhFadeAnotherTimeShort);
	         pObj:StopParticleEmission(false);
	         pObj.mCurCell = pObj.mCurCell + 1;
			else
			   pObj.mState = pObj.mState + 1;
			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

      -- Test if the door is active
      if pObj.mState == 0 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         return true;

      elseif pObj.mState == 2 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

	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


