_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 >= 2 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(PhFadeAnotherTimeLong);
         pObj.mState   = 1;
			pObj.mCurCell = pObj.mCurCell + 1;

			local aNum = pObj:GetFirstActive(0);
			pObj:SetActive(0, aNum, false);
			pObj:SetActive(0, aNum + 1, true);
			pObj:StopParticleEmission(false);
			cm.ShowInfo(pObj, "The golem removes its foot from the trapdoor!", 300);
      end

   elseif pObj.mState == 1 then

      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag2") then
         pObj:StartFadeRects(PhFadeAnotherTimeLong);
         pObj.mState   = 2;
			pObj.mCurCell = pObj.mCurCell + 1;

			local aNum = pObj:GetFirstActive(0);
			pObj:SetActive(0, aNum, false);
			pObj:SetActive(0, aNum + 1, true);
			pObj:StopParticleEmission(false);
			IcPlaySample("PH_SFX_DOOROPENS");
			cm.ShowInfo(pObj, "The trapdoor has opened", 200);
      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 == 0 then
   	   cm.ShowInfo(pObj, "The Golem is standing on the trapdoor");
   		return true;

      elseif pObj.mState == 1 then
         cm.ShowInfo(pObj, "The trapdoor is locked");
   		return true;

      elseif pObj.mState == 2 then
         pObj.mPdata:SetNextLevel(2);
			pObj.mPdata:LevelComplete(false);
   		IcPlaySample("PH_SFX_FOOTSTEPSONLADDER");
   		return true;
   	end

   	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


