//================================= CLASS CONSTRUCTOR ========================================
function WindowMgr(idocument,irmgr)
{
//MEMBER VARIABLES
//The resource manager ...
 this.rmgr = irmgr;
//The parent document ...
 this.document = idocument;
//The window list ... it is always in the order of the z-order ...(for destroyed windows contains null)
 this.winList = new Array();
//The filter for modal displaying of windows ...
 this.cBlockFilter = null;
 this.cBFShown = false;

//Initializing ...
 this.initialize();
}

//====================================== STATIC ==============================================

//====================================== MEMBERS =============================================
WindowMgr.prototype.initialize = function()
{
//Creating the block filter ...
 this.cBlockFilter = this.document.createElement("<IFRAME src='/adiJed/blank.htm'>");
 this.cBlockFilter.style.position = 'absolute';
 this.cBlockFilter.style.left = 0;
 this.cBlockFilter.style.top = 0;
 this.cBlockFilter.style.width = 0;
 this.cBlockFilter.style.height = 0;
 this.cBlockFilter.style.background = '#3C3C3C';
 this.cBlockFilter.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=70,style=0)';
 this.cBlockFilter.style.display = 'none';
 this.cBlockFilter.style.zIndex = 1;
 this.document.body.appendChild(this.cBlockFilter);
}

WindowMgr.prototype.getClientWidth = function()
{
 return this.document.body.clientWidth;
}

WindowMgr.prototype.getClientHeight = function()
{
 return this.document.body.clientHeight;
}

WindowMgr.prototype.resizeBlockFilter = function()
{
 this.cBlockFilter.style.width = this.getClientWidth();
 this.cBlockFilter.style.height = this.getClientHeight();
}

WindowMgr.prototype.showBlockFilter = function(iShown)
{
 if (this.cBFShown == iShown)
  return;
//Resizing the filter ...
 this.resizeBlockFilter();
 this.cBlockFilter.style.display = (iShown == true)?'':'none';
 this.cBFShown = iShown;
}

WindowMgr.prototype.putBlockFilter = function(iWinId)
{
//Setting filter's z-order ...
 this.cBlockFilter.style.zIndex = (iWinId+1)*2-1;
//Showing the filter ...
 this.showBlockFilter(true);
}

WindowMgr.prototype.autoSetBlockFilter = function()
{
 var
  cWin = null;
  
//We'll find the first window(from top to bottom) that is modal ...
 cWin = this.getTopWindow(true);
//Activating block filter beneath it ...
 if (cWin != null)
  this.putBlockFilter(cWin.getId());
 else
  this.showBlockFilter(false);
}

WindowMgr.prototype.createWindow = function(iModal,iscrolling,imaximizable)
{
 var
  nWin = null;
  nId = this.winList.length;

//Searching for the first free element in the window list ...
 for (var i=0;i<this.winList.length;i++)
 if (this.winList[i] == null)
 {
  nId = i;
  break;
 }
//Creating a new Window ...
 if (arguments.length >= 3)
  nWin = new Window(this,nId,iscrolling,imaximizable);
 else
 if (arguments.length >= 2)
  nWin = new Window(this,nId,iscrolling,true);
 else
  nWin = new Window(this,nId,false,true);
//Attaching it into array ...
 if (nId == this.winList.length)
  this.winList.push(nWin);
 else
  this.winList[nId] = nWin;
//Setting properties ...
 nWin.setModal(iModal);
//Returning it ...
 return nWin;
}

WindowMgr.prototype.isEmpty = function(iid)
{
 return this.winList[iid] == null;
}

WindowMgr.prototype.collectGarbage = function()
{
 var
  result = new Array();

 for (var i=0;i<this.winList.length;i++)
 if (this.isEmpty(i) == false)
  result.push(this.winList[i]);
 this.winList.splice(0,this.winList.length);
 this.winList = this.winList.concat(result);
 result = null;
}

WindowMgr.prototype.getTopWindow = function(iModal)
{
 var
  cWin = null;
  
 if (arguments.length < 1)
  iModal = null;
//Searching for the top window ...
 for (var i=this.winList.length-1;i>=0;i--)
 {
  cWin = this.winList[i];
  if (cWin == null)
   continue;
  if (cWin.getVisible() == false)
   continue;
  if (iModal == null)
   return cWin;
  else
  if (cWin.getModal() == iModal)
   return cWin;
 }
 return null;
}

WindowMgr.prototype.onHideWindow = function(iid)
{
 this.autoSetBlockFilter();
}

WindowMgr.prototype.onCloseWindow = function(iid)
{
 var
  topWin = null;

//We'll set this id to null in list ...
 this.winList[iid] = null;
//We'll decrease all ids bigger than this one by one ...
 for (var i=iid+1;i<this.winList.length;i++)
 if (this.winList[i] != null)
  this.winList[i].setId(i-1);
//Collecting garbage ...
 this.collectGarbage();
//We'll bring the top window to front ...
 topWin = this.getTopWindow();
 if (topWin != null)
  topWin.bringToFront();
 else
  this.showBlockFilter(false);
}

WindowMgr.prototype.onBringWindowToFront = function(iid)
{
 var
  sz = 0;
  cWin = null;
  idPrev = this.winList.length;

//Getting required window ...
 cWin = this.winList[iid];
//We'll move the window on top ...
 this.winList.push(cWin);
//Moving all windows that are above this window one level down ...
 sz = this.winList.length;
 for (var i=iid;i<sz;i++)
 {
  if (this.winList[i] == null)
   continue;
  if (idPrev < i)
  {
   this.winList[idPrev] = this.winList[i];
   this.winList[idPrev].setId(idPrev);
  }
  idPrev = i;
 }
//Deleting the last window from the list ...
 this.winList.pop();
//Auto-searching for the place to put the block filter ...
 this.autoSetBlockFilter();
}
