_G[gTable] = {};

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

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

-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)
	aState = 0 + pObj.mState;
end

-- Called after a level is loaded
_G[gTable].OnLevelLoad = function(pObj)
	aState = 0 + pObj.mState;
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 aState == 0 then
      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         pObj:StartFadeRects(PhFadeAnotherTimeLong);
         aState =  aState + 1;
   		pObj.mState = aState;
   		pObj.mCurCell	= pObj.mCurCell + 1;
         pObj:SetActive(0, 0, true);
      end

   elseif aState == 1 then
      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag2") then
         pObj:StartFadeRects(PhFadeAnotherTimeLong);
         aState			=  aState + 1;
   		pObj.mState		= aState;
   		pObj.mCurCell	= pObj.mCurCell + 1;
         IcPlaySample("PH_SFX_DOOROPENS");
      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 aState == 1 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
         return true;

      elseif aState == 2 then
         pObj:StopParticleEmission(false);

   		local aNextLevel = pObj.mCsData:GetInt("NextLevel");
   		pObj.mPdata:SetNextLevel(aNextLevel);
         local aNextStage = pObj.mCsData:GetBool("NextStage");
         pObj.mPdata:LevelComplete(aNextStage);

        	IcPlaySample("PH_SFX_FOOTSTEPSONGROUND");
   		return true;
		end
		return true;

	else
      if aState < 2 then
         cm.ObjDoesNotWork(pObj);
      end
		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


