
function openInnerWindow (name, url, width, height, title, params) {
  var window = new InnerWindow(name, url, width, height, title, params);
  window.open();
}

function closeInnerWindow (name) {
  windowManager.closeWindow(name);
}

WindowManager = function () {
  this.windows = new Array();
}

WindowManager.prototype = {

  addWindow:function (window) {
    this.windows[window.name] = window;
  },

  removeWindow:function (windowName) {
    this.windows[windowName] = null;
  },

  getWindow:function (windowName) {
    return this.windows[windowName];
  },

  getMaximumZIndex:function () {
    var maximumIndex = 1;
    for (var i=0; i<this.windows.length; i++) {
      var windowIndex = this.windows[i].getZIndex();
      if (windowIndex > maximumIndex)
        maximumIndex = windowIndex;
    }
    return maximumIndex;
  },

  closeWindow:function (windowName) {
    var window = this.windows[windowName];
    window.close();
  },

  closeAllWindows:function () {

  },

  minimizeAllWindows:function () {

  },

  minimizeWindow:function (windowName) {
    var window = this.windows[windowName];
    window.minimize();
  },

  maximizeWindow:function (windowName) {
    var window = this.windows[windowName];
    window.maxmize();
  },

  normalizeWindow:function (windowName) {
    var window = this.windows[windowName];
    window.normalize();
  }

}

var windowManager = new WindowManager();

InnerWindow = function (name, url, width, height, title, params, x, y) {
  this.name = name + "_window";
  this.url = url;
  this.width = defaultValue(width, 500);
  this.height = defaultValue(height, 350);
  this.title = defaultValue(title, "");
  this.x = defaultValue(x, "");
  this.y = defaultValue(y, "");
  this.zIndex = 2;
  this.params = defaultValue(params, "");
  this.contentRetriever = null;
}

