var BoxFrame = '<div id="SubWindow" style="display: fixed; top: 10px; left: 10px">'
+'<table cellspacing="0" cellpadding="2" width="%width%">'
+'  <tr style="border-bottom: 1px solid black; color: #000000" height="20">'
+'    <td style="background: url(/images/MainMenuBG.png) top left" width="5px">&nbsp;</td>'
+'    <td style="background: url(/images/MainMenuBG.png) top repeat-x" id="SubWindowTitle" onmousedown="HandleMouseDown(this)" onmouseup="HandleMouseUp(this)"><b>%title%</b></td>'
+'    <td style="background: url(/images/MainMenuBG.png) top right" width="5px" align="left">'
+'      <img width="14" src="/images/closebutton.png" onclick="CloseBox()" style="cursor: pointer"/>'
+'    </td>'
+'  </tr>'
+'  <tr height="%height%" valign="top" style="border: 1px solid black; background-color: white">'
+'    <td style="border-left: 1px solid black; border-bottom: 1px solid black">&nbsp;</td>'
+'    <td style="border-bottom: 1px solid black" align="left">'
+'      <div id="SubWindowHTML" onRemoteDone="CenterBox(\'SubWindow\',0)">&nbsp;</div>'
+'    </td>'
+'    <td style="border-right: 1px solid black; border-bottom: 1px solid black">&nbsp;</td>'
+'  </tr>'
+'</table>'
+'</div>';

var MouseX = 0;
var MouseY = 0;
var DragId = null;

function SetImageSwap(AImg,OverSrc){
  var OutSrc = AImg.src;
  AImg.onmouseover = function(){
    AImg.src = OverSrc;
  }
  AImg.onmouseout = function(){
    AImg.src = OutSrc;
  }
  AImg.src = OverSrc;
}

function MenuOver(AMenu,AId){
  var o = document.getElementById('SubMenu'+AId);
  o.style.display='';
  o.style.position = 'absolute';
}
function MenuOut(AMenu, AId){
  var o = document.getElementById('SubMenu'+AId);
  o.style.display='none';
}
function SubMenuOver(AItem){
  //AItem.style.color = '#ffffff';
  //AItem.style.backgroundColor = "#486c00"; //"#728617";
}
function SubMenuOut(AItem){
  //AItem.style.color = 'black';
  //AItem.style.backgroundColor = "#e3e3e3";
}

function CenterBox(AId,AInterval){
  var e = document.getElementById(AId);
  if (!e)
    return;
  if (e.style.display == 'none')
    return;
  if (e.id == DragId)
    return;
  var x = document.body.clientWidth/2 - e.clientWidth/2 + (document.body.scrollLeft + document.documentElement.scrollLeft);
  var y = document.body.clientHeight/2 - e.clientHeight/2 + (document.body.scrollTop + document.documentElement.scrollTop);
  if (x < 0)
    x = 0;
  if (y < 0)
    y = 0;
  if ((AInterval) && (AInterval > 0) && (AInterval < 1000)){
    var xx = parseInt(e.style.left);
    var yy = parseInt(e.style.top);
    x = (x - xx)/(1000/AInterval) + xx;
    y = (y - yy)/(1000/AInterval) + yy;
  }
  e.style.left = x;
  e.style.top = y;
  if ((AInterval) && (AInterval > 0)){
    setTimeout("CenterBox('"+AId+"',"+AInterval+")",AInterval);
  }
}

function HandleMouseUp(e){
  DragId = null;
}

function HandleMouseDown(e){
  e.onMouseUp =  function(){HandleMouseUp(e)}
  var w = document.getElementById('SubWindow');
  //Get current xy
  var CurX = parseInt(w.offsetLeft);
  var CurY = parseInt(w.offsetTop);
  
  var OfsX = CurX - MouseX;
  var OfsY = CurY - MouseY;
  
  w.setAttribute('OfsX',OfsX);
  w.setAttribute('OfsY',OfsY);
  DragId = w.id;
}

function HandleMouseMove(e){
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event) 
  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      MouseX = e.pageX;
      MouseY = e.pageY;
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      MouseX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
      MouseY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }
  }
  if (DragId){
    var w = document.getElementById(DragId);
    if (w){
      if (w.style.display == 'none'){
        DragId = null
      } else {
        var OfsX = parseInt(w.getAttribute('OfsX'));
        var OfsY = parseInt(w.getAttribute('OfsY'));
        w.style.left = MouseX + OfsX;
        w.style.top = MouseY + OfsY;
      }
    }
  }
}

function CloseBox(){
  document.onmousemove = null;
  var e = document.getElementById('SubWindow');
  if (!e)
    return;
  e.style.display = 'none';
}

function MakeBox(AWidth,AHeight,ATitle){
  //Remove The Old Dialog
  var e = document.getElementById('SubWindow');
  if (e)
    e.parentNode.removeChild(e);
  //Create The New Dialog
  var s = BoxFrame;
  s = s.replace('%title%',ATitle);
  s = s.replace('%width%',AWidth);
  s = s.replace('%height%',AHeight);
  var d = document.getElementById('SubWindowTarget');
  if (d)
    d.innerHTML = s;
  else
    document.body.innerHTML += s;
  var e = document.getElementById('SubWindow');
  if (!e){
    alert('Failed To Create Dialog');
    return null;
  }
  //Display
  e.style.display = '';
  e.style.position = 'absolute';
  CenterBox(e.id);
  //CenterBox(e.id,100);
  document.onmousemove = HandleMouseMove;
  return e;
}

function CopyInnerHTML(SourceId, TargetId){
  eSource = document.getElementById(SourceId);
  eTarget = document.getElementById(TargetId);
  if ((!eSource) || (!eTarget))
    return;
  eTarget.innerHTML = eSource.innerHTML;
}

//document.onmousemove = HandleMouseMove;