_G[gTable] = {};

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

-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState = 0;
	pObj.mCsData:SetVal("aEnabled", false);
end

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

end


-- Called to fetch mouse over info
_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 not pObj.mCsData:GetBool("aEnabled") then
	   if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
			pObj.mCsData:SetVal("aEnabled", true);
			pObj:SetActive(0, 0, true);
		end
	end

   if pObj.mState == 0 then
      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag2") 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;
				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
	
	   if pObj.mState == 2 then

	      -- Warp to next stage or level
			local aNextLevel = pObj.mCsData:GetInt("NextLevel");
			pObj.mPdata:SetNextLevel(aNextLevel);

	      if pObj.mCsData:IfExistsBool("NextStage") then
	         local aNextStage = pObj.mCsData:GetBool("NextStage");
	         pObj.mPdata:LevelComplete(aNextStage);
			else
	         pObj.mPdata:LevelComplete(false);
	      end
	      IcPlaySample("PH_SFX_FOOTSTEPSONGROUND");
	      return true;

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

     	IcPlaySample("PH_SFX_OBJ_USED");
		return true;

	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
