var Monitor = new function() {
  this.targetURL = null;
  this.timeoutThreshold = null;
  this.ajax = null;
  this.timeoutHand = null; // Interval ID for timeout watcher
  
  this.init = function() {
    var self = Monitor;
    self.ajax = new Ajax();
    self.targetURL = "/includes/sidebar_x.php";
    self.timeoutThreshold = 10;
    var buttonX = document.getElementById('buttonX');
    buttonX.onclick = self.doPoll;
    
    var wrap = document.getElementById('main-body');     // MODIFY main-body right margin
    wrap.style.marginRight = '160px';
  };
  
  this.doPoll = function() {
    var self = Monitor;
    self.ajax.doGet(self.targetURL + '?sidebar=none', self.showPoll);
    self.timeoutHand = setTimeout(self.handleTimeout, self.timeoutThreshold * 1000);
  };
  
  this.showPoll = function(str) {
    var self = Monitor;
    clearTimeout(self.timeoutHand);
    if (str == 'ok') {
        var sidebar_right = document.getElementById('sidebar_right');
        sidebar_right.style.display = 'none';

        var menu_button = document.getElementById('menu_button');
        if (menu_button) menu_button.value = 'Show Right Sidebar';
        
        var wrap = document.getElementById('main-body');
        wrap.style.marginRight = '0px';
    }
  };
  
  this.handleTimeout = function() {
    var self = Monitor;
    if (self.ajax) self.ajax.abort();
    clearTimeout(self.timeoutHand);
  };
};
// window.onload = Monitor.init;    // init called from footer.js init