InnerWindow.prototype = {

  bringToFront:function () {
    var maxIndex = windowManager.getMaximumZIndex();
    setZIndex(maxIndex +1);
  },

  getZIndex:function () {
    return this.zIndex;
  },

  open:function () {
    var loadingDiv = getElement("loading_messge");
    if (!loadingDiv)
      loadingDiv = createCenteredDivide(this.name + "_loading_message", 150, 20, "Loading...");

    loadingDiv.setAttribute("style", "background-color: #FFFFFF; padding: 4px;");
    this.contentRetriever = new HttpRequest(this.url, this.openCallback);
    this.contentRetriever.caller = this;
    this.contentRetriever.post(this.params);
  },

  openCallback:function () {
    /* 'this' in this context will be the HttpRequest, as this function is a callback. */
    var content = this.contentRetriever.getResponse();
    // console.log(content);
    var innerWindow = getElement(this.name);
    var loadingMessage = getElement(this.name + "_loading_message");
    var body = document.body;
    if (innerWindow)
      body.removeChild(innerWindow);

    var titlebar = document.createElement("DIV");
    titlebar.setAttribute("class", "window_titlebar");
    titlebar.setAttribute("className", "window_titlebar");
    titlebar.setAttribute("id", this.name + "_titlebar");
    // titlebar.setAttribute("style", "height: 25px;");
    titlebar.setAttribute("onDblClick", "windowManager.maximizeWindow('" + this.name + "')");
    titlebar.setAttribute("style", "padding: 3px; cursor: move;");
    var widthSpecifier = "";
    if (this.width > 50) {
      var width = this.width-50;
      widthSpecifier = "width: " + width + "px;";
    }
    titlebar.innerHTML = "<DIV><IMG src=\"images/close.gif\" style=\"float: right; margin-right: 5px; cursor: pointer;\" onClick=\"windowManager.closeWindow('" + this.name + "')\"><!-- <IMG src=\"images/minimize.gif\" style=\"margin: 3px;\" id=\"" + this.name + "_minimize_button\" onClick=\"windowManager.minimizeWindow('" + this.name + "')\"> --></DIV><DIV style=\"float: left; margin-left: 5px; " + widthSpecifier + "\"><B>" + this.title + "</B></DIV><DIV style='clear: left'></DIV>";

    var resizeHandle = document.createElement("DIV");
    resizeHandle.setAttribute("id", this.name + "_resize_handle");
    resizeHandle.innerHTML = "<IMG src=\"images/resize.gif\">";
    makeResizeableHandle(resizeHandle);

    resizeHandle.style.position = "absolute"; // "relative";
    resizeHandle.style.left = (Number(this.width) - 5) + "px";
    resizeHandle.style.top = (Number(this.height) - 5) + "px";

    var contentsContainer = document.createElement("DIV");
    contentsContainer.setAttribute("id", this.name + "_contents");
    contentsContainer.setAttribute("class", "window_contents");
    contentsContainer.setAttribute("className", "window_contents");
    contentsContainer.innerHTML = content;
    // contentsContainer.class = "window_contents";

    var windowDivide = null;
    if (this.x != "" || this.y != "")
      windowDivide = createDivide(this.name, this.width, this.height, x, y, "", "inner_window");
    else windowDivide = createCenteredDivide(this.name, this.width, this.height, "", "inner_window");

    makeDraggableHandle(titlebar);

    if (loadingMessage)
      body.removeChild(loadingMessage);
    var windowStyle = getStyleObject(this.name);

    // windowDivide.appendChild(resizeHandle);
    windowDivide.appendChild(titlebar);
    windowDivide.appendChild(contentsContainer);
    windowStyle.backgroundColor = "#FFFFFF";

    windowManager.addWindow(this);
  },

  minimize:function () {
    var minimizeButton = getElement(this.name + "_minimize_button");
    var windowStyle = getStyleObject(this.name);
    var left = stripTrailing(windowStyle.left, 2);
    var top = stripTrailing(windowStyle.top, 2);
    var width = stripTrailing(windowStyle.width, 2);
    var height = stripTrailing(windowStyle.height, 2);
    minimizeButton.setAttribute("src", "images/maximize.gif");
    minimizeButton.setAttribute("onClick", "windowManager.normalizeWindow('" + this.name + "')");
    var leftMin = 0;
    var topMin = 0;
    var minWidth = 200;
    var minHeight = 20;
    windowStyle.left = leftMin + "px";
    windowStyle.top = topMin + "px";
    windowStyle.width = minWidth + "px";
    windowStyle.height = minHeight + "px";
  },

  close:function (name) {
    var innerWindowTitlebar = getElement(this.name + "_titlebar");
    innerWindowTitlebar.innerHTML = "<B>Closing...</B>";
    var innerWindowDiv = getElement(this.name);
    var body = document.body;
/*
    var parent = this.parentNode;
    while (parent.getAttribute("class") != "inner_window")
      parent = parent.parentNode;

    var innerWindowDiv = parent;
*/
    if (innerWindowDiv)
      body.removeChild(innerWindowDiv);

    windowManager.removeWindow(this.name);
  },

  maximize:function () {
    var titlebar = getElement(this.name + "_titlebar");
    var resizeHandle = getStyleObject(this.name + "_resize_handle");
    var windowStyle = getStyleObject(this.name);
    var left = stripTrailing(windowStyle.left, 2);
    var top = stripTrailing(windowStyle.top, 2);
    var width = stripTrailing(windowStyle.width, 2);
    var height = stripTrailing(windowStyle.height, 2);
    var windowWidth = getWindowWidth();
    var windowHeight = getWindowHeight();

    titlebar.setAttribute("onDblClick", "windowManager.normalizeWindow('" + this.name + "')");
    windowStyle.left = 0 + "px";
    windowStyle.top = 0 + "px";
    windowStyle.width = windowWidth + "px";
    windowStyle.height = windowHeight + "px";
    resizeHandle.left = (Number(windowWidth)/* -15 */) + "px";
    resizeHandle.top = (Number(windowHeight)/* -15 */) + "px";
  },

  normalize:function (width, height, x, y) {
    var titlebar = getElement(this.name + "_titlebar");
    var resizeHandle = getStyleObject(this.name + "_resize_handle");
    var windowStyle = getStyleObject(this.name);
    var normalizeButton = getElement(this.name + "_minimize_button");
    normalizeButton.setAttribute("src", "images/minimize.gif");
    normalizeButton.setAttribute("onClick", "windowManager.minimizeWindow('" + this.name + "')");
    windowStyle.left = this.x + "px";
    windowStyle.top = this.y + "px";
    windowStyle.width = this.width + "px";
    windowStyle.height = this.height + "px";
    titlebar.setAttribute("onDblClick", "windowManager.maximizeWindow('" + this.name + "')");
    resizeHandle.left = this.width + "px";
    resizeHandle.top = this.height + "px";
  },

  resize:function () {
    var innerWindowStyle = getStyleObject(this.name);
    this.width = width;
    this.height = height;
    innerWindowStyle.width = width + "px";
    innerWindowStyle.height = height + "px";
  },

  setContents:function (contents) {
    var contentPane = getElement(this.name + "_contents");
    contentPane.innerHTML = contents;
  },

  setZIndex:function (zIndex) {
    var style = getStyleObject(this.name);
    style.zIndex = zIndex;
    this.zIndex = zIndex;
  }

}
