// Global variable. Holds.
var GLOBAL_currentMaxZIndex = 0;  
var GLOBAL_absoluteMaxZIndex = "1000";
var GLOBAL_windowBackgroundColor;
var GLOBAL_titleBarBackgroundColor;  
var GLOBAL_borderStyle;    
var GLOBAL_borderWidth;    
var GLOBAL_borderColor;

/**
 * This method creates a generic window object. All windows are derived from the generic window.
 *
 * @param {String}  title    The title of the window.
 * @param {String}  width    The width of the window. Allowed parameters are any numeric values like '50%', '100px', '10em' or 'auto'.
 * @param {String}  height   The height of the window. Allowed parameters are any numeric values like '50%', '100px', '10em' or 'auto'.
 * @param {String}  posX     The horizontal position of the window. Allowed paramters are any numeric values like '50%', '100px' or 'auto'.
 * @param {String}  posY     The vertical position of the window. Allowed paramters are any numeric values like '50%', '100px' or 'auto'.
 * @param {String}  iconSrc  If defined, the icon at the specified location will be used. If not, a generic icon will be used instead.
 */
function GenericWindow(title, width, height, posX, posY, iconSrc) {              
  var dialog = document.createElement("div");
  var dialogRandomId = Math.round(Math.random()*10000);    
  dialog.id = "dialog_"+dialogRandomId;      

  var classAttribute = document.createAttribute("class");
  classAttribute.nodeValue = "dialog";
  dialog.setAttributeNode(classAttribute);            
  dialog.style.position = "fixed";
  if (height) dialog.style.height = height; else dialog.style.height = "auto";
  dialog.style.minHeight = "20px";
  if (width) dialog.style.width = width; else dialog.style.width = "auto";
  dialog.style.minWidth = "20px";    
  if (posY) dialog.style.top = posY;
  if (posX) dialog.style.left = posX;          
  dialog.style.overflow = "hidden";
  GLOBAL_windowBackgroundColor = dialog.style.backgroundColor;
  GLOBAL_borderStyle = dialog.style.borderStyle;
  GLOBAL_borderWidth = dialog.style.borderWidth;
  GLOBAL_borderColor = dialog.style.borderColor;
  dialog.onclick = function() {                                
    this.style.zIndex = checkCurrentMaxZIndex(this.style.zIndex);
    return false;
  };    
  dialog.style.zIndex = checkCurrentMaxZIndex(GLOBAL_currentMaxZIndex);
  
  var titleBar = document.createElement("div");
  titleBar.id = dialog.id+"_titleBar";
  var classAttribute = document.createAttribute("class");
  classAttribute.nodeValue = "titleBar";
  titleBar.setAttributeNode(classAttribute);
  GLOBAL_titleBarBackgroundColor = titleBar.style.backgroundColor;
  titleBar.onmousedown = function() {
                                      titleBar.onclick();
                                      dragStart(this.parentNode);
                                      return false;
                                    };                        
  titleBar.style.height = "1.5em";                        
  titleBar.style.cursor = "move";            
  titleBar.onclick = function() {                                      
                                  this.parentNode.style.zIndex = checkCurrentMaxZIndex(this.parentNode.style.zIndex); return false;
                                };    
  titleBar.ondblclick = function() {
                                      if (this.parentNode.style.width != "100%") {
                                        this.oldWidth = this.parentNode.style.width;
                                        this.oldHeight = this.parentNode.style.height;
                                        this.oldLeft = this.parentNode.style.left;
                                        this.oldTop = this.parentNode.style.top;

                                        this.parentNode.style.left = "0px";
                                        this.parentNode.style.top = "0px";
                                        this.parentNode.style.width = "100%";
                                        this.parentNode.style.height = "100%";                                       
                                      }
                                      else if (this.oldWidth) {
                                        this.parentNode.style.left = this.oldLeft;
                                        this.parentNode.style.top = this.oldTop;
                                        this.parentNode.style.width = this.oldWidth;
                                        this.parentNode.style.height = this.oldHeight;                                                                              
                                      } 
                                      document.getElementById(dialog.id+"_titleBar").style.width = (document.getElementById(dialog.id).offsetWidth - 4) + "px";
                                      document.getElementById(dialog.id+"_statusBar").style.width = (document.getElementById(dialog.id).offsetWidth - 4) + "px";
                                      var content = document.getElementById(dialog.id+"_content");
                                      content.style.width = (dialog.offsetWidth - 10) + "px";            
                                      content.style.height = (dialog.offsetHeight - 50) + "px";            

                                      this.parentNode.style.zIndex = checkCurrentMaxZIndex(this.parentNode.style.zIndex);                                                                              
                                      return false;
                                  };    

  var icon = document.createElement("img");
  icon.id = dialog.id+"_icon";
  icon.style.position = "absolute";
  icon.style.cursor = "pointer";
  icon.style.left = "2px";
  icon.style.top = "2px";
  icon.style.display = "none";
  icon.onclick = function() {                               
                              toggleVisibility(dialog.id + "_iconMenu");                              
                              return false;
                            };     
  icon.ondblclick = function() { return false; };     

  icon.style.width = "1.3em";
  icon.style.height = "1.3em";    
  if (iconSrc) icon.src = iconSrc; else icon.src = "./rootsrock/resources/standardIcon.png";    
  dialog.appendChild(icon);                                 

  var iconMenu = document.createElement("div");
  iconMenu.id = dialog.id + "_iconMenu";
  var classAttribute = document.createAttribute("class");
  classAttribute.nodeValue = "dialog";
  iconMenu.setAttributeNode(classAttribute);        
  iconMenu.style.position = "absolute";
  iconMenu.style.zIndex = GLOBAL_absoluteMaxZIndex;
  iconMenu.style.display = "none";
  iconMenu.style.cursor = "default";
  iconMenu.style.left = "2px";                                  
  iconMenu.style.top = "1.5em";
  iconMenu.style.padding = "0.2em";

  var option = document.createElement("span");
  option.innerHTML = "Minimize<br />";
  option.onmouseover = function() { this.style.backgroundColor = "#FFFF80"; return false; };
  option.onmouseout = function() { this.style.backgroundColor = GLOBAL_windowBackgroundColor; return false; };
  option.onclick = function() {
                                minimizeButton.onclick();
                                this.parentNode.style.display = "none";
                                return false;
                              };
  iconMenu.appendChild(option);

  var option = document.createElement("span");
  option.innerHTML = "Maximize<br />";
  option.onmouseover = function() { this.style.backgroundColor = "#FFFF80"; return false; };
  option.onmouseout = function() { this.style.backgroundColor = GLOBAL_windowBackgroundColor; return false; };
  option.onclick = function() {
                                titleBar.ondblclick();
                                this.parentNode.style.display = "none";
                                return false;
                              };
  iconMenu.appendChild(option);                              

  var option = document.createElement("span");
  option.innerHTML = "-----------<br />";
  option.style.color = "#CCCCCC";
  option.onclick = function() { return false; };
  iconMenu.appendChild(option);                                                                                                  

  var option = document.createElement("span");
  option.innerHTML = "Close<br />";
  option.onmouseover = function() { this.style.backgroundColor = "#FFFF80"; return false; };
  option.onmouseout = function() { this.style.backgroundColor = GLOBAL_windowBackgroundColor; return false; };
  option.onclick = function() {
                                closeButton.onclick();
                                this.parentNode.style.display = "none";
                                return false;
                              };
  iconMenu.appendChild(option);                              

  var option = document.createElement("span");
  option.innerHTML = "-----------<br />";
  option.style.color = "#CCCCCC";
  option.onclick = function() { return false; };
  iconMenu.appendChild(option);                                                                                                                                    

  var option = document.createElement("span");
  option.innerHTML = "[ ] Always On Top";
  option.onmouseover = function() { this.style.backgroundColor = "#FFFF80"; return false; };
  option.onmouseout = function() { this.style.backgroundColor = GLOBAL_windowBackgroundColor; return false; };
  option.id = dialog.id + "_onTopButton";
  this.oldZIndex = 1;
  option.onclick = function() {                                
                                if (this.parentNode.parentNode.style.zIndex != GLOBAL_absoluteMaxZIndex) {
                                  this.oldZIndex = this.parentNode.parentNode.style.zIndex;
                                  option.innerHTML = "[X] Always On Top";
                                  document.getElementById(dialog.id).style.zIndex = GLOBAL_absoluteMaxZIndex;
                                } else {                                
                                  option.innerHTML = "[ ] Always On Top";                                  
                                  document.getElementById(dialog.id).style.zIndex = this.oldZIndex;
                                }                                
                                this.parentNode.style.display = "none";
                                return false;
                              };
  iconMenu.appendChild(option);

  dialog.appendChild(iconMenu);        

  var caption = document.createElement("span");
  caption.id = dialog.id+"_caption";
  caption.style.position = "absolute";
  caption.style.left = "0.2em";
  caption.style.top = "2px";        
  var classAttribute = document.createAttribute("class");
  classAttribute.nodeValue = "caption";
  caption.setAttributeNode(classAttribute);
  caption.appendChild(document.createTextNode(title));
  titleBar.appendChild(caption);
  dialog.appendChild(titleBar);                

  var minimizeButton = document.createElement("span");
  minimizeButton.id = dialog.id+"_minimizeButton";
  minimizeButton.appendChild(document.createTextNode("V"));
  var classAttribute = document.createAttribute("class");
  classAttribute.nodeValue = "minimizeButton";
  minimizeButton.setAttributeNode(classAttribute);
  minimizeButton.style.display = "none";
  minimizeButton.style.position = "absolute";
  minimizeButton.style.right = "2.6em";
  minimizeButton.style.top = "2px";
  minimizeButton.style.width = "1.1em";
  minimizeButton.style.textAlign = "center";
  minimizeButton.style.fontFamily = "Verdana,Arial,Helvetica,sans-serif";    
  minimizeButton.style.fontSize = "1.0em";
  minimizeButton.style.cursor = "pointer";
  minimizeButton.onmouseover = function()  { this.style.fontWeight = "bold"; return false; };
  minimizeButton.onmouseout = function()  { this.style.fontWeight = "normal"; return false; };
  minimizeButton.onclick = function() {    
                                        var oldWidth = dialog.style.width;
                                        var oldHeight = dialog.style.height;
                                        var oldLeft = dialog.style.left;
                                        var oldTop = dialog.style.top;                                          
                                        var oldIconOnClick = document.getElementById(dialog.id+"_icon").onclick;                                                                                    

                                        var statusBar = document.getElementById(dialog.id+"_statusBar");
                                        statusBar.style.display = "none";                                          
                                        var content = document.getElementById(dialog.id+"_content");
                                        content.style.display = "none";                                          
                                        var caption = document.getElementById(dialog.id+"_caption");
                                        caption.style.top = "3.5em";                                          
                                        caption.style.left = "0.2em";
                                        caption.style.cursor = "pointer";
                                        caption.style.backgroundColor = "#FFFF80";
                                        var maximizeButton = document.getElementById(dialog.id+"_maximizeButton");
                                        maximizeButton.style.display = "none";
                                        var peekBehindIcon = document.getElementById(dialog.id+"_peekBehindIcon");
                                        peekBehindIcon.style.display = "none";                                          
                                        var minimizeButton = document.getElementById(dialog.id+"_minimizeButton");
                                        minimizeButton.style.display = "none";                                          
                                        var closeButton = document.getElementById(dialog.id+"_closeButton");
                                        closeButton.style.display = "none";                                          
                                        var resizeArea = document.getElementById(dialog.id+"_resizeArea");
                                        resizeArea.style.display = "none";                                          
                                        var iconMenu = document.getElementById(dialog.id + "_iconMenu");
                                        iconMenu.style.display = "none";
                                        var icon = document.getElementById(dialog.id + "_icon");
                                        icon.style.width = "3em";                                          
                                        icon.style.height = "3em";                                                                      
                                        dialog.style.width = "16.2em";
                                        dialog.style.height = "5em";
                                        dialog.style.left = "10px";
                                        dialog.style.bottom = "10px";
                                        dialog.style.border = "none";
                                        dialog.style.backgroundColor = "transparent";
                                        document.getElementById(dialog.id+"_titleBar").style.backgroundColor = "transparent";

                                        icon.onclick = function() { return false; };
                                        icon.onmousedown = function() { dragStart(this.parentNode); return false; };
                                        icon.ondblclick = function() { restoreWindowfunction(dialog.id, oldWidth, oldHeight, oldLeft, oldTop, oldIconOnClick); return false; };
                                        return false;
                                      };
  dialog.appendChild(minimizeButton);        

  var maximizeButton = document.createElement("span");
  maximizeButton.id = dialog.id+"_maximizeButton";
  maximizeButton.appendChild(document.createTextNode("^"));
  var classAttribute = document.createAttribute("class");
  classAttribute.nodeValue = "maximizeButton";
  maximizeButton.setAttributeNode(classAttribute);
  maximizeButton.style.position = "absolute";
  maximizeButton.style.display = "none";
  maximizeButton.style.right = "1.4em";
  maximizeButton.style.top = "2px";
  maximizeButton.style.width = "1.1em";
  maximizeButton.style.textAlign = "center";    
  maximizeButton.style.fontFamily = "Verdana,Arial,Helvetica,sans-serif";    
  maximizeButton.style.fontSize = "1.0em";
  maximizeButton.style.cursor = "pointer";
  maximizeButton.onmouseover = function()  { this.style.fontWeight = "bold"; return false; };
  maximizeButton.onmouseout = function()  { this.style.fontWeight = "normal"; return false; };
  maximizeButton.onclick = function() { titleBar.ondblclick(); return false; };
  dialog.appendChild(maximizeButton);    

  var closeButton = document.createElement("span");
  closeButton.id = dialog.id+"_closeButton";
  closeButton.appendChild(document.createTextNode("X"));
  var classAttribute = document.createAttribute("class");
  classAttribute.nodeValue = "closeButton";
  closeButton.setAttributeNode(classAttribute);
  closeButton.style.position = "absolute";
  closeButton.style.right = "0.2em";
  closeButton.style.width = "1.1em";
  closeButton.style.textAlign = "center";    
  closeButton.style.top = "2px";
  closeButton.style.fontFamily = "Verdana,Arial,Helvetica,sans-serif";    
  closeButton.style.fontSize = "1.0em";
  closeButton.style.cursor = "pointer";
  closeButton.onmouseover = function()  { this.style.fontWeight = "bold"; return false; };
  closeButton.onmouseout = function()  { this.style.fontWeight = "normal"; return false; };
  closeButton.onclick = function() {
                                    document.getElementsByTagName("body")[0].removeChild(document.getElementById(dialog.id));
                                    return false;
                                  };
  dialog.appendChild(closeButton);    

  var peekBehindIcon = document.createElement("img");
  peekBehindIcon.id = dialog.id + "_peekBehindIcon";    
  peekBehindIcon.src = "./rootsrock/resources/peekBehindActive.png";        
  peekBehindIcon.style.position = "absolute";
  peekBehindIcon.style.right = "3.8em";    
  peekBehindIcon.style.width = "1.2em";        
  peekBehindIcon.style.top = "2px";
  peekBehindIcon.onmouseover = function()  {
                                              this.parentNode.style.opacity = "0.5";
                                              this.parentNode.style.filter = "alpha(opacity=50)";
                                              peekBehindIcon.src = "./rootsrock/resources/peekBehindPassive.png";
                                              return false;
                                          };
  peekBehindIcon.onmouseout = function()  {
                                              this.parentNode.style.opacity = "1.0";
                                              this.parentNode.style.filter = "alpha(opacity=100)";
                                              peekBehindIcon.src = "./rootsrock/resources/peekBehindActive.png";
                                              return false;
                                          };
  dialog.appendChild(peekBehindIcon);    


  var content = document.createElement("div");    
  content.id = dialog.id+"_content";  
  content.style.position = "absolute";    
  content.style.padding = "0.2em";
  
  var dialogWidthUnit = dialog.style.width.replace(/[0-9]*/, "");  
  var dialogWidth = parseFloat(dialog.style.width);    
  switch (dialogWidthUnit) {
    case "px":      
      content.style.width = (dialogWidth - 10) + dialogWidthUnit;            
      break;
    case "%":
      content.style.width = "99.8" + dialogWidthUnit;          
      break;
    case "em":         
      content.style.width = (dialogWidth - 0.2) + dialogWidthUnit;    
      break;
    default:
      break;      
  }
  
  var dialogHeightUnit = dialog.style.height.replace(/[0-9]*/, "");
  var dialogHeight = parseFloat(dialog.style.height);  
  switch (dialogHeightUnit) {
    case "px":            
      content.style.height = (dialogHeight - 50) + dialogHeightUnit;            
      break;
    case "%":    
      content.style.height = "93" + dialogHeightUnit;            
      break;
    case "em":           
      content.style.height = (dialogHeight - 3.2) + dialogHeightUnit;
      break;
    default:
      break;      
  }
    
  if (navigator.appName == "Microsoft Internet Explorer") content.style.overflow = "scroll"; else content.style.overflow = "auto";
  dialog.appendChild(content);

  var statusBar = document.createElement("div");
  statusBar.id = dialog.id+"_statusBar";
  var classAttribute = document.createAttribute("class");
  classAttribute.nodeValue = "statusBar";
  statusBar.setAttributeNode(classAttribute);    
  statusBar.style.height = "1.3em";                            
  statusBar.style.fontSize = "1.0em"; 
  statusBar.style.paddingLeft = "4px";
  statusBar.style.position = "absolute";
  statusBar.style.bottom = "0px";
  statusBar.style.left = "0px";                            
  statusBar.style.display = "none";    

  dialog.appendChild(statusBar);    

  var resizeArea = document.createElement("div");    
  resizeArea.id = dialog.id+"_resizeArea";
  resizeArea.style.position = "absolute";
  resizeArea.style.bottom = "0px";
  resizeArea.style.right = "0px";    
  resizeArea.style.display = "none";
  resizeArea.style.cursor = "nw-resize";    
  resizeArea.onmousedown = function() { resizeStart(this); resizeInit(this); return false; };                        
  var img = document.createElement("img");
  img.src = "./rootsrock/resources/resizeArea.png";
  resizeArea.appendChild(img);
  dialog.appendChild(resizeArea);

  document.getElementsByTagName("body")[0].insertBefore(dialog, document.getElementsByTagName("body")[0].firstChild);    

  statusBar.style.width = (dialog.offsetWidth - parseInt(statusBar.style.paddingLeft) - 4) + "px";        
  
  return dialog;
}

