//
// Window functions
//


// Settings

var WindowImg1 = '/style/wnd1.png';
var WindowImg2 = '/style/wnd2.png';
var WindowImg3 = '/style/wnd3.png';
var WindowImg4 = '/style/wnd4.png';
var WindowImg5 = '/style/wnd5.png';
var WindowImg6 = '/style/wnd6.png';
var WindowImg7 = '/style/wnd7.png';
var WindowImg8 = '/style/wnd8.png';
var WindowImg9 = '/style/wnd9.png';
var WindowImgC = '/style/wndClose.png'

var WindowTop = 96;
var WindowLeft = 56; 
var WindowRight = 56; 
var WindowBottom = 56;
var WindowPadding = -12;
var WindowTitPad = -52;

var WindowZIndex = 1000;


// Initialization

document.write('<style type="text/css">');
document.write('.WindowCloseButton {display:block;width:20px;height:20px;background:url('+WindowImgC+') top right;cursor:pointer;} ');
document.write('.WindowCloseButton:hover {background-position:top left;} ');
document.write('</style>');
  

// Public Functions

function WindowOpen(sTarget,sHref,nWidth,nHeight,sTitle) 
  {
  nWidth += WindowPadding*2;
  nHeight += WindowPadding*2;
  
  newDiv1 = document.createElement('div');
  newDiv2 = document.createElement('div');

  newDiv1.id = sTarget;
  newDiv2.id = sTarget+'Back_';
  
  if (!window.XMLHttpRequest) {
    newDiv2.style.position = 'absolute';
    newDiv2.style.left = '0px';
    newDiv2.style.top = '0px';
    newDiv2.style.width = document.documentElement.clientWidth+'px';
    newDiv2.style.height = document.documentElement.clientHeight+document.documentElement.scrollTop+'px';
    newDiv2.style.backgroundColor = 'black';
    newDiv2.style.opacity = 0.5;
    newDiv2.style.filter = 'alpha(opacity=50)';
    newDiv2.style.zIndex = ++WindowZIndex;

    newDiv1.style.position = 'absolute';
    newDiv1.style.left = '50%';
    newDiv1.style.top = '50%';
    newDiv1.style.marginLeft = (-(nWidth+WindowLeft+WindowRight)/2)+'px';
    newDiv1.style.marginTop = (-(nHeight+WindowTop+WindowBottom)/2 + document.documentElement.scrollTop)+'px';
    newDiv1.style.width = nWidth+'px';
    newDiv1.style.height = nHeight+'px';
    newDiv1.style.padding = WindowTop+'px '+WindowRight+'px '+WindowBottom+'px '+WindowLeft+'px';
    newDiv1.style.zIndex = ++WindowZIndex;
  } else {
    newDiv2.style.position = 'fixed';
    newDiv2.style.left = '0px';
    newDiv2.style.top = '0px';
    newDiv2.style.right = '0px';
    newDiv2.style.bottom = '0px';
    newDiv2.style.backgroundColor = 'black';
    newDiv2.style.opacity = 0;
    newDiv2.style.filter = 'alpha(opacity=0)';
    newDiv2.style.zIndex = ++WindowZIndex;

    newDiv1.style.position = 'fixed';
    newDiv1.style.left = '50%';
    newDiv1.style.top = '50%';
    newDiv1.style.marginLeft = (-(nWidth+WindowLeft+WindowRight)/2)+'px';
    newDiv1.style.marginTop = (-(nHeight+WindowTop+WindowBottom)/2)+'px';
    newDiv1.style.width = nWidth+'px';
    newDiv1.style.height = nHeight+'px';
    newDiv1.style.padding = WindowTop+'px '+WindowRight+'px '+WindowBottom+'px '+WindowLeft+'px';
    newDiv1.style.zIndex = ++WindowZIndex;
  }

  var sHtml='';
  sHtml += '<div style="position:absolute;left:0px;top:0px;width:'+WindowLeft+'px;height:'+WindowTop+'px;background:url('+WindowImg1+') no-repeat;"></div>';
  sHtml += '<div style="position:absolute;left:'+WindowLeft+'px;top:0px;width:'+nWidth+'px;height:'+WindowTop+'px;background:url('+WindowImg2+') repeat;"></div>';
  sHtml += '<div style="position:absolute;right:0px;top:0px;width:'+WindowRight+'px;height:'+WindowTop+'px;background:url('+WindowImg3+') no-repeat;"></div>';
  sHtml += '<div style="position:absolute;left:0px;top:'+WindowTop+'px;width:'+WindowLeft+'px;height:'+nHeight+'px;background:url('+WindowImg4+') repeat;"></div>';
  sHtml += '<div style="position:absolute;left:'+WindowLeft+'px;top:'+WindowTop+'px;width:'+nWidth+'px;height:'+nHeight+'px;background:url('+WindowImg5+') repeat;"></div>';
  sHtml += '<div style="position:absolute;right:0px;top:'+WindowTop+'px;width:'+WindowRight+'px;height:'+nHeight+'px;background:url('+WindowImg6+') repeat;"></div>';
  sHtml += '<div style="position:absolute;left:0px;bottom:0px;width:'+WindowLeft+'px;height:'+WindowBottom+'px;background:url('+WindowImg7+') no-repeat;"></div>';
  sHtml += '<div style="position:absolute;left:'+WindowLeft+'px;bottom:0px;width:'+nWidth+'px;height:'+WindowBottom+'px;background:url('+WindowImg8+') repeat;"></div>';
  sHtml += '<div style="position:absolute;right:0px;bottom:0px;width:'+WindowRight+'px;height:'+WindowBottom+'px;background:url('+WindowImg9+') no-repeat;"></div>';

  nWidth -= WindowPadding*2;
  nHeight -= WindowPadding*2;

  sHtml += '<div style="position:absolute;left:'+(WindowLeft+WindowPadding)+'px;top:'+(WindowTop+WindowTitPad)+'px;width:'+(nWidth)+'px;text-align:center;cursor:move;" onmousedown="WindowDragInit(event,this.parentNode);">';
  sHtml += '<div class="WindowCloseButton" style="float:right;cursor:pointer;" onclick="WindowClose(\''+sTarget+'\');"></div>';
  sHtml += '<span id="'+sTarget+'Title_" style="font:bold 16px Arial;color:white;">'+sTitle+'</span>';
  sHtml += '</div>';
  sHtml += '<iframe style="position:absolute;left:'+(WindowLeft+WindowPadding)+'px;top:'+(WindowTop+WindowPadding)+'px;width:'+nWidth+'px;height:'+nHeight+'px;overflow:hidden;" name="'+sTarget+'" frameborder="0" scrolling="no" src="'+sHref+'"></iframe>';
  newDiv1.innerHTML = sHtml;

  document.body.appendChild(newDiv1);
  document.body.appendChild(newDiv2);
  }
  
