/**
 * jt_DialogBox.js - DHTML modal dialog box
 *
 */
/************ REQUIRES: 'jt_BodyZ' and 'jt_Veil' from 'jt_.js' ************/

jt_DialogBox = function(dialogId, isModal, hideCloseIcon, hideMaximizeIcon, imagePath) {
  // CONSTRUCTOR for 'jt_DialogBox' object
  if (arguments.length == 0) return;
  this.imagePath = imagePath;
  this.dialogId = dialogId;
  this.isModal = isModal;
  this.hideCloseIcon = hideCloseIcon;
  this.hideMaximizeIcon = hideMaximizeIcon;
  this.container = document.getElementById(this.dialogId);
  this.container.dialogBox = this;
  this.contentArea = document.getElementById(this.dialogId+'_content');

}


/************ BEGIN: Public Methods ************/

jt_DialogBox.prototype.paintDialogBox = function() {

  var generalTable = document.createElement('table');
  generalTable.setAttribute('cellSpacing', '0');
  generalTable.setAttribute('cellPadding', '0');
  generalTable.setAttribute('border', '0');
  generalTable.setAttribute('height', '100%');

  var tBodyG = document.createElement('tbody');
  var rowG = document.createElement('tr');
  var titleArea = document.createElement('td');
  titleArea.setAttribute('id', this.dialogId+'_titleArea');
  titleArea.setAttribute('style', 'height: 20px;');
  rowG.appendChild(titleArea);
  tBodyG.appendChild(rowG);

  this.container.titleRow = rowG;

  rowG = document.createElement('tr');
  var cellG = document.createElement('td');
  cellG.className = "MainPanel";
  cellG.appendChild(this.contentArea);

  var resizeIcon = document.createElement('img');
  resizeIcon.src = this.imagePath + "resize.png";
  resizeIcon.style["cursor"] = "nw-resize";
  //resizeIcon.setAttribute('style', 'cursor: nw-resize;');
  ResizeDialogBox.init(resizeIcon, this.container);


  var div = document.createElement('div');
  div.setAttribute('align', 'right');
  div.className = "ResizeIcon";
  div.appendChild(resizeIcon);
  cellG.appendChild(div);
  rowG.appendChild(cellG);
  tBodyG.appendChild(rowG);

  generalTable.appendChild(tBodyG);
  this.container.appendChild(generalTable);
  this.container.GeneralTable = generalTable;

  //*********** BEGIN Title TABLE ***********
  var titleTable = document.createElement('table');
  titleTable.setAttribute('cellSpacing', '0');
  titleTable.setAttribute('cellPadding', '0');
  titleTable.setAttribute('border', '0');
  titleTable.setAttribute('width', '100%');

  var tBodyT = document.createElement('tbody');
  var rowT = document.createElement('tr');
  var cellT = document.createElement('td');
  cellT.setAttribute('width', '5');
  cellT.className = "tbLeft";
  rowT.appendChild(cellT);

  this.titleCell = document.createElement('td');
  this.titleCell.setAttribute('align', 'left');
  this.titleCell.className = "Title";

  var titleDiv = document.getElementById(this.dialogId+'_title');
  this.titleCell.appendChild(titleDiv);
  rowT.appendChild(this.titleCell);

  cellT = document.createElement('td');
  cellT.className = "tbRight";
  cellT.setAttribute('align', 'left');
  cellT.setAttribute('width', '5');
  cellT.setAttribute('style', 'padding-right: 1px;');

  var hCIcon = this.hideCloseIcon || false;
  var hMIcon = this.hideMaximizeIcon || false;
  var nrButtons = 0;

  if(!hCIcon) {
    var divL = document.createElement('SPAN');
    divL.setAttribute('class','dialog_close_btn');
    divL.dialogBox = this;

    divL.onclick = jt_DialogBox.closeBox;
    titleDiv.appendChild(divL);
  }

  if (!hMIcon) {
    var divL = document.createElement('SPAN');
    divL.setAttribute('class','dialog_maximize_btn');
    divL.dialogBox = this;
    divL.onclick = jt_DialogBox.maximizeBox;
    titleDiv.appendChild(divL);
  }

  rowT.appendChild(cellT);

  tBodyT.appendChild(rowT);
  titleTable.appendChild(tBodyT);
  //*********** END Title TABLE ***********

  titleArea.appendChild(titleTable);

  Drag.init(this.titleCell, this.container, 0, null, 0);

}