function restoreWindowfunction(dialogId, oldWidth, oldHeight, oldLeft, oldTop, oldIconOnClick) {    
  var statusBar = document.getElementById(dialogId+"_statusBar");
  statusBar.style.display = "block";                                          
  var caption = document.getElementById(dialogId+"_caption");
  caption.style.top = "2px";        
  caption.style.left = "2em";        
  caption.style.cursor = "move";
  caption.style.backgroundColor = "transparent";
  var maximizeButton = document.getElementById(dialogId+"_maximizeButton");
  maximizeButton.style.display = "block";
  var minimizeButton = document.getElementById(dialogId+"_minimizeButton");
  minimizeButton.style.display = "block";                                          
  var closeButton = document.getElementById(dialogId+"_closeButton");
  closeButton.style.display = "block";                                          
  var peekBehindIcon = document.getElementById(dialogId+"_peekBehindIcon");
  peekBehindIcon.style.display = "block";                                              
  var resizeArea = document.getElementById(dialogId+"_resizeArea");
  resizeArea.style.display = "block";                                          
  var content = document.getElementById(dialogId+"_content");
  content.style.display = "block";                                              
  var icon = document.getElementById(dialogId + "_icon");
  icon.style.width = "1.3em";                                          
  icon.style.height = "1.3em";                       
  icon.onclick = oldIconOnClick;
  icon.onmousedown = function() { return false; };
  var dialog = document.getElementById(dialogId);
  dialog.style.width = oldWidth;
  dialog.style.height = oldHeight;
  dialog.style.left = oldLeft;
  dialog.style.top = oldTop
  dialog.style.borderStyle = GLOBAL_borderStyle;
  dialog.style.borderWidth = GLOBAL_borderWidth;
  dialog.style.borderColor = GLOBAL_borderColor;
  dialog.style.backgroundColor = GLOBAL_windowBackgroundColor;
  document.getElementById(dialog.id+"_titleBar").style.backgroundColor = GLOBAL_titleBarBackgroundColor;  

  return false;
};

