/*************************************************************************
(c)2004,2005 by Scalix Corporation and are the property of Scalix Corporation.
This file is available to you only under license from Scalix Corporation,
the terms of which include limited rights to install and use the software
but specifically do not permit copying and distribution of this file, and
do not permit the removal, alteration, or effacing of this notice or any
copyright or other proprietary notices contained in the software or files.
Do not use the software without a valid license from Scalix Corporation.
**************************************************************************/

function NameValuePair(e) {
}

NameValuePair.create = function(name, value) {
  var thisObj = new NameValuePair();
  thisObj.initialize(name, value);
  return thisObj;
};

NameValuePair.prototype.initialize = function(name, value) {
  this.name = name;
  this.value = value;
};

// BaseGUI
// Should not create a BaseGUI - thus no create static method

function BaseGUI() {
}

BaseGUI.prototype.winGUI = null;
BaseGUI.prototype.parentGUI = null;
BaseGUI.prototype.parentElement = null;
BaseGUI.prototype.rootElement = null;
BaseGUI.prototype.id = null;
BaseGUI.prototype.left = 0;
BaseGUI.prototype.top = 0;
BaseGUI.prototype.width = null;
BaseGUI.prototype.height = null;
BaseGUI.prototype.maxWidth = 0;
BaseGUI.prototype.maxHeight = 0;
BaseGUI.prototype.rootElementType = "SPAN";
BaseGUI.prototype.exactPosition = true;
BaseGUI.prototype.isDisplayed = false;
BaseGUI.prototype.hidden = false;
BaseGUI.prototype.disabled = false;


BaseGUI.prototype.initialize = function(guiObj, pElement) {
  this.parentGUI = guiObj;
  this.winGUI = (guiObj != null) ? guiObj.winGUI : null;
  this.parentElement = (pElement == null && guiObj != null) ? guiObj.rootElement : pElement;
};


BaseGUI.prototype.setWindowGUI = function(win) {
  this.winGUI = win;
}

BaseGUI.prototype.setParentGUI = function(guiObj, pElement) {
  this.parentGUI = guiObj;
  this.winGUI = guiObj.winGUI;

  this.parentElement = (pElement == null) ? guiObj.rootElement : pElement;
}

BaseGUI.prototype.setID = function(id) {
  this.id = id;
  if (this.rootElement != null) {
    this.rootElement.id = id;
  }
}

BaseGUI.prototype.setup = function(tabIndex) {
  this.rootElement = this.createElement(this.rootElementType);
  if (this.exactPosition) {
    this.rootElement.className = "ExactPosition";
  }
  this.rootElement.guiObj = this;
  if (this.id != null) {
    this.rootElement.id = this.id;
  }
  this.parentElement = this.rootElement;

  this.rootElement.onmouseover = BaseGUI.MouseOverFunction;
  this.rootElement.onmouseout = BaseGUI.MouseOutFunction;
  this.rootElement.onmousemove = BaseGUI.MouseMoveFunction;

  this.rootElement.oncontextmenu = BaseGUI.ContextMenuFunction;
  if(this.rootElement.addEventListener != null)
  {
    this.rootElement.addEventListener('click', BaseGUI.ClickFunction, true);
  }
  else
  {
    this.rootElement.onclick = BaseGUI.ClickFunction;
  }

  return tabIndex;
}

BaseGUI.prototype.calculateSize = function(x, y, w, h) {
  pushFunc("BaseGUI.calculateSize");

  if (x == null) {
    x = 0;
  }
  if (y == null) {
    y = 0;
  }
  if (w == null) {
    if (this.winGUI != null) {
      w = this.winGUI.width;
    } else {
      w = 800;
    }
  }
  if (h == null) {
    if (this.winGUI != null) {
      h = this.winGUI.height;
    } else {
      h = 450;
    }
  }
  this.left = Math.max(x, 0);
  this.top = Math.max(y, 0);
  this.width = this.maxWidth = Math.max(w, 0);
  this.height = this.maxHeight = Math.max(h, 0);

  popFunc();
}

BaseGUI.prototype.setFinalSize = function() {
  this.left = this.rootElement.offsetLeft;
  this.top = this.rootElement.offsetTop;
  this.width = this.rootElement.offsetWidth;
  this.height = this.rootElement.offsetHeight;
}

BaseGUI.prototype.layout = function(x, y, w, h) {
  this.calculateSize(x, y, w, h);
  this.doLayout();
  this.setFinalSize();
  // this.preDisplay();
}

BaseGUI.prototype.preDisplay = function()
{
  this.isDisplayed = true;
};

