
var dialogRadioId = 1;

function Dialog(top, left, height, width, title, buttons, actions, modal, backdropOpacity, container) {

  if (browser.isIE && browser.version < 7) {
    modal = false;  // IE6 doesn't support the opacity attribute
  }

  if (top==null) {
    top = (document.body.offsetHeight - height) / 2;
  }
  if (left==null) {
    left = (document.body.offsetWidth - width) / 2;
  }

  var mainDiv = document.createElement("div");
  var titleDiv = document.createElement("div");
  var closeButtonDiv = document.createElement("div");
  var closeButtonImgDiv = document.createElement("div");
  var bodyDiv = document.createElement("div");
  var backdropDiv = null;
  
  this.onEscape = null;
  this.onEnter = null;
  this.div = bodyDiv;

  var me = this;    // Reference to object "me" allows privileged methods to access the public members.
  
  
  // create backdrop div
  if (modal) {
    backdropDiv = document.createElement("div");
    backdropDiv.style.position = "absolute";
    backdropDiv.style.left = "0px";
    backdropDiv.style.top = "0px";
    backdropDiv.style.backgroundColor = "#999999";
    backdropDiv.style.zIndex = styles.zDialog;
    if (backdropOpacity == null) backdropOpacity = 50;
    backdropDiv.style.opacity = backdropOpacity / 100;
    backdropDiv.style.filter = "alpha(opacity=" + backdropOpacity + ")";
    resizeBackdrop();
    document.body.appendChild(backdropDiv);
    utils.addEvent(window, "resize", function() {
      setTimeout(resizeBackdrop, 1);
    } ); 
  }
  
  // create main div
  mainDiv.style.position = "absolute";
  mainDiv.style.top = top + 'px';
  mainDiv.style.left = left + 'px';
  mainDiv.style.height = height + 'px';
  mainDiv.style.width = width + 'px';
  mainDiv.style.border = styles.dialogBorder;
  mainDiv.style.zIndex = styles.zDialog;
  mainDiv.style.filter = "progid:DXImageTransform.Microsoft.Shadow(color=gray, direction=135, strength=10)";  
  mainDiv.onkeydown = function(e) { 
   	if(!e) e = window.event;
    var key=(typeof event!='undefined')?window.event.keyCode:e.keyCode;
    if (key==13) {  // Enter
      if (me.onEnter!=null) {
        me.onEnter();
        return false;
      }
    }    
    if (key==27) {  // Escape
      if (me.onEscape!=null) {
        me.onEscape();
        return;
      }
    }
  }
    
  // create title  
  var adjust = 10;
  titleDiv.style.position = "absolute";
  titleDiv.style.top = "0px";  
  titleDiv.style.left = "0px";  
  titleDiv.style.width = (width-adjust) + "px";
  titleDiv.style.padding = "5px";
  titleDiv.style.cursor = "move";
  titleDiv.style.height = "24px";
  titleDiv.style.font = styles.dialogTitleFont;
  titleDiv.style.fontWeight = "bold";
  // titleDiv.style.backgroundColor = styles.dialogTitleBackground;
  // titleDiv.style.color = styles.dialogTitleColor;
  //titleDiv.className = "DHTMLSuite_paneHeader";
  titleDiv.style.backgroundImage = "url(" + styles.dialogTitleBackImgPath + ")";
  titleDiv.style.backgroundRepeat = "repeat-x";
  titleDiv.style.textAlign = "left";
  titleDiv.innerHTML = title;
  titleDiv.onmousedown = dragObj.dragStart
  utils.addEvent(titleDiv, "mousedown", dragWindow);
  function dragWindow(event) { 
    dragObj.type = "dialog";
    dragObj.dragStart(event, mainDiv, true);
  }
  
  // Create close button
  closeButtonDiv.style.position = "absolute";
  closeButtonDiv.style.top = "3px";
  closeButtonDiv.style.right = "3px";
  closeButtonDiv.style.width = "15px";  
  closeButtonDiv.style.height = "15px";  
  closeButtonDiv.style.cursor = "pointer";
  closeButtonDiv.style.border = "1px solid transparent";
  if (browser.isIE && browser.version < 7) closeButtonDiv.style.border = "1px solid #CBE0FB";  // IE6 doesn't support transparent borders 
  closeButtonDiv.onmouseover = function() {
    closeButtonDiv.style.backgroundColor = "#FFFFFF";
    closeButtonDiv.style.border = "1px solid #9CBDFF";
    
  }
  closeButtonDiv.onmouseout = function() {
    closeButtonDiv.style.backgroundColor = "";
    closeButtonDiv.style.border = "1px solid transparent";
    if (browser.isIE && browser.version < 7) closeButtonDiv.style.border = "1px solid #CBE0FB";  // IE6 doesn't support transparent borders    
  }
  closeButtonDiv.onclick = function() {
    me.close();
  }  

  closeButtonImgDiv.style.backgroundImage = "url(" + styles.dialogCloseImgPath + ")";  
  closeButtonImgDiv.style.backgroundRepeat = "no-repeat";
  closeButtonImgDiv.style.position = "absolute";
  closeButtonImgDiv.style.top = "4px";
  closeButtonImgDiv.style.left = "4px";
  closeButtonImgDiv.style.width = "7px";
  closeButtonImgDiv.style.height = "7px";
  
  
  closeButtonDiv.appendChild(closeButtonImgDiv);
  titleDiv.appendChild(closeButtonDiv);
  
  mainDiv.appendChild(titleDiv);
  
  // create dialog body
  bodyDiv.style.position = "absolute";
  bodyDiv.style.top = "23px";
  bodyDiv.style.left = "0px";
  var bodyDivHeight = height - 23;
  // if (browser.isIE) bodyDivHeight = bodyDivHeight - 2;
  bodyDiv.style.height = bodyDivHeight + 'px';
  bodyDiv.style.width = width + 'px';
  bodyDiv.style.backgroundColor = styles.dialogBodyBackground;
  mainDiv.appendChild(bodyDiv);
  
  // create buttons
  if (buttons!=null) {
    var curWidth = width - 85;
    for (var i=buttons.length-1; i>=0; i=i-1) {
      var cmd = document.createElement("input");
      cmd.style.position = "absolute";
      cmd.style.top = (height - 55) + "px";
      cmd.style.left = curWidth + "px";
      cmd.style.width = "70px";
      cmd.style.fontWeight = "bold";
      cmd.style.font = styles.dialogButtonFont;
      cmd.style.color = styles.dialogButtonColor;
      cmd.style.align = "left";
      cmd.type = "button";
      cmd.value = buttons[i];
      cmd.tabIndex = i+2;
      if (actions!=null && actions[i]!=null) {
        cmd.onclick = actions[i];
      }
      bodyDiv.appendChild(cmd);      
      curWidth = curWidth - 80;
    }
  }  
  
  // show dialog
  if (container==null) container = document.body;
  container.appendChild(mainDiv);
  
  utils.fadeIn(mainDiv, null, null, null, function() { 
    mainDiv.style.filter = "progid:DXImageTransform.Microsoft.Shadow(color=gray, direction=135, strength=10)";
  } );
  
  
  this.close = function() {
    // mainDiv.parentNode.removeChild(mainDiv);
    utils.fadeOut(mainDiv, null, null, null, function() {
      mainDiv.parentNode.removeChild(mainDiv);
    });
    if (modal) {
      utils.removeEvent(window, "resize", resizeBackdrop);     
      backdropDiv.parentNode.removeChild(backdropDiv);
    }
  }
  
  this.addLabel = function(top, left, text) {
    var div = document.createElement("div");
    div.style.position = "absolute";
    div.style.top = top + "px";
    div.style.left = left + "px";
    div.style.font = styles.dialogTextFont;
    div.innerHTML = text;
    bodyDiv.appendChild(div);
    return div;   
  }
  
  this.addInputBox = function(top, left, size, value) {
    var box = document.createElement("input");
    box.style.position = "absolute";
    box.style.top = top + "px";
    box.style.left = left + "px";
    box.style.font = styles.dialogTextFont;
    box.style.color = styles.dialogTextBoxColor;
    box.style.border = styles.dialogTextBoxBorder;
    box.tabIndex = 1;
    if (size!=null) box.size = size;
    if (value!=null) box.value = value;
    utils.addEvent(box, 'focus', function(e) {    	
      box.style.backgroundColor = styles.dialogTextBoxFocusBackground;      
    });
    utils.addEvent(box, 'blur', function(e) {
      box.style.backgroundColor = '';
    });
    bodyDiv.appendChild(box);
    return box;
  }
  
  this.addTextArea = function(top, left, rows, cols, value) {
    var box = document.createElement("textarea");
    box.style.position = "absolute";
    box.style.top = top + "px";
    box.style.left = left + "px";
    box.style.font = styles.dialogTextFont;
    box.style.color = styles.dialogTextBoxColor;
    // box.style.border = styles.dialogTextBoxBorder;
    box.tabIndex = 1;
    if (rows!=null) box.rows = rows;
    if (cols!=null) box.cols = cols;
    if (value!=null) box.value = value;
    utils.addEvent(box, 'focus', function(e) {    	
      box.style.backgroundColor = styles.dialogTextBoxFocusBackground;      
    });
    utils.addEvent(box, 'blur', function(e) {
      box.style.backgroundColor = '';
    });
    bodyDiv.appendChild(box);
    return box;
  }

  this.addRadioButton = function(top, left, labelText, checked, groupName) {
    if (groupName==null) groupName = "radioButton";
    var box;
    var checkedAttr = "";
    if (checked) {
      checkedAttr = " checked";
    }
    if (browser.isIE) {
      box = document.createElement("<input type='radio' name='" + groupName + "' value='" + labelText + "'" + checkedAttr + ">");
    }
    else {
      box = document.createElement("input");
      box.checked = checked;
    }
    box.style.position = "absolute";
    box.style.top = top + "px";
    box.style.left = left + "px";
    box.tabIndex = 1;
    box.type = "radio";
    box.name = groupName;
    box.value = labelText;
    dialogRadioId++;
    box.id = dialogRadioId;
    var label = document.createElement("label");
    label.style.position = "absolute";
    label.style.top = (top + 4) + "px";
    label.style.left = (left + 23) + "px";
    label.tabIndex = 1;    
    label.style.font = styles.dialogTextFont;
    label.style.cursor = "pointer";
    label.htmlFor = dialogRadioId;
    label.appendChild(document.createTextNode(labelText));
    bodyDiv.appendChild(box);
    bodyDiv.appendChild(label);
    return box;
  }

  this.addSelectBox = function(top, left, options, values, selectedValue) {
    var box = document.createElement("select");
    box.style.position = "absolute";
    box.style.top = top + "px";
    box.style.left = left + "px";
    box.style.font = styles.dialogTextFont;
    box.style.color = styles.dialogTextBoxColor;
    box.tabIndex = 1;
    for (var i=0; i<options.length; i++) {
      var opt = new Option(options[i], values[i]);
      if (selectedValue!=null && values[i]==selectedValue) {
        opt.selected = true;
      }
      box.options[i] = opt;
    }
    // the following effect is commented out because of IE7 bug, which makes you click on the select box twice to focus on it
//    utils.addEvent(box, 'focus', function(e) {    	
//      box.style.backgroundColor = styles.dialogTextBoxFocusBackground;      
//    });
//    utils.addEvent(box, 'blur', function(e) {
//      box.style.backgroundColor = '';
//    });
    bodyDiv.appendChild(box);
    return box;
  }

  
  // Private functions
  function resizeBackdrop() {
    var pageX = (document.documentElement && document.documentElement.scrollWidth) ? document.documentElement.scrollWidth : (document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth; 
    var pageY = (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;    
    
    backdropDiv.style.width = pageX + "px";
    backdropDiv.style.height = pageY + "px";
  }
  
}