var GLOBAL_dragObject = null;
var GLOBAL_dragX = 0;
var GLOBAL_dragY = 0;

var GLOBAL_mouseX = 0;
var GLOBAL_mouseY = 0;

function dragInit() {
  document.onmousemove = drag;
  document.onmouseup = dragStop;
}

function dragStart(element) {
  GLOBAL_dragObject = element;
  GLOBAL_dragX = GLOBAL_mouseX - GLOBAL_dragObject.offsetLeft;
  GLOBAL_dragY = GLOBAL_mouseY - GLOBAL_dragObject.offsetTop;
}

function dragStop() {
  GLOBAL_dragObject = null;
}

function drag(anEvent) {
  GLOBAL_mouseX = document.all? window.event.clientX : anEvent.pageX;
  GLOBAL_mouseY = document.all? window.event.clientY : anEvent.pageY;
  if(GLOBAL_dragObject != null) {
    GLOBAL_dragObject.style.left = (GLOBAL_mouseX - GLOBAL_dragX) + "px";
    GLOBAL_dragObject.style.top = (GLOBAL_mouseY - GLOBAL_dragY) + "px";
  }
}


var GLOBAL_resizeObject = null;
var GLOBAL_resizeX = 0;
var GLOBAL_resizeY = 0;

function resizeInit(element) {       
  document.onmouseup = function() { resizeStop(); return false; };
  document.onmousemove = function(element) { resize(element); return false; };
}  