BaseGUI.prototype.postDisplay = function()
{
  this.isDisplayed = false;

  // Returns true if the pane is "willing" to be hidden
  return true;
}

BaseGUI.prototype.setMinimumSize = function(minW, minH) {
  this.minWidth = minW;
  this.minHeight = minH;
}

BaseGUI.prototype.doLayout = function() {
  if (this.exactPosition) {
    this.rootElement.style.left = this.left;
    this.rootElement.style.top = this.top;
  }
  this.rootElement.style.width = (this.width < 0) ? 0 : this.width;
  this.rootElement.style.height = (this.height < 0) ? 0 : this.height;

  {
    if ((this.minWidth != null) && (this.width < this.minWidth)) {
      //this.rootElement.style.overflow = "auto";
      this.rootElement.width = this.width;

      this.width = this.minWidth;

      // adjust the height for horizontal scroll bar
      if ((this.minHeight != null) && (this.height > this.minHeight)) {
        if (this.rootElement.innerHeight) {
          this.height = this.rootElement.height = this.rootElement.innerHeight;
        }
        else if (this.rootElement.clientHeight) {
          this.height = this.rootElement.height = this.rootElement.clientHeight;
        }
      }
    }

    if ((this.minHeight != null) && (this.height < this.minHeight)) {
      //this.rootElement.style.overflow = "auto";
      this.rootElement.height = this.height;

      this.height = this.minHeight;

      // adjust the width for vertical scroll bar
      if ((this.minWidth != null) && (this.width > this.minWidth)) {
        if (this.rootElement.innerWidth) {
          this.width = this.rootElement.width = this.rootElement.innerWidth;
        }
        else if (this.rootElement.clientWidth) {
          this.width = this.rootElement.width = this.rootElement.clientWidth;
        }
      }
    }
  }

}

BaseGUI.prototype.setPosition = function(x, y) {
  this.rootElement.style.left = x;
  this.rootElement.style.top = y;
  this.left = x;
  this.top = y;
};

BaseGUI.prototype.hide = function()
{
  this.rootElement.style.visibility = "hidden";
  this.hidden = true;
};

BaseGUI.prototype.expose = function()
{
  this.rootElement.style.visibility = "inherit";
  this.hidden = false;
};

BaseGUI.prototype.update = function(editable)
{
  this.disabled = !editable;
};

BaseGUI.prototype.setFocus = function()
{
  return false;
};

BaseGUI.prototype.createElement = function(tag, parent, allowTooltip)
{
  var elem = this.winGUI.doc.createElement(tag);

  if (parent != null) {
    parent.appendChild( elem );
  } else if (this.parentElement != null) {
    this.parentElement.appendChild(elem);
  } else {
    this.winGUI.doc.body.appendChild( elem );
  }

  if(allowTooltip)
  {
    elem.onmouseover = BaseGUI.MouseOverFunction;
    elem.onmouseout = BaseGUI.MouseOutFunction;
    elem.onmousemove = BaseGUI.MouseMoveFunction;
    elem.setTooltip = function(tooltipGUI) {BaseGUI.prototype.setTooltip.call(this, tooltipGUI);};
    elem.clearTooltip = function() {BaseGUI.prototype.clearTooltip.call(this);};
    elem.showTooltip = function(x,y) {BaseGUI.prototype.showTooltip.call(this,x,y);};
    elem.hideTooltip = function() {BaseGUI.prototype.hideTooltip.call(this);};
    elem.guiObj = elem;
  }

  return elem;
}

BaseGUI.prototype.createText = function(str, parent) {
  var elem = this.winGUI.doc.createTextNode(str);

  if (parent != null) {
    parent.appendChild( elem );
  } else if (this.parentElement != null) {
    this.parentElement.appendChild(elem);
  } else {
    this.winGUI.doc.body.appendChild( elem );
  }

  return elem;
}


BaseGUI.prototype.createNextSiblingElement = function(tag, sibling, parent) {
  var elem = this.winGUI.doc.createElement(tag);

  if (sibling == null || sibling.nextSibling == null) {
    parent.appendChild(elem);
  } else {
    parent.insertBefore(elem, sibling.nextSibling);
  }

  return elem;
}

BaseGUI.prototype.createPrevSiblingElement = function(tag, sibling, parent) {
  var elem = this.winGUI.doc.createElement(tag);

  if (sibling == null) {
    parent.appendChild(elem);
  } else {
    parent.insertBefore(elem, sibling);
  }

  return elem;
}


BaseGUI.prototype.createInputElement = function(inputType, parent, name, allowTooltip) {
  var elem;

  if (name != null) {
    try {
      elem = this.winGUI.doc.createElement("<INPUT name='" + name + "'>");
    } catch(e) {
      elem = null;
    }
  }
  if (elem == null || elem.tagName != "INPUT") {
    elem = this.winGUI.doc.createElement("INPUT");
    if (name != null) {
      elem.name = name;
    }
  }
  elem.type = inputType;

  if (parent != null) {
    parent.appendChild( elem );
  } else if (this.parentElement != null) {
    this.parentElement.appendChild(elem);
  } else {
    this.winGUI.doc.body.appendChild( elem );
  }

  if(allowTooltip)
  {
    elem.onmouseover = BaseGUI.MouseOverFunction;
    elem.onmouseout = BaseGUI.MouseOutFunction;
    elem.onmousemove = BaseGUI.MouseMoveFunction;
    elem.setTooltip = function(tooltipGUI) {BaseGUI.prototype.setTooltip.call(this, tooltipGUI);};
    elem.clearTooltip = function() {BaseGUI.prototype.clearTooltip.call(this);};
    elem.showTooltip = function(x,y) {BaseGUI.prototype.showTooltip.call(this,x,y);};
    elem.hideTooltip = function() {BaseGUI.prototype.hideTooltip.call(this);};
    elem.guiObj = elem;
  }

  return elem;
}

BaseGUI.prototype.createIEElement = function(fullString, parent) {
  var elem = this.winGUI.doc.createElement(fullString);

  if (parent != null) {
    parent.appendChild( elem );
  } else if (this.parentElement != null) {
    this.parentElement.appendChild(elem);
  } else {
    this.winGUI.doc.body.appendChild( elem );
  }

  return elem;
}


BaseGUI.prototype.removeFromParent = function() {
  pushFunc("BaseGUI.removeFromParent");
  
  if (this.parentElement != null) {
    this.parentElement.removeChild(this.rootElement);
  } else {
    this.winGUI.doc.body.removeChild(this.rootElement);
  }

  popFunc();
}

BaseGUI.prototype.toString = function() {
  return "BaseGUI";
};

BaseGUI.prototype.setTabIndex = function(tabIndex) {
  return tabIndex;
};

BaseGUI.MouseOverFunction = function(e)
{
  var evt = (e == null) ? window.event : e;
  var elem = (evt.srcElement != null) ? evt.srcElement : evt.target;

  while(elem != null)
  {
    if(typeof(BaseGUI) != "undefined" && elem.onmouseover == BaseGUI.MouseOverFunction)
    {
      var item = elem.guiObj;
      if(gIsNetscape)
      {
        item.showTooltip(evt.pageX + 2, evt.pageY + 2);
      }
      if(gIsExplorer)
      {
	item.showTooltip(evt.clientX + 2, evt.clientY + 2);
      }
      break;
    }
    elem = elem.parentNode;
  }

  return false;
};

BaseGUI.MouseOutFunction = function(e)
{
  var evt = (e == null) ? window.event : e;
  var elem = (evt.srcElement != null) ? evt.srcElement : evt.target;

  while(elem != null)
  {
    if(typeof(BaseGUI) != "undefined" && elem.onmouseout == BaseGUI.MouseOutFunction)
    {
      var item = elem.guiObj;
      item.hideTooltip();
      break;
    }
    elem = elem.parentNode;
  }

  return false;
};

BaseGUI.MouseMoveFunction = function(e)
{
  var evt = (e == null) ? window.event : e;
  var elem = (evt.srcElement != null) ? evt.srcElement : evt.target;

  while(elem != null)
  {
    if(typeof(BaseGUI) != "undefined" && elem.onmousemove == BaseGUI.MouseMoveFunction)
    {
      var item = elem.guiObj;
      if(item != null && item.setTooltipLocation != null)
      {
        if(gIsNetscape)
        {
          item.setTooltipLocation(evt.pageX + 2, evt.pageY + 2);
        }
        if(gIsExplorer)
        {
	  item.setTooltipLocation(evt.clientX + 2, evt.clientY + 2);
        }
      }
      break;
    }
    elem = elem.parentNode;
  }

  return false;
};

BaseGUI.ContextMenuFunction = function(e)
{
  var evt = (e == null) ? window.event : e;
  var elem = (evt.srcElement != null) ? evt.srcElement : evt.target;

  if(elem.allowRightClick)
    return true;

  while(elem != null)
  {
    if(typeof(BaseGUI) != "undefined" && elem.oncontextmenu == BaseGUI.ContextMenuFunction)
    {
      var item = elem.guiObj;
      if(gIsNetscape)
      {
        item.showContextMenu(evt.pageX, evt.pageY);
      }
      if(gIsExplorer)
      {
	item.showContextMenu(evt.clientX, evt.clientY);
      }
      break;
    }
    elem = elem.parentNode;
  }

  return false;
};