function WindowClose(sTarget) 
  {
  document.body.removeChild(document.getElementById(sTarget));
  document.body.removeChild(document.getElementById(sTarget+'Back_')); 
  }
  
function WindowRefresh(sTarget) 
  {
  var oIFrame = document.getElementById(sTarget).lastChild;
  oIFrame.src = oIFrame.src;
  }

function WindowDocument(sTarget) 
  {
  var oIFrame = document.getElementById(sTarget).lastChild;
  var oDoc = (oIFrame.contentWindow || oIFrame.contentDocument);
  if (oDoc.document) oDoc = oDoc.document;
  return oDoc;
  }

function WindowSetTitle(sTarget,sTitle) 
  {
  document.getElementById(sTarget+'Title_').innerHTML = sTitle;
  }


// Private Functions


var WindowDrag = null;
var WindowDragX = 0;
var WindowDragY = 0;

function WindowDragInit(oEvent,oElement)
  {
  if (!oEvent) oEvent = window.event;
  WindowDrag = oElement;
  WindowDrag.style.left = WindowDrag.offsetLeft+'px'; 
  WindowDrag.style.top  = WindowDrag.offsetTop+'px'; 
  WindowDrag.style.marginLeft = '0px';
  WindowDrag.style.marginTop = '0px';
  WindowDrag.getElementsByTagName('iframe')[0].style.visibility="hidden";
  WindowDragX = oEvent.clientX;
  WindowDragY = oEvent.clientY;
  document.onmousemove = WindowDragMove;
  document.onmouseup = WindowDragEnd;
  clearSelection();
  document.onselectstart = function () { return false; };
  return false;
  }

function WindowDragEnd()
  {
  WindowDrag.getElementsByTagName('iframe')[0].style.visibility="visible";
  WindowDrag = null;
  document.onmousemove = null;
  document.onmouseup = null;
  document.onselectstart = null;
  clearSelection();
  }

function WindowDragMove(oEvent)
  {
  if (!oEvent) oEvent = window.event;
  WindowDrag.style.left = (parseInt(WindowDrag.style.left)+oEvent.clientX-WindowDragX)+'px'; 
  WindowDrag.style.top = (parseInt(WindowDrag.style.top)+oEvent.clientY-WindowDragY)+'px'; 
  WindowDragX = oEvent.clientX;
  WindowDragY = oEvent.clientY;
  return false;
  }

function clearSelection() {
  document.body.focus();
  //if (document.selection && document.selection.empty){
  //  document.selection.empty() ;
  //} else if(window.getSelection) {
  //  var sel=window.getSelection();
  //  if (sel && sel.removeAllRanges) sel.removeAllRanges() ;
  //}
}