function resizeStart(element) {  
  GLOBAL_resizeObject = element;        
  GLOBAL_resizeX = GLOBAL_resizeObject.offsetLeft;    
  GLOBAL_resizeY = GLOBAL_resizeObject.offsetTop;
  document.getElementsByTagName("body")[0].style.cursor = "nw-resize";    
}

function resizeStop() {
  GLOBAL_resizeObject = null;
  document.onmousemove = drag;
  document.onmouseup = dragStop;    
  document.getElementsByTagName("body")[0].style.cursor = "default";    
}

function resize(anEvent) {
  GLOBAL_mouseX = document.all? window.event.clientX : anEvent.pageX;
  GLOBAL_mouseY = document.all? window.event.clientY : anEvent.pageY;    
  GLOBAL_resizeX = GLOBAL_resizeObject.offsetLeft;
  GLOBAL_resizeY = GLOBAL_resizeObject.offsetTop;
  if(GLOBAL_resizeObject != null) {    
    var dialog = GLOBAL_resizeObject.parentNode;
    dialog.style.width = (dialog.parentNode.offsetWidth + (GLOBAL_mouseX - GLOBAL_resizeX - dialog.offsetLeft) > parseInt(dialog.style.minWidth))? dialog.offsetWidth + (GLOBAL_mouseX - GLOBAL_resizeX - dialog.offsetLeft)+ "px" : null;      
    dialog.style.height = (dialog.offsetHeight + (GLOBAL_mouseY - GLOBAL_resizeY - dialog.offsetTop) > parseInt(dialog.style.minHeight))? dialog.offsetHeight + (GLOBAL_mouseY - GLOBAL_resizeY - dialog.offsetTop) + "px" : null;
    var statusBar = document.getElementById(dialog.id+"_statusBar");
    statusBar.style.width = (dialog.offsetWidth - parseInt(statusBar.style.paddingLeft) - 4) + "px";
    document.getElementById(dialog.id+"_titleBar").style.width = (document.getElementById(dialog.id).offsetWidth - 4) + "px";
    var content = document.getElementById(dialog.id+"_content");
    content.style.width = (dialog.offsetWidth - 10) + "px";            
    content.style.height = (dialog.offsetHeight - 50) + "px";            
  }
}  

