//
// 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 = 80;
var WindowLeft = 48; 
var WindowRight = 48; 
var WindowBottom = 48;
var WindowPadding = -16;
var WindowTitPad = -46;

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>');
  

// Functions

function WindowOpen(sTarget,sHref,nWidth,nHeight,sTitle,sColor) 
  {
  nWidth += WindowPadding*2;
  nHeight += WindowPadding*2;
  
  newDiv1 = document.createElement('div');
  newDiv2 = document.createElement('div');

  newDiv1.id = sTarget;
  newDiv2.id = sTarget+'Back_';

  if (!sTitle) sTitle = '';
  if (!sColor) sColor = 'white';
  
  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.2;
    newDiv2.style.filter = 'alpha(opacity=20)';
    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.2;
    newDiv2.style.filter = 'alpha(opacity=20)';
    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;">';
  sHtml += '<div class="WindowCloseButton" style="float:right;" onclick="WindowClose(\''+sTarget+'\');"></div>';
  sHtml += '<span id="'+sTarget+'Title_" style="font:bold 16px Arial;color:white;">'+sTitle+'</span>';
  sHtml += '</div>';
  sHtml += '<div style="position:absolute;cursor:wait;background:'+sColor+';left:'+(WindowLeft+WindowPadding)+'px;top:'+(WindowTop+WindowPadding)+'px;width:'+nWidth+'px;height:'+nHeight+'px;overflow:hidden;">';
  sHtml += '<iframe style="visibility:hidden;width:100%;height:100%;" name="'+sTarget+'" frameBorder="0" scrolling="no" src="'+sHref+'" onload="this.style.visibility=\'visible\';"></iframe>';
  sHtml += '</div>';
  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;
  }