BaseGUI.ClickFunction = function(e)
{
  var evt = (e == null) ? window.event : e;
  var elem = (evt.srcElement != null) ? evt.srcElement : evt.target;

  if(typeof(ContextMenuGUI) != "undefined" && ContextMenuGUI.currentlyDisplayed != null)
  {
    ContextMenuGUI.currentlyDisplayed.hideContextMenu();
  }
};

BaseGUI.prototype.showTooltip = function(x, y)
{
  if(this.tooltipGUI != null && ContextMenuGUI.currentlyDisplayed == null)
  {
    this.tooltipGUI.showTooltip(x, y);
  }
};

BaseGUI.prototype.hideTooltip = function()
{
  if(this.tooltipGUI != null)
  {
    this.tooltipGUI.hideTooltip();
  }
};

BaseGUI.prototype.setTooltipLocation = function(x, y)
{
  if(this.tooltipGUI != null)
  {
    this.tooltipGUI.setTooltipLocation(x, y);
  }
};

BaseGUI.prototype.setTooltip = function(tooltipGUI)
{
  this.tooltipGUI = tooltipGUI;
};

BaseGUI.prototype.clearTooltip = function()
{
  this.tooltipGUI = null;
};

BaseGUI.prototype.showContextMenu = function(x, y)
{
  if(ContextMenuGUI.currentlyDisplayed != null)
  {
    ContextMenuGUI.currentlyDisplayed.hideContextMenu();
  }

  if(this.contextMenuGUI != null)
  {
    if(BaseTooltipGUI.currentlyDisplayed != null)
    {
      BaseTooltipGUI.currentlyDisplayed.hideTooltip();
    }

    this.contextMenuGUI.showContextMenu(x, y);
  }
};

BaseGUI.prototype.hideContextMenu = function()
{
  if(this.contextMenuGUI != null)
  {
    this.contextMenuGUI.hideContextMenu();
  }
};

BaseGUI.prototype.setContextMenu = function(contextMenuGUI)
{
  this.contextMenuGUI = contextMenuGUI;
};

BaseGUI.prototype.clearContextMenu = function()
{
  this.contextMenuGUI = null;
};


// Base Tooltip GUI

BaseTooltipGUI.prototype = new BaseGUI;
BaseTooltipGUI.prototype.constructor = BaseTooltipGUI;
BaseTooltipGUI.superclass = BaseGUI.prototype;

function BaseTooltipGUI()
{
};

BaseTooltipGUI.prototype.delay = UIvalues.tooltipDelay;
BaseTooltipGUI.prototype.rootElementType = "DIV";

BaseTooltipGUI.currentlyDisplayed = null;

BaseTooltipGUI.create = function(parentGUI)
{
  var thisObj = new BaseTooltipGUI();
  thisObj.initialize(parentGUI);
  return thisObj;
};

BaseTooltipGUI.prototype.initialize = function(parentGUI)
{
  BaseTooltipGUI.superclass.initialize.call(this, parentGUI);

  this.parentElement = null;//child of whole page so positioning will work
};

BaseTooltipGUI.prototype.setup = function(tabIndex)
{
  tabIndex = BaseTooltipGUI.superclass.setup.call(this, tabIndex);

  this.rootElement.style.visibility = "hidden";

  this.body = this.createElement("DIV", this.rootElement);
  this.body.className = "ExactPosition Tooltip";
  this.body.style.zIndex = 25;

  if(gIsExplorer)
  {
    this.background = this.createElement("DIV", this.rootElement);
    this.background.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + ImageArray.tooltipBackground.src + "', sizingMethod='scale')";
  }
  else
  {
    this.background = this.createElement("IMG", this.rootElement);
    this.background.src = ImageArray.tooltipBackground.src;
  }
  this.background.className = "ExactPosition";
  this.background.style.zIndex = 24;

  this.shadow1 = this.createElement("DIV", this.rootElement);
  this.shadow1.className = "ExactPosition";
  this.shadow1.style.backgroundColor = UIvalues.tooltipShadowColor;
  this.shadow1.style.zIndex = 24;

  this.shadow2 = this.createElement("DIV", this.rootElement);
  this.shadow2.className = "ExactPosition";
  this.shadow2.style.backgroundColor = UIvalues.tooltipShadowColor;
  this.shadow2.style.zIndex = 24;

  return tabIndex;
};

BaseTooltipGUI.timeoutCookie = null;
BaseTooltipGUI.justClosed = false;

BaseTooltipGUI.prototype.hideTooltip = function()
{
  if(BaseTooltipGUI.timeoutCookie != null)
  {
    clearTimeout(BaseTooltipGUI.timeoutCookie);
    BaseTooltipGUI.timeoutCookie = null;
  }

  this.rootElement.style.visibility = "hidden";

  if(BaseTooltipGUI.currentlyDisplayed != null)
  {
    BaseTooltipGUI.justClosed = true;
  }

  BaseTooltipGUI.currentlyDisplayed = null;

  setTimeout("BaseTooltipGUI.justClosed = false", 25);
};

BaseTooltipGUI.prototype.setTooltipLocation = function(x, y)
{
  if(BaseTooltipGUI.toDisplay != null)
  {
    BaseTooltipGUI.toDisplay.x = x;
    BaseTooltipGUI.toDisplay.y = y;
  }
};

BaseTooltipGUI.toDisplay = null;

BaseTooltipGUI.actuallyShowTooltip = function()
{
  var x = BaseTooltipGUI.toDisplay.x;
  var y = BaseTooltipGUI.toDisplay.y;

  var windowWidth, windowHeight;

  if(window.innerWidth)
  {
    windowHeight = window.innerHeight;
    windowWidth = window.innerWidth;
  }
  else
  {
    windowHeight = document.body.parentElement.scrollHeight;
    windowWidth = document.body.parentElement.scrollWidth;
  }

  BaseTooltipGUI.toDisplay.rootElement.style.left = 0;
  BaseTooltipGUI.toDisplay.rootElement.style.top = 0;
  BaseTooltipGUI.toDisplay.rootElement.style.width = windowWidth;
  BaseTooltipGUI.toDisplay.rootElement.style.height = windowHeight;
  var maxLeft = windowWidth - BaseTooltipGUI.toDisplay.body.offsetWidth - UIvalues.AdminTooltipPadding;
  var maxTop = windowHeight - BaseTooltipGUI.toDisplay.body.offsetHeight - UIvalues.AdminTooltipPadding;
  BaseTooltipGUI.toDisplay.rootElement.style.left = Math.min(x, maxLeft);
  BaseTooltipGUI.toDisplay.rootElement.style.top = Math.min(y, maxTop);
  BaseTooltipGUI.toDisplay.rootElement.style.width = BaseTooltipGUI.toDisplay.body.offsetWidth + 15;
  BaseTooltipGUI.toDisplay.rootElement.style.height = BaseTooltipGUI.toDisplay.body.offsetHeight + 15;

  BaseTooltipGUI.toDisplay.background.style.left = 0;
  BaseTooltipGUI.toDisplay.background.style.top = 0;
  BaseTooltipGUI.toDisplay.background.style.width = BaseTooltipGUI.toDisplay.body.offsetWidth;
  BaseTooltipGUI.toDisplay.background.style.height = BaseTooltipGUI.toDisplay.body.offsetHeight;

  BaseTooltipGUI.toDisplay.shadow1.style.left = UIvalues.tooltipShadowOffset;
  BaseTooltipGUI.toDisplay.shadow1.style.top = BaseTooltipGUI.toDisplay.body.offsetHeight;
  BaseTooltipGUI.toDisplay.shadow1.style.width = BaseTooltipGUI.toDisplay.body.offsetWidth;
  BaseTooltipGUI.toDisplay.shadow1.style.height = UIvalues.tooltipShadowOffset;

  BaseTooltipGUI.toDisplay.shadow2.style.left = BaseTooltipGUI.toDisplay.body.offsetWidth;
  BaseTooltipGUI.toDisplay.shadow2.style.top = UIvalues.tooltipShadowOffset;
  BaseTooltipGUI.toDisplay.shadow2.style.width = UIvalues.tooltipShadowOffset;
  BaseTooltipGUI.toDisplay.shadow2.style.height = BaseTooltipGUI.toDisplay.body.offsetHeight - UIvalues.tooltipShadowOffset;

  BaseTooltipGUI.toDisplay.rootElement.style.visibility = "inherit";

  BaseTooltipGUI.currentlyDisplayed = BaseTooltipGUI.toDisplay;
};

BaseTooltipGUI.prototype.showTooltip = function(x, y)
{
  if(BaseTooltipGUI.timeoutCookie != null)
  {
    clearTimeout(BaseTooltipGUI.timeoutCookie);
    BaseTooltipGUI.timeoutCookie = null;
  }

  BaseTooltipGUI.toDisplay = this;
  if(x != null && y != null)
  {
    BaseTooltipGUI.toDisplay.x = x;
    BaseTooltipGUI.toDisplay.y = y;
  }

  BaseTooltipGUI.timeoutCookie = setTimeout("BaseTooltipGUI.actuallyShowTooltip()", (BaseTooltipGUI.justClosed) ? 1 : this.delay)
};

BaseTooltipGUI.prototype.setTooltip = function(string)
{
  this.body.innerHTML = string;

  if(BaseTooltipGUI.currentlyDisplayed != null)
  {
    BaseTooltipGUI.currentlyDisplayed.showTooltip();
  }
};

BaseTooltipGUI.prototype.clearTooltip = function()
{
  this.body.innerHTML = "";
};

// Context Menu Item

function ContextMenuItem()
{
};

ContextMenuItem.create = function(text, selectionFunction, selectionObject)
{
  var thisObj = new ContextMenuItem();
  thisObj.initialize(text, selectionFunction, selectionObject);
  return thisObj;
};

ContextMenuItem.prototype.initialize = function(text, selectionFunction, selectionObject)
{
  this.text = text;
  this.selectionFunction = selectionFunction;
  this.selectionObject = selectionObject;
};

// Context Menu GUI

ContextMenuGUI.prototype = new BaseGUI;
ContextMenuGUI.prototype.constructor = ContextMenuGUI;
ContextMenuGUI.superclass = BaseGUI.prototype;

function ContextMenuGUI()
{
};

ContextMenuGUI.currentlyDisplayed = null;

ContextMenuGUI.create = function(parentGUI)
{
  var thisObj = new ContextMenuGUI();
  thisObj.initialize(parentGUI);
  return thisObj;
};

ContextMenuGUI.prototype.initialize = function(parentGUI)
{
  ContextMenuGUI.superclass.initialize.call(this, parentGUI);

  this.parentElement = null;//child of whole page so positioning will work
};

ContextMenuGUI.prototype.setup = function(tabIndex)
{
  tabIndex = BaseTooltipGUI.superclass.setup.call(this, tabIndex);

  this.rootElement.className += " ContextMenu";
  this.rootElement.style.backgroundColor = UIvalues.contextUnselectedBackgroundColor;
  this.rootElement.style.zIndex = 25;

  return tabIndex;
};

ContextMenuGUI.prototype.clearContextMenu = function()
{
  this.rootElement.innerHTML = "";
};

ContextMenuGUI.prototype.setContextMenu = function(contextMenuArray)
{
  this.clearContextMenu();
  this.rootElement.style.width = 2000;
  this.rootElement.style.visibility = "inherit";

  this.contextMenuArray = contextMenuArray;
  this.divs = new Array();
  var maxWidth = 0;

  var index = 0;

  for(var i in contextMenuArray)
  {
    if(contextMenuArray[i] == "")
    {
      this.createElement("HR");
    }
    else
    {
      var div = this.divs[index] = this.createElement("DIV");
      var link = div.link = this.createElement("A", div);
      this.createText(contextMenuArray[i].text, link);

      link.contextMenu = this;
      link.div = div;
      link.tabIndex = -1;
      link.href = "#";
      link.onclick = ContextMenuGUI.ClickFunction;

      maxWidth = Math.max(maxWidth, link.offsetWidth + 2 * link.offsetLeft + 2);

      div.index = index;
      div.contextMenu = this;

      div.selectionFunction = contextMenuArray[i].selectionFunction;
      div.selectionObject = contextMenuArray[i].selectionObject;

      div.onclick = ContextMenuGUI.DivClickFunction;
      div.onmouseover = ContextMenuGUI.MouseOverFunction;
      div.onkeypress = ContextMenuGUI.KeyPressFunction;

      index++;
    }
  }

  for(var i in this.divs)
  {
    this.divs[i].style.width = maxWidth;
  }

  this.rootElement.style.width = maxWidth;
  this.rootElement.style.visibility = "hidden";
};

ContextMenuGUI.MouseOverFunction = function(e)
{
  var evt = (e == null) ? window.event : e;
  var elem = (evt.srcElement != null) ? evt.srcElement : evt.target;

  while(elem != null)
  {
    if(elem.onmouseover == ContextMenuGUI.MouseOverFunction)
    {
      var item = elem.contextMenu;

      item.highlight(elem.index);

      break;
    }
    elem = elem.parentNode;
  }

  return false;
};

ContextMenuGUI.ClickFunction = function(e)
{
  var evt = (e == null) ? window.event : e;
  var elem = (evt.srcElement != null) ? evt.srcElement : evt.target;

  while(elem != null)
  {
    if(elem.onclick == ContextMenuGUI.ClickFunction)
    {
      var item = elem.div;

      if(item.selectionFunction != null)
      {
	item.selectionFunction(item.selectionObject);
      }
      break;
    }
    elem = elem.parentNode;
  }

  return false;
};

ContextMenuGUI.DivClickFunction = function(e)
{
  var evt = (e == null) ? window.event : e;
  var elem = (evt.srcElement != null) ? evt.srcElement : evt.target;

  while(elem != null)
  {
    if(elem.onclick == ContextMenuGUI.DivClickFunction)
    {
      var item = elem;

      if(item.selectionFunction != null)
      {
	item.selectionFunction(item.selectionObject);
      }
      break;
    }
    elem = elem.parentNode;
  }

  return false;
};

ContextMenuGUI.KeyPressFunction = function(e)
{
  var evt = (e == null) ? window.event : e;
  var elem = (evt.srcElement != null) ? evt.srcElement : evt.target;

  while(elem != null)
  {
    if(elem.onkeypress == ContextMenuGUI.KeyPressFunction)
    {
      var item = elem.contextMenu;

      if(evt.keyCode == 40)//down arrow
      {
	var index = Number(elem.index) + 1;

	if(index >= item.divs.length)
	  index = 0;

	item.highlight(index);
      }
      else if(evt.keyCode == 38)//up arrow
      {
	var index = Number(elem.index) - 1;

	if(index < 0)
	  index = item.divs.length - 1;

	item.highlight(index);
      }

      break;
    }
    elem = elem.parentNode;
  }
};

ContextMenuGUI.prototype.highlight = function(index)
{
  for(var i in this.divs)
  {
    this.divs[i].style.backgroundColor = UIvalues.contextUnselectedBackgroundColor;
  }

  this.divs[index].style.backgroundColor = UIvalues.contextHighlightedBackgroundColor;
  this.divs[index].firstChild.focus();
};

ContextMenuGUI.prototype.hideContextMenu = function()
{
  this.rootElement.style.visibility = "hidden";

  ContextMenuGUI.currentlyDisplayed = null;
};

ContextMenuGUI.prototype.showContextMenu = function(x, y)
{
  var windowWidth, windowHeight;

  if(window.innerWidth)
  {
    windowHeight = window.innerHeight;
    windowWidth = window.innerWidth;
  }
  else
  {
    windowHeight = document.body.parentElement.scrollHeight;
    windowWidth = document.body.parentElement.scrollWidth;
  }

  this.rootElement.style.left = 0;
  this.rootElement.style.top = 0;
  var maxLeft = windowWidth - this.rootElement.offsetWidth - UIvalues.AdminTooltipPadding;
  var maxTop = windowHeight - this.rootElement.offsetHeight - UIvalues.AdminTooltipPadding;
  this.rootElement.style.left = Math.min(x, maxLeft);
  this.rootElement.style.top = Math.min(y, maxTop);

  this.rootElement.style.visibility = "inherit";

  this.setFocus();

  ContextMenuGUI.currentlyDisplayed = this;
};

ContextMenuGUI.prototype.setFocus = function()
{
  if(this.divs != null && this.divs.length > 0)
  {
    this.divs[0].firstChild.focus();
  }
};

// Base Window GUI

BaseWindowGUI.prototype = new BaseGUI;
BaseWindowGUI.prototype.constructor = BaseWindowGUI;
BaseWindowGUI.prototype.exactPosition = false;
BaseWindowGUI.superclass = BaseGUI.prototype;

function BaseWindowGUI() {
}

BaseWindowGUI.prototype.initialize = function(window) {
  BaseWindowGUI.superclass.initialize.call(this);
  this.winGUI = this;
  this.window = window;
  this.doc = window.document;
  this.rootElement = this.doc.body;
}

BaseWindowGUI.prototype.ready = false;
BaseWindowGUI.prototype.contextMenu = null;

function BaseWindowGUI_ContextMenu(e) {
  var evt = (e == null) ? window.event : e;
  var elem = (evt.srcElement != null) ? evt.srcElement : evt.target;

  if(elem.allowRightClick)
    return true;

  if (evt.preventDefault != null) {
    evt.preventDefault();
  }
  if (evt.stopPropagation != null) {
    evt.stopPropagation();
  } else {
    evt.cancelBubble = true;
  }

  evt.returnValue = false;

  if (this.contextMenu != null) {
    this.contextMenu.startProcessing(evt, evt.clientX, evt.clientY, pane, true);
  }
}

BaseWindowGUI.handleUnload = function(e) {
  window.GUI.close();
};

BaseWindowGUI.prototype.init = function() {
  this.window.onresize = resizeWindow;
  this.window.onunload = BaseWindowGUI.handleUnload;
  this.window.GUI = this;

  this.setup(1);
  this.ready = true;
  this.calculateSize(0, 0);
  this.doLayout();
  this.preDisplay();
  this.setFocus();
}

BaseWindowGUI.prototype.relayout = function() {
  pushFunc("BaseWindowGUI.relayout");

  var oldWidth = this.width;
  var oldHeight = this.height;

  this.calculateSize(0, 0);

  if (oldWidth != this.width || oldHeight != this.height) {
    this.doLayout();
  }

  popFunc();
};

BaseWindowGUI.prototype.close = function() {
  return true;
};

BaseWindowGUI.prototype.setup = function(tabIndex) {
  this.rootElement = this.createElement("DIV");
  this.rootElement.className = "ExactPosition";
  this.rootElement.oncontextmenu = BaseWindowGUI_ContextMenu;
  gSmallVersion = screen.width < 1024 || screen.height < 768;
  if (gSmallVersion) {
    this.rootElement.style.fontSize = 10;
  }
  return tabIndex;
}

BaseWindowGUI.prototype.calculateSize = function(x, y, w, h) {
  pushFunc("BaseWindowGUI.calculateSize");

  if(this.window.innerWidth) {
    this.height = this.window.innerHeight;
    this.width = this.window.innerWidth;
  } else {
    this.height = this.window.document.body.parentElement.scrollHeight;
    this.width = this.window.document.body.parentElement.scrollWidth;
  }

  this.left = x;
  this.top = y;

  popFunc();
}

BaseWindowGUI.prototype.setFinalSize = function() {
  // Size is just the size of the window
}

BaseWindowGUI.prototype.toString = function() {
  return "BaseWindowGUI";
};


BaseAdminWindow.prototype = new BaseWindowGUI;
BaseAdminWindow.prototype.constructor = BaseAdminWindow;
BaseAdminWindow.superclass = BaseWindowGUI.prototype;

function BaseAdminWindow() {
}

BaseAdminWindow.create = function(window) {
  var thisObj = new BaseAdminWindow();
  thisObj.initialize(window);
  return thisObj;
};

BaseAdminWindow.handleMouseDown = function(e) {
  var evt = (e == null) ? window.event : e;
  var elem = (evt.srcElement != null) ? evt.srcElement : evt.target;

  var tagName = (elem.tagName != null) ? elem.tagName.toLowerCase() : "";
  if (tagName == "input" || tagName == "select" || tagName == "textarea" || elem.allowMouseEvents) {
    elem.onmousemove = null;
    elem.onmouseup = null;
    return;
  }

  if (gIsNetscape && evt.button == 1) {
    evt.cancelBubble = true;
    evt.stopPropagation();
    evt.preventDefault();
    return false;
  }

  while (elem != null && elem.onmousedown != BaseAdminWindow.handleMouseDown) {
    elem = elem.parentNode;
  }

  if (elem == null) {
    return;
  }

  if (evt.preventDefault != null) {
    evt.preventDefault();
  }
  if (evt.stopPropagation != null) {
    evt.stopPropagation();
  } else {
    evt.cancelBubble = true;
  }

  evt.returnValue = false;

  elem.onmousemove = BaseAdminWindow.handleMouseMove;
  elem.onmouseup = BaseAdminWindow.handleMouseUp;
};

BaseAdminWindow.handleMouseUp = function(e) {
  var evt = (e == null) ? window.event : e;
  var elem = (evt.srcElement != null) ? evt.srcElement : evt.target;

  while (elem != null && elem.onmouseup != BaseAdminWindow.handleMouseUp) {
    elem = elem.parentNode;
  }

  if (elem == null) {
    return;
  }

  if (evt.preventDefault != null) {
    evt.preventDefault();
  }
  if (evt.stopPropagation != null) {
    evt.stopPropagation();
  } else {
    evt.cancelBubble = true;
  }

  evt.returnValue = false;

  elem.onmousemove = null;
  elem.onmouseup = null;

};

BaseAdminWindow.handleMouseMove = function(e) {
  var evt = (e == null) ? window.event : e;
  var elem = (evt.srcElement != null) ? evt.srcElement : evt.target;

  if (evt.preventDefault != null) {
    evt.preventDefault();
  }
  if (evt.stopPropagation != null) {
    evt.stopPropagation();
  } else {
    evt.cancelBubble = true;
  }

  evt.returnValue = false;
};

BaseAdminWindow.prototype.setup = function(tabIndex) {
  tabIndex = BaseAdminWindow.superclass.setup.call(this, tabIndex);

  this.rootElement.onmousedown = BaseAdminWindow.handleMouseDown;

  return tabIndex;
};