function toggleVisibility(elementId) {
  if (document.getElementById(elementId)) {
    if (document.getElementById(elementId).style.display == "block") document.getElementById(elementId).style.display = "none";
    else document.getElementById(elementId).style.display = "block";
  }
}

function show(elementId) {
  document.getElementById(elementId).style.display = "block";
}

function hide(elementId) {
  document.getElementById(elementId).style.display = "none";
}


function checkCurrentMaxZIndex(zIndex) {  
  if (zIndex <= GLOBAL_currentMaxZIndex) {
    if (parseInt(GLOBAL_currentMaxZIndex)+1 < GLOBAL_absoluteMaxZIndex) {
      GLOBAL_currentMaxZIndex = parseInt(GLOBAL_currentMaxZIndex) + 1;      
    }
    return GLOBAL_currentMaxZIndex;
  }    
  else return zIndex;
};  

function SimpleWindow(title, width, height, posX, posY, iconSrc) {
  var dialog = new GenericWindow(title, width, height, posX, posY, iconSrc);   

  var content = document.getElementById(dialog.id + "_content");
  content.style.overflow = "hidden";
  
  var peekBehindIcon = document.getElementById(dialog.id + "_peekBehindIcon");
  peekBehindIcon.style.right = "1.4em";
  
  if (navigator.appName == "Microsoft Internet Explorer") document.getElementById(dialog.id+"_titleBar").style.width = (document.getElementById(dialog.id).offsetWidth - 4) + "px";

  this.addImage = function(imgSrc, imgAlt, imgWidth, imgHeight, imgAlign) { addImg(document.getElementById(dialog.id+"_content"), imgSrc, imgAlt, imgWidth, imgHeight, imgAlign); return false; };   
  this.addParagraph = function(innerHtml, textAlign, border, convertLineBreaks) { addP(document.getElementById(dialog.id+"_content"), innerHtml, textAlign, border, convertLineBreaks); return false; };       
  this.addHtml = function(innerHtml, convertLineBreaks) { addHtml(document.getElementById(dialog.id+"_content"), innerHtml, convertLineBreaks); return false; };
  this.addDom = function(domStructure) { addDom(document.getElementById(dialog.id+"_content"), domStructure); return false; };
  this.addTextarea = function(text, cols, rows, readonly, labelText) { addTextarea(document.getElementById(dialog.id+"_content"), text, cols, rows, readonly, labelText); return false; };
  this.addSelect = function(options, size, multiple, labelText) { addSelect(document.getElementById(dialog.id+"_content"), options, size, multiple, labelText); return false; };
  this.addRadioGroup = function(options, radioGroupLabel, selectedIndex) { addRadioGroup(document.getElementById(dialog.id+"_content"), options, radioGroupLabel, selectedIndex); return false; };
  this.close = function() { closeWindow(document.getElementById(dialog.id+"_closeButton")); return false; };
  this.minimize = function() { minimizeWindow(document.getElementById(dialog.id+"_minimizeButton")); return false; };
  this.maximize = function() { maximizeWindow(document.getElementById(dialog.id+"_maximizeButton")); return false; };
  this.invisible = function() { invisibleWindow(document.getElementById(dialog.id)); return false; };
  this.visible = function() { visibleWindow(document.getElementById(dialog.id)); return false; };  
  this.onTop = function() { onTopWindow(document.getElementById(dialog.id+"_onTopButton")); return false; };
  this.statusText = function(text) { statusBarText(document.getElementById(dialog.id+"_statusBar"), text); return false; };  
  this.width = function(width) { setWidth(document.getElementById(dialog.id), document.getElementById(dialog.id+"_content"), width); return false; };  
  this.height = function(height) { setHeight(document.getElementById(dialog.id), document.getElementById(dialog.id+"_content"), height); return false; };  
}        