jt_DialogBox.prototype.show = function() {

  this.container.style.display = "block";
  this.container.style.position = "absolute";
  jt_BodyZ.toTop(this.container);

  if (this.isModal) {
    jt_Veil.show(true, this.container);
    jt_Veil.fix();
  }
  jt_divOnScrn(this.container);
  }

jt_DialogBox.prototype.hide = function(ok) {
  this.container.style.display = "none";
  if (this.isModal) jt_Veil.show(false, this.container);
  this.contentArea.jtClosed = true;
  if (ok) {
    if (this.callOK)
      if (this.returnData) this.callOK(this.returnData);
      else this.callOK();
    }
  else if (this.callCancel) this.callCancel();
  }

jt_DialogBox.prototype.moveTo = function(x, y) {
  var width;
  var height;
  var offset;
  if (window.innerWidth) {
    width = window.innerWidth;
  } else if (document.documentElement) {
    width = document.documentElement.clientWidth;
    width = width > 0 ? width : document.body.clientWidth;
  }
  if (window.innerHeight) {
    height = window.innerHeight;
  } else if (document.documentElement) {
    height = document.documentElement.clientHeight;
    height = height > 0 ? height : document.body.clientHeight;
  }

  if(document.all !== void 0){ // IE4, IE5, IE6
     if (document.documentElement)
      offset = document.documentElement.scrollTop;
     else
      offset = document.body.scrollTop;
  } else if(document.layers !== void 0 || (navigator.userAgent.indexOf("Opera") != -1 || window.opera !== void 0)){ // NN4, Opera6
  offset = window.pageYOffset;
  } else if(navigator.userAgent.indexOf("Gecko") != -1){ // NS6, Mozilla
    offset = window.scrollY;
  }	else{
    offset = 0;
  }

  if (x == -1) x = Math.round((width - this.container.offsetWidth) / 2);
  if (y == -1) y = Math.round((height - this.container.scrollHeight) / 2) + offset;

  this.container.style.left = x + "px";
  this.container.style.top = y + "px";
  }

jt_DialogBox.prototype.maximize = function() {

  this.container.lastLeft   = this.container.style.left;
  this.container.lastTop    = this.container.style.top;
  this.container.lastWidth  = this.container.clientWidth;
  this.container.lastHeight = this.container.clientHeight;

  this.container.style.left   = document.body.clientLeft + "px";
  this.container.style.top    = document.body.clientTop + "px";
  this.container.style.width  = "100%";
  this.container.style.height = "100%";
}

jt_DialogBox.prototype.unmaximize = function() {

  this.container.style.left   = this.container.lastLeft;
  this.container.style.top    = this.container.lastTop;
  this.container.style.width  = this.container.lastWidth + "px";
  this.container.style.height = this.container.lastHeight + "px";
  }

jt_DialogBox.prototype.setTitle = function(title) {
  this.titleCell.innerHTML = title;
  }

jt_DialogBox.prototype.setUrl = function(url, height) {
  // creates one IFRAME above 'setContent()' area, updates 'url'
  if (!this._jtDialogBIF) {
    this._jtDialogBIF = document.createElement('IFRAME');
    this._jtDialogBIF.setAttribute('frameBorder', 'no');
    this._jtDialogBIF.style.width = "100%";
    if (height) this._jtDialogBIF.style.height = height;
    this.contentArea.parentNode.insertBefore(this._jtDialogBIF, this.contentArea);
    }
  this._jtDialogBIF.src = url;
  }

jt_DialogBox.prototype.getUrl = function() {
  if (this._jtDialogBIF) {
    jt_TraceObj.show(this._jtDialogBIF);
    var url = this._jtDialogBIF.src;
    if (this._jtDialogBIF.contentWindow) {
      try {url = this._jtDialogBIF.contentWindow.location.href;}
      catch(e) {}
      }
    return url;
    }
  }

jt_DialogBox.parseWidth = function(width) {
  var numberWidth = "";
  for (var i=0;i<width.length;i++) {
    var c = width.charAt(i);
    if (c >= '0' && c <= '9') {
      numberWidth = numberWidth + c;
    }
  }
  if (width.indexOf('%') > 0) {
    var w = parseInt(numberWidth);
    return (parseInt(document.body.clientWidth*w*0.01))+"";
  }
  return numberWidth;
}

jt_DialogBox.prototype.setWidth = function(width) {
  var pxWidth = jt_DialogBox.parseWidth(width);
  if(this.container.offsetWidth < pxWidth) {
     this.container.style.width = pxWidth + "px";
    }
    else {
      this.container.style.width = this.container.offsetWidth + "px";
    }
}

