_G[gTable] = {};

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

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

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


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

   local aShowButtons = cm.GetGlobalBoolIndirect(pObj, "ControllerFlag");
   if aState == 0 then
      if aShowButtons then
   		pObj.mState  = aState + 1;
   		aState       = pObj.mState;
   		pObj.mCurCell= pObj.mCurCell + 1;
      end
   elseif aState == 1 then

      if cm.GetGlobalBoolIndirect(pObj, "PuzzleFlag") then
   		pObj.mState  = aState + 1;
   		aState       = pObj.mState;
         cm.DisableObject(pObj);

      elseif not aShowButtons then
   		pObj.mState  = aState - 1;
   		aState       = pObj.mState;
   		pObj.mCurCell= pObj.mCurCell - 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 shutter completely covers the pit");
         return true;

      elseif pObj.mState == 1 then
		   local aPuzzleFlag = pObj.mCsData:GetString("PuzzleFlag");
		   local aPuzzIndex = pObj.mCsData:GetInt("PuzzleNum");
		   AddPasswordPuzzleWidget(pObj.mPdata, aPuzzleFlag, aPuzzIndex);
         return true;
      end
      return true;

	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