function NormalWindow(title, statusText, width, height, posX, posY, iconSrc) {
  var dialog = new GenericWindow(title, width, height, posX, posY, iconSrc);   

  var icon = document.getElementById(dialog.id+"_icon");
  icon.style.display = "block";

  var caption = document.getElementById(dialog.id+"_caption"); 
  caption.style.left = "2em";

  var maximizeButton = document.getElementById(dialog.id+"_maximizeButton");
  maximizeButton.style.display = "block";

  var minimizeButton = document.getElementById(dialog.id+"_minimizeButton");
  minimizeButton.style.display = "block";        

  var statusBar = document.getElementById(dialog.id+"_statusBar");
  statusBar.style.display = "block";
  if (statusText) statusBar.appendChild(document.createTextNode(statusText)); else statusBar.appendChild(document.createTextNode(" "));   

  var resizeArea = document.getElementById(dialog.id+"_resizeArea");
  resizeArea.style.display = "block";

  if (navigator.appName == "Microsoft Internet Explorer") document.getElementById(dialog.id+"_titleBar").style.width = (document.getElementById(dialog.id).offsetWidth - 4) + "px";    

  this.addImage = function(imgSrc, imgAlt, imgWidth, imgHeight, imgAlign) { addImg(document.getElementById(dialog.id+"_content"), imgSrc, imgAlt, imgWidth, imgHeight, imgAlign); return false; };   
  this.addParagraph = function(innerHtml, textAlign, border) { addP(document.getElementById(dialog.id+"_content"), innerHtml, textAlign, border); return false; };   
  this.addHtml = function(innerHtml, convertLineBreaks) { addHtml(document.getElementById(dialog.id+"_content"), innerHtml, convertLineBreaks); return false; };
  this.addDom = function(domStructure) { addDom(document.getElementById(dialog.id+"_content"), domStructure); return false; };
  this.addTextarea = function(text, cols, rows, readonly, labelText) { addTextarea(document.getElementById(dialog.id+"_content"), text, cols, rows, readonly, labelText); return false; };
  this.addSelect = function(options, size, multiple, labelText) { addSelect(document.getElementById(dialog.id+"_content"), options, size, multiple, labelText); return false; };
  this.addRadioGroup = function(options, radioGroupLabel, selectedIndex) { addRadioGroup(document.getElementById(dialog.id+"_content"), options, radioGroupLabel, selectedIndex); return false; };
  this.close = function() { closeWindow(document.getElementById(dialog.id+"_closeButton")); return false; };
  this.minimize = function() { minimizeWindow(document.getElementById(dialog.id+"_minimizeButton")); return false; };
  this.maximize = function() { maximizeWindow(document.getElementById(dialog.id+"_maximizeButton")); return false; };
  this.invisible = function() { invisibleWindow(document.getElementById(dialog.id)); return false; };
  this.visible = function() { visibleWindow(document.getElementById(dialog.id)); return false; };
  this.onTop = function() { onTopWindow(document.getElementById(dialog.id+"_onTopButton")); return false; };    
  this.statusText = function(text) { statusBarText(document.getElementById(dialog.id+"_statusBar"), text); return false; };  
  this.width = function(width) { setWidth(document.getElementById(dialog.id), document.getElementById(dialog.id+"_content"), width); return false; };  
  this.height = function(height) { setHeight(document.getElementById(dialog.id), document.getElementById(dialog.id+"_content"), height); return false; };  
}  