jt_DialogBox.parseHeight = function(height) {
  var numberHeight = "";
  for (var i=0;i<height.length;i++) {
    var c = height.charAt(i);
    if (c >= '0' && c <= '9') {
      numberHeight = numberHeight + c;
    }
  }
  if (height.indexOf('%') > 0) {
    var w = parseInt(numberHeight);
    return (parseInt(document.body.clientHeight*w*0.01))+"";
  }
  return numberHeight;
}

jt_DialogBox.prototype.setHeight = function(height) {
  var pxHeight = jt_DialogBox.parseHeight(height);
  if(this.container.offsetHeight < pxHeight) {
     this.container.style.height = pxHeight + "px";
    }
    else {
      this.container.style.height = this.container.offsetHeight + "px";
    }
}

jt_DialogBox.prototype.fixTitleHeight = function() {

  this.container.GeneralTable.setAttribute('width', '100%');
  this.container.titleRow.style.height = this.container.titleRow.clientHeight + "px";
}

jt_DialogBox.prototype.setCallOK = function(callOK) {
  // set by application as needed
  this.callOK = callOK;
  }

jt_DialogBox.prototype.setCallCancel = function(callCancel) {
  // set by application as needed
  this.callCancel = callCancel;
  }

jt_DialogBox.prototype.getContentNode = function() {
  // expose 'contentArea' DOM node for direct manipulation
  return this.contentArea;
  }
/************ END: Public Methods ************/


/************ BEGIN: Private Methods ************/
jt_DialogBox.className = "jtDialogBox"; // CSS className

jt_DialogBox.closeBox = function(e) {
  if (!e) e = window.event;
  var node = e.target ? e.target : e.srcElement;
  if (node.dialogBox) {
    node.dialogBox.hide();
  }
  return false;
}

jt_DialogBox.maximizeBox = function(e) {
  if (!e) e = window.event;
  var node = e.target ? e.target : e.srcElement;
  if (node.dialogBox) {
    node.dialogBox.maximize();
  }
  node.onclick = jt_DialogBox.unmaximizeBox;
//	node.parentNode.blur();
//	node.src = this.imagePath + "unmaximize_blue.png";
  return false;
}

jt_DialogBox.unmaximizeBox = function(e) {
  if (!e) e = window.event;
  var node = e.target ? e.target : e.srcElement;
  if (node.dialogBox) {
    node.dialogBox.unmaximize();
  }
  node.onclick = jt_DialogBox.maximizeBox;
  //node.parentNode.blur();
  //node.src = this.imagePath + "maximize_blue.png";
  return false;
}


/***************** Resize jt_DialogBox   ******************/
var ResizeDialogBox = {

    obj : null,

    init : function(o, oRoot) {
      o.onmousedown	= ResizeDialogBox.start;
      o.root = oRoot && oRoot != null ? oRoot : o ;
    },

    start : function(e) {
      var o = ResizeDialogBox.obj = this;
      e = ResizeDialogBox.fixE(e);
      var y = parseInt(o.root.style.top);
      var x = parseInt(o.root.style.left);

      var cWidth = o.root.offsetWidth;
      var cHeight = o.root.offsetHeight;

      var eWidth = e.clientX - x;
      var eHeight = e.clientY - y;

      o.wDif = cWidth - eWidth;
      o.hDif = cHeight - eHeight;

      document.onmousemove	= ResizeDialogBox.drag;
      document.onmouseup		= ResizeDialogBox.end;

      return false;
    },

    drag : function(e) {
      e = ResizeDialogBox.fixE(e);
      var o = ResizeDialogBox.obj;

      var ey	= e.clientY;
      var ex	= e.clientX;

      var cLeft = parseInt(o.root.style.left);
      var cTop = parseInt(o.root.style.top);

      var eWidth = ex - cLeft;
      var eHeight = ey - cTop;

      o.root.style.width = eWidth + o.wDif + "px";
      o.root.style.height = eHeight + o.hDif + "px";

      var minimWidth = o.root.GeneralTable.clientWidth;
      if(minimWidth > parseInt(o.root.style.width)) {
        o.root.style.width = minimWidth + "px";
      }
      var minimHeight = o.root.GeneralTable.clientHeight;
      if(minimHeight > parseInt(o.root.style.height)) {
        o.root.style.height = minimHeight + "px";
      }

      return false;
    },

    end : function() {
      document.onmousemove = null;
      document.onmouseup   = null;
      ResizeDialogBox.obj = null;
    },

    fixE : function(e) {
      if (typeof e == 'undefined') e = window.event;
      if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
      if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
      return e;
    }
  };




