_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 on a update
_G[gTable].OnUpdate = function(pObj)

   if pObj.mState == 0 then
      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         if pObj.mCsData:GetBool("DoorVisible") then
            local aNum = pObj:GetFirstActive(0);
            pObj:SetActive(0, aNum, false);
            pObj:SetActive(0, aNum + 1, true);
         else
            pObj:SetActive(0, 0, true);
         end
   		pObj.mState = pObj.mState + 1;

      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.ShowInfo(pObj, "The door does not budge");
         return true;

      elseif pObj.mState == 1 then
         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
   		pObj.mState     = pObj.mState + 1;
   		pObj.mCurCell   = pObj.mCurCell + 1;
         IcPlaySample("PH_SFX_OBJ_PICKUP");
         return true;

      else
         -- 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_OBJ_USED");
   		return true;
		end

	else
		cm.ObjDoesNotWork(pObj);
		return false;
	end

	return false;

end
-- Called on draw
_G[gTable].OnDraw =  function(pObj, pG)

	pG:PushState();

	local aImg = IcImageWrap(pObj.mSmallImage);
	assert(aImg);
	aRect = aImg:GetCelRect(pObj.mCurCell);
	pG:DrawImage(aImg:GetImage(), pObj.mSmPos.x, pObj.mSmPos.y, aRect);
	pG:PopState();

end


-- Called on moused
_G[gTable].OnMoused =  function(pObj)
	print(2+2);
end

-- Called on Unmoused
_G[gTable].OnUnMoused =  function(pObj)
	print(2+2);
end