function OkayDialog(title, onOkayClick, width, height, posX, posY) {
  var dialog = new GenericWindow(title, width, height, posX, posY);

  var buttonPanel = document.createElement("div");    
  buttonPanel.style.textAlign = "center";

  var input = document.createElement("button");    
  input.style.whiteSpace = "nowrap";
  input.style.margin = "5px";
  var img = document.createElement("img");
  img.src = "./rootsrock/resources/ok.png";
  img.align = "absmiddle";
  input.appendChild(img);
  input.appendChild(document.createTextNode(" OK"));
  input.onclick = onOkayClick;    

  buttonPanel.appendChild(input);
  dialog.appendChild(buttonPanel);
  if (navigator.appName == "Microsoft Internet Explorer") document.getElementById(dialog.id+"_titleBar").style.width = (document.getElementById(dialog.id).offsetWidth - 4) + "px";    
}

function OkayCancelDialog(title, onOkayClick, onCancelClick, width, height, posX, posY) {
  var dialog = new GenericWindow(title, width, height, posX, posY);    

  var buttonPanel = document.createElement("div");    
  buttonPanel.style.textAlign = "center";    

  var input = document.createElement("button");
  input.style.whiteSpace = "nowrap";
  input.style.margin = "5px";
  var img = document.createElement("img");
  img.src = "./rootsrock/resources/ok.png";
  img.align = "absmiddle";
  input.appendChild(img);
  input.appendChild(document.createTextNode(" OK"));
  input.onclick = onOkayClick;

  buttonPanel.appendChild(input);

  var input = document.createElement("button");
  input.style.whiteSpace = "nowrap";
  input.style.margin = "5px";
  var img = document.createElement("img");
  img.src = "./rootsrock/resources/cancel.png";
  img.align = "absmiddle";
  input.appendChild(img);
  input.appendChild(document.createTextNode(" Cancel"));
  input.onclick = function() {
                                document.getElementsByTagName("body")[0].removeChild(document.getElementById(dialog.id));
                                if (onCancelClick != null) onCancelClick();
                                return false;
                            };

  buttonPanel.appendChild(input);
  dialog.appendChild(buttonPanel);                               
  if (navigator.appName == "Microsoft Internet Explorer") document.getElementById(dialog.id+"_titleBar").style.width = (document.getElementById(dialog.id).offsetWidth - 4) + "px";
}

function AlertDialog(title, message, width, height, posX, posY) {
  var dialog = new GenericWindow(title, width, height, posX, posY);    

  document.getElementById(dialog.id+"_content").appendChild(document.createTextNode(message));    

  var buttonPanel = document.createElement("div");    
  buttonPanel.style.textAlign = "center";

  var input = document.createElement("button");
  input.style.whiteSpace = "nowrap";
  input.style.margin = "5px";
  var img = document.createElement("img");
  img.src = "./rootsrock/resources/alert.png";
  img.align = "absmiddle";
  input.appendChild(img);    
  input.appendChild(document.createTextNode(" OK"));
  input.onclick = function() {
                                document.getElementsByTagName("body")[0].removeChild(document.getElementById(dialog.id));
                                return true;
                              };    
  buttonPanel.appendChild(input);
  dialog.appendChild(buttonPanel);                               
  if (navigator.appName == "Microsoft Internet Explorer") document.getElementById(dialog.id+"_titleBar").style.width = (document.getElementById(dialog.id).offsetWidth - 4) + "px";    
}

function addP(windowObject, innerHtml, textAlign, border, convertLineBreaks) {
  var p = document.createElement("p");
  if (textAlign) p.style.textAlign = textAlign;
  if (border) p.style.border =  border;
  if (convertLineBreaks) innerHtml = innerHtml.replace("\n", "<br />");
  p.innerHTML = innerHtml;
  windowObject.appendChild(p);
  return true;
}

function addHtml(windowObject, innerHtml, convertLineBreaks) {  
  if (convertLineBreaks) innerHtml = innerHtml.replace("\n", "<br />");
  windowObject.innerHTML = windowObject.innerHTML + innerHtml;
  return true;
}  

function addDom(windowObject, domStructure) {    
  windowObject.appendChild(domStructure);
  return true;
}    

function addImg(windowObject, src, alt, width, height, align) {
  var img = document.createElement("img");
  img.src = src;    
  if (width) img.width = width;
  if (alt) img.alt = alt;
  if (align) img.align = align;
  if (height) img.height = height;   
  windowObject.appendChild(img);
  return true;
}

function addTextarea(windowObject, text, cols, rows, readonly, labelText) {
  var textarea = document.createElement("textarea");
  textarea.id = windowObject.parentNode.id + "_textarea";
  if (cols) textarea.cols = cols;
  if (rows) textarea.rows = rows;    
  if (text) textarea.value = text;
  if (labelText) {
    var label = document.createElement("label");
    var forAttribute = document.createAttribute("for");
    forAttribute.nodeValue = textarea.id;
    label.setAttributeNode(forAttribute);        
    label.innerHTML = "<b>"+labelText+"</b><br />"; 
    windowObject.appendChild(label);
  }
  if (readonly) {
    var readonlyAttribute = document.createAttribute("readonly");
    readonlyAttribute.nodeValue = "readonly";
    textarea.setAttributeNode(readonlyAttribute);            
    textarea.style.backgroundColor="#FFFF80";
  }
  windowObject.appendChild(textarea);
}

function addSelect(windowObject, options, size, multiple, labelText, selectedIndex) {
  var select = document.createElement("select");
  select.id = windowObject.parentNode.id + "_select";
  if (size) select.size = size;
  if (multiple) select.multiple = "multiple";
  if (labelText) {
    var label = document.createElement("label");
    var forAttribute = document.createAttribute("for");
    forAttribute.nodeValue = select.id;
    label.setAttributeNode(forAttribute);        
    label.innerHTML = "<b>"+labelText+"</b><br />"; 
    windowObject.appendChild(label);
  }
  for (var i=0; i<options.length; i++) {
    var option = document.createElement("option");
    if (i%2 == 0) option.style.backgroundColor = "#FFFF80";
    if (i == selectedIndex) option.selected = "selected";
    option.appendChild(document.createTextNode(options[i]));
    select.appendChild(option);
  }
  windowObject.appendChild(select);
}

function addRadioGroup(windowObject, options, radioGroupLabel, selectedIndex) {  
  var fieldset = document.createElement("fieldset");    
  fieldset.id = windowObject.parentNode.id + "_fieldset";
  var legend = document.createElement("legend");
  legend.innerHTML = "<b>"+radioGroupLabel+"</b>";
  fieldset.appendChild(legend);
  for (var i=0; i<options.length; i++) {
    try {
      var input = document.createElement("<input type='radio' name='"+windowObject.parentNode.id+"_radio' value='"+windowObject.parentNode.id + "_radio_" + i +"' />");
    }
    catch(e) {
      var input = document.createElement("input");        
      input.setAttribute('type', 'radio');
      input.setAttribute('name', windowObject.parentNode.id+'_radio');        
    }
    if ((selectedIndex) && (i == selectedIndex)) input.checked = "checked";
    input.setAttribute('value', windowObject.parentNode.id + "_radio_" + i);
    input.setAttribute('id', windowObject.parentNode.id + "_radio_" + i);

    var label = document.createElement("label");
    var forAttribute = document.createAttribute("for");
    forAttribute.nodeValue = input.id;
    label.setAttributeNode(forAttribute);              
    label.innerHTML = options[i]; 
    fieldset.appendChild(input);
    fieldset.appendChild(label);      
    fieldset.appendChild(document.createElement("br"));
  }
  windowObject.appendChild(fieldset);
}  

function statusBarText(statusBar, text) {
  statusBar.innerHTML = text;
  return false;
}

function closeWindow(closeButton) {
  closeButton.onclick();
  return false;
}

function maximizeWindow(maximizeButton) {
  maximizeButton.onclick();
  return false;
}

function setWidth(dialog, content, width) {
  dialog.style.width = width;
  content.style.width = (dialog.offsetWidth - 10) + "px";              
  return false;
}

function setHeight(dialog, content, height) {
  dialog.style.height = height;  
  content.style.height = (dialog.offsetHeight - 50) + "px";              
  return false;
}

function invisibleWindow(dialog) {
  dialog.style.display = "none";
  return false;
}

function visibleWindow(dialog) {
  dialog.style.display = "block";
  return false;
}

function minimizeWindow(minimizeButton) {
  minimizeButton.onclick();
  return false;
}

function onTopWindow(onTopButton) {
  onTopButton.onclick();
  return false;
}        

dragInit();

