var windows = new Array();

function getYesNo(b){
  if(b)
    return "yes";
  return false;
}

function WindowManager(wnd){
  if(wnd == null)
    return false;

  this.wnd = wnd;

  this.x = 0;
  this.setX = function(x){this.x = x;}

  this.y = 0;
  this.setY = function(y){this.y = y;}

  this.width = 0;
  this.setWidth = function(w){this.width = w;}

  this.height = 0;
  this.setHeight = function(h){this.height = h;}

  this.centerAt = function(w, h){
    this.width = w;
    this.height = h;
    this.x = (this.wnd.screen.width - w) / 2;
    this.y = (this.wnd.screen.height - h) / 2;
  }

  this.resizable=true;
  this.setResizable = function(b){this.resizable=b;}

  this.menubar=true;
  this.setMenubar = function(b){this.menubar=b;}

  this.location=false;
  this.setLocation = function(b){this.location=b;}

  this.toolbar=false;
  this.setToolbar = function(b){this.toolbar=b;}

  this.scrollbars=true;
  this.setScrollbars = function(b){this.scrollbars=b;}

  //do not return anything in this function,
  //ie will prompt the user if they want to
  //leave the page.
  this.onUnload=function(){
    if(windows != null && windows.length){
      var i = 0;
      for(i = 0; i < windows.length; i++){
        if(!windows[i].closed)
          windows[i].close();
        windows[i] = null;
      }
    }
    windows = null;
    this.wnd = null;
  }

  this.wnd.onunload=this.onUnload;
  this.wnd.onbeforeunload=this.onUnload;

  this.getWindow = function(id){
    for(var i = 0; i < windows.length; i++)
      if(!windows[i].closed && windows[i].name == id)
        return windows[i];
    return null;
  }

  //This function adds a window to the internal array.
  //The id must be supplied and it must be unique.
  //closeEventListener: Optional. Method to listen to window close events.
  this.showWindow = function(url, id){
    var w = this.getWindow(id);
    if(w != null){
      w.focus();
      w.document.location.href = url;
      return false;
    }

    var options = "";
    options += "width=" + this.width + ",height=" + this.height + ",screenX=" + this.x + ",screenY=" + this.y + ",left=" + this.x + ",top=" + this.y + ",";
    options += "resizable=" + getYesNo(this.resizable) + ",";
    options += "menubar=" + getYesNo(this.menubar) + ",";
    options += "location=" + getYesNo(this.location) + ",";
    options += "toolbar=" + getYesNo(this.toolbar) + ",";
    options += "scrollbars=" + getYesNo(this.scrollbars);

    //var w = this.wnd.open(url, id, options);
    w = this.wnd.open(url, id, options);
    if(w != null)
      windows[windows.length] = w;
    else
      alert('Failed to open ' + url + '.\nCheck to see if you have a popup blocker turned on.');
    return false;
  }

  this.closeWindow = function(id){
    var w = this.getWindow(id);
    if(w)
      w.close();
    return false;
  }

  return false;
}