//================================= CLASS CONSTRUCTOR ========================================
function Window(iwindowMgr,iid,iscrolling,imaximizable)
{
//MEMBER VARIABLES
 this.windowMgr = iwindowMgr;
//Note that the id is exactly the id from the window array in windowMgr ...
 this.id = iid;
 this.src = null;
 this.params = null;
 this.document = iwindowMgr.document;
 this.visible = false;
 this.modal = false;
 this.hideOnClose = false;
 this.scrolling = iscrolling;
 this.maximizable = imaximizable;
 this.maximized = false;
//The dimensions of non-maximized window ...
 this.left = 0;
 this.top = 0;
 this.width = 0;
 this.height = 0;
 this.eClose = null;
 this.eDestroy = null;
//The tag of the window ... it's a free param ...
 this.tag = null;
//The extended tag of the window ... another free param ...
 this.extTag = null;
 this.cWin = null;
 this.cTitle = null;
 this.cTitleText = null;
 this.cMaximizeBtn = null;
 this.cCloseBtn = null;
 this.cContent = null;

 this.initialize();
}

//====================================== STATIC ==============================================
//The window that is being dragged at this moment ...
Window.draggedWin = null;
Window.dragX = 0;
Window.dragY = 0;

Window.getWindowFromContent = function(icontentPage)
{
//This will get the window that has this content Page ...
 return icontentPage.frameElement.tag;
}

//====================================== MEMBERS =============================================
Window.prototype.setDefaults = function()
{
 this.windowMgr = null;
 this.id = null;
 this.src = null;
 this.params = null;
 this.document = null;
 this.modal = null;
 this.hideOnClose = null;
 this.scrolling = null;
 this.maximizable = null;
 this.maximized = null;
 this.left = null;
 this.top = null;
 this.width = null;
 this.height = null;
 this.eClose = null;
 this.eDestroy = null;
 this.tag = null;
 this.extTag = null;
 this.cWin = null;
 this.cTitle = null;
 this.cTitleText = null;
 this.cMaximizeBtn = null;
 this.cCloseBtn = null;
 this.cContent = null;
}

Window.prototype.initialize = function()
{
 var
  cTable = null,
  cRow = null,
  cCell = null,
  cTitleBox = null,
  cTitleTable = null;
  
//Creating the TitleText ...
 this.cTitleText = this.document.createElement("<DIV>");
 this.cTitleText.className = "labelWT";
 this.cTitleText.style.width = '100%';
 this.cTitleText.style.height = '100%';
 this.cTitleText.style.cursor = 'move';
 this.cTitleText.style.paddingTop = "4";
 this.cTitleText.tag = this;
 this.cTitleText.attachEvent("onmousedown",this.onMouseDown);
 this.cTitleText.attachEvent("onmousemove",this.onMouseMove);
 this.cTitleText.attachEvent("onmouseup",this.onMouseUp);
 if( this.maximizable )
	 this.cTitleText.attachEvent("ondblclick",this.onDblClick);
 this.cTitleText.attachEvent("onlosecapture",this.onLoseCapture);

//Creating the TitleBox ...
 cTitleBox = this.document.createElement("<DIV>");
 cTitleBox.className = "barWT";
 cTitleBox.style.overflow = 'hidden';
 cTitleBox.style.width = '100%';
 cTitleBox.style.height = '20';
 //cTitleBox.style.border = '1 solid black';
 cTitleBox.appendChild(this.cTitleText);

//Creating the maximize Button ... only if we have rights ...
 if (this.maximizable == true)
 {
  this.cMaximizeBtn = this.document.createElement("<LABEL>");
 // this.cMaximizeBtn.alt = this.windowMgr.rmgr.getResource('JSP_PLG_WMGR_0');
  this.cMaximizeBtn.style.width = 13;
  this.cMaximizeBtn.style.height = 13;
  this.cMaximizeBtn.style.paddingLeft = 2;
  this.cMaximizeBtn.style.paddingRight = 2;
  this.cMaximizeBtn.style.paddingBottom = 2;
  this.cMaximizeBtn.style.cursor = 'hand';
  this.cMaximizeBtn.className="quadratinoWT";
     //font-family:Wingdings
  this.cMaximizeBtn.innerHTML="o";
  this.cMaximizeBtn.tag = this;
  this.cMaximizeBtn.attachEvent("onclick",this.onMaximize);
  this.cMaximizeBtn.attachEvent("onmouseover",this.onMouseOverMaximizeBtnQ);
  this.cMaximizeBtn.attachEvent("onmouseout",this.onMouseOutMaximizeBtnQ);
 }

//Creating the close Button ...
 this.cCloseBtn = this.document.createElement("<LABEL>");
// this.cCloseBtn.alt = this.windowMgr.rmgr.getResource('JSP_PLG_WMGR_0');
 this.cCloseBtn.style.width = 13;
 this.cCloseBtn.style.height = 13;
 this.cCloseBtn.style.paddingLeft = 2;
 this.cCloseBtn.style.paddingRight = 2;
 this.cCloseBtn.style.paddingBottom = 2;
 this.cCloseBtn.style.cursor = 'hand';
 this.cCloseBtn.className="labelWT";
 this.cCloseBtn.innerText="x";
 this.cCloseBtn.tag = this;
 this.cCloseBtn.attachEvent("onclick",this.onClose);
 this.cCloseBtn.attachEvent("onmouseover",this.onMouseOverCloseBtn);
 this.cCloseBtn.attachEvent("onmouseout",this.onMouseOutCloseBtn);

//Creating the content ...
 this.cContent = this.document.createElement("<IFRAME src='/adiJed/blank.htm'>");
 this.cContent.width = '100%';
 this.cContent.height = '100%';
 this.cContent.frameBorder = '0';
 this.cContent.marginWidth = '0';
 this.cContent.marginHeight = '0';
 this.cContent.style.border = '1 solid black';
 if (this.scrolling)
  this.cContent.scrolling = 'auto';
 else
  this.cContent.scrolling = 'no';
 this.cContent.tag = this;

//Creating the outer table ...
 cTable = this.document.createElement("<TABLE>");
 cTable.width = '100%';
 cTable.height = '100%';
 cTable.cellSpacing = '0';

 cRow = cTable.insertRow();
 
 cCell = cRow.insertCell();
 cCell.style.padding = '0';
 cCell.width = '100%';
 cCell.height = '13';
 cCell.align = 'center';
 cCell.appendChild(cTitleBox);

 if (this.maximizable == true)
 {
  cCell = cRow.insertCell();
  cCell.style.padding = '0';
  cCell.className = "barWT";
  cCell.appendChild(this.cMaximizeBtn);
 }

 cCell = cRow.insertCell();
 cCell.style.padding = '0';
 cCell.className = "barWT";
 cCell.appendChild(this.cCloseBtn);
 
 cRow = cTable.insertRow();
 
 cCell = cRow.insertCell();
 cCell.style.padding = '0';
 if (this.maximizable == true)
  cCell.colSpan = '3';
 else
  cCell.colSpan = '2';
 cCell.appendChild(this.cContent);

//Creating the window ... initially it is hidden ...
 this.cWin = this.document.createElement("<DIV>");
 this.cWin.style.position = 'absolute';
 this.cWin.style.width = '300';
 this.cWin.style.height = '200';
 this.cWin.style.display = "none";
 this.cWin.appendChild(cTable);

//Creating the window into the document body ...
 this.document.body.appendChild(this.cWin);

//Setting focus on content ...
 //this.cContent.focus();
//Setting the id of the window to update it's z-Index ...
 this.setId(this.id);
}

Window.prototype.finalize = function()
{
 this.cTitleText.detachEvent("onmousedown",this.onMouseDown);
 this.cTitleText.detachEvent("onmousemove",this.onMouseMove);
 this.cTitleText.detachEvent("onmouseup",this.onMouseUp);
 this.cTitleText.detachEvent("ondblclick",this.onDblClick);
 this.cTitleText.detachEvent("onlosecapture",this.onLoseCapture);
 this.cTitleText.tag = null;
 if (this.maximizable == true)
 {
  this.cMaximizeBtn.detachEvent("onclick",this.onMaximize);
  this.cMaximizeBtn.detachEvent("onmouseover",this.onMouseOverMaximizeBtn);
  this.cMaximizeBtn.detachEvent("onmouseout",this.onMouseOutMaximizeBtn);
  this.cMaximizeBtn.tag = null;
 }
 this.cCloseBtn.detachEvent("onclick",this.onClose);
 this.cCloseBtn.detachEvent("onmouseover",this.onMouseOverCloseBtn);
 this.cCloseBtn.detachEvent("onmouseout",this.onMouseOutCloseBtn);
 this.cCloseBtn.tag = null;
 this.cContent.tag = null;
//Deleting this window ...
 this.cWin.removeNode(true);
 this.setDefaults();
}

Window.prototype.bringToFront = function()
{
//Calling bringtofront from the window manager ...
 this.windowMgr.onBringWindowToFront(this.id);
}

Window.prototype.show = function(ishown)
{
//Showing/Hiding the window...
 if (this.visible == ishown)
  return;
 this.visible = ishown;
 this.cWin.style.display = (ishown?'':'none');
 if (ishown)
  this.bringToFront();
 else
  this.windowMgr.onHideWindow(this.id);
}

Window.prototype.clearContent = function()
{
 this.cContent.src = "/adiJed/blank.htm";
}

Window.prototype.close = function()
{
//Calling the close event ...
 if (this.eClose != null)
  this.eClose(this);
 if (this.hideOnClose)
  this.show(false);
 else
  this.destroy();
}

Window.prototype.destroy = function()
{
//Hiding the window ...
 this.show(false);
//clear the content of page
 this.clearContent();
//Calling close from the window manager ...
 this.windowMgr.onCloseWindow(this.id);
//Calling the destroy event
 if (this.eDestroy != null)
  this.eDestroy(this);
 this.finalize();
}

Window.prototype.setId = function(ivalue)
{
//Setting id to the window ...
 this.id = ivalue;
//Setting window's Z-index based on it's id(which is the exact id in z-index order - ex. 0 - lowest)...
 this.cWin.style.zIndex = (ivalue+1)*2;
}

Window.prototype.getId = function()
{
 return this.id;
}

Window.prototype.getVisible = function()
{
 return this.visible;
}

Window.prototype.setModal = function(ivalue)
{
 this.modal = ivalue;
}

Window.prototype.getModal = function()
{
 return this.modal;
}

Window.prototype.setHideOnClose = function(ivalue)
{
 if (this.hideOnClose == ivalue)
  return;
 this.hideOnClose = ivalue;
}

Window.prototype.getHideOnClose = function()
{
 return this.hideOnClose;
}

Window.prototype.setTitle = function(ivalue)
{
 this.cTitleText.innerHTML = ivalue;
}

Window.prototype.reloadContent = function()
{
 if (this.src != null)
  DATA_loadIFrame(this.cContent,this.src,this.params);
}

Window.prototype.loadContent = function(itargetsrc,iparams)
{
 this.src = itargetsrc;
 this.params = iparams;
 DATA_loadIFrame(this.cContent,itargetsrc,iparams);
}

Window.prototype.setCloseEvent = function(ieClose)
{
 this.eClose = ieClose;
}

Window.prototype.setDestroyEvent = function(ieDestroy)
{
 this.eDestroy = ieDestroy;
}

Window.prototype.setTag = function(itag)
{
 this.tag = itag;
}

Window.prototype.getTag = function()
{
 return this.tag;
}

Window.prototype.setExtTag = function(iextTag)
{
 this.extTag = iextTag;
}

Window.prototype.getExtTag = function()
{
 return this.extTag;
}

Window.prototype.getBounds = function()
{
 return {l:this.cWin.style.pixelLeft,
         t:this.cWin.style.pixelTop,
         w:this.cWin.style.pixelWidth,
         h:this.cWin.style.pixelHeight};
}

Window.prototype.getTitleBar = function()
{
 return this.cTitleBox;
}

Window.prototype.moveTo = function(ix,iy)
{
 var
  b = null;
  mw = null;
  mh = null;

//Initializing ...
 b = this.getBounds();
 mw = this.windowMgr.getClientWidth();
 mh = this.windowMgr.getClientHeight();
//Checking if the window hasn't got out of screen ...
 if (ix < 0)
  ix = 0;
 else
 if (ix + b.w > mw)
  ix = mw - b.w;
 if (iy < 0)
  iy = 0;
 else
 if (iy + b.h > mh)
  iy = mh - b.h;
 this.cWin.style.pixelLeft = ix;
 this.cWin.style.pixelTop = iy;
 if (this.maximized == false)
 {
  this.left = ix;
  this.top = iy;
 }
}

Window.prototype.resizeTo = function(iw,ih)
{
//Only resizing if not maximized ...
 this.cWin.style.pixelWidth = iw;
 this.cWin.style.pixelHeight = ih;
 if (this.maximized == false)
 {
  this.width = iw;
  this.height = ih;
 }
}

Window.prototype.maximize = function()
{
//If we're maximized then we'll make the normal size, else making maximized ...
 if (this.maximized == true)
 {
  this.maximized = false;
  this.resizeTo(this.width,this.height);
  this.moveTo(this.left,this.top);
 }
 else
 {
  this.maximized = true;
  this.resizeTo(this.windowMgr.getClientWidth(),this.windowMgr.getClientHeight());
  this.moveTo(0,0);
 }
}

Window.prototype.center = function()
{
 var
  b = null,cw=0,ch=0;

//Getting current left,top,width and height ...
 b = this.getBounds();
 cw = this.windowMgr.getClientWidth();
 ch = this.windowMgr.getClientHeight();
//Centering the window into the body of document ...
 b.l = Math.round((cw - b.w)/2);
 b.t = Math.round((ch - b.h)/2);
 this.moveTo(b.l,b.t);
}

Window.prototype.onMouseDown = function()
{
 var
  thisobj = event.srcElement.tag,
  b = null;
 
 b = thisobj.getBounds();
 Window.dragX = event.x - b.l;
 Window.dragY = event.y - b.t;
 Window.draggedWin = thisobj;
//Bringing this window to top ...
 Window.draggedWin.bringToFront();
 thisobj.cTitleText.setCapture();
}

Window.prototype.onMouseMove = function()
{
 var
  thisobj = Window.draggedWin;
 
 if (thisobj == null)
  return;
 thisobj.moveTo(event.x - Window.dragX,event.y - Window.dragY);
}

Window.prototype.onMouseUp = function()
{
 var
  thisobj = Window.draggedWin;

 if (thisobj == null)
  return;
 thisobj.cTitleText.releaseCapture();
 Window.draggedWin = null;
}

Window.prototype.onDblClick = function()
{
 var
  thisobj = event.srcElement.tag;

 thisobj.onMaximize();
}

Window.prototype.onLoseCapture = function()
{
 var
  thisobj = Window.draggedWin;
 
 thisobj.cTitleText.setCapture();
}

Window.prototype.onMaximize = function()
{
 var
  thisobj = event.srcElement.tag;

//Calling the window close method ...
 thisobj.maximize();
}

Window.prototype.onMouseOverMaximizeBtn = function()
{
 var
  thisobj = event.srcElement.tag;

//Calling the window close method ...
 thisobj.cMaximizeBtn.className="flatButtonOver";
}
Window.prototype.onMouseOverMaximizeBtnQ = function()
{
 var
  thisobj = event.srcElement.tag;

//Calling the window close method ...
 thisobj.cMaximizeBtn.className="quadratinoOver";
}

Window.prototype.onMouseOutMaximizeBtn = function()
{
 var
  thisobj = event.srcElement.tag;

 thisobj.cMaximizeBtn.className="labelWT";
}
Window.prototype.onMouseOutMaximizeBtnQ = function()
{
 var
  thisobj = event.srcElement.tag;

 thisobj.cMaximizeBtn.className="quadratinoWT";
}

Window.prototype.onClose = function()
{
 var
  thisobj = event.srcElement.tag;

//Calling the window close method ...
 thisobj.close();
}

Window.prototype.onMouseOverCloseBtn = function()
{
 var
  thisobj = event.srcElement.tag;

//Calling the window close method ...
 thisobj.cCloseBtn.className="flatButtonOver";
}

Window.prototype.onMouseOutCloseBtn = function()
{
 var
  thisobj = event.srcElement.tag;

 thisobj.cCloseBtn.className="labelWT";
}
