var windowOpen = null;
var windowMaximized = false;
var windowDrag = false;
var windowTop = '0px';
var windowLeft = '0px';
var windowWidth = 400;
var windowHeight = 400;
var windowLoadingOpen = false;
var windowScrollX = 0;
var windowScrollY = 0;
var dx, tx, dy, ty = 0;

function windowOC(evt, w)
{
  if (w == null) {

    w = windowOpen;

    if (windowMaximized) {
      windowMaxMin();
    }
    window.setTimeout(function() { windowSetOpacity(w, 0, 1.0); }, 50);

    windowOpen = null;

  } else {

    if (windowOpen != w) {

      if (windowOpen != null) {
        windowOC(evt, null);
      }

      if (ie6 || ie7) {
        w.style.filter = 'alpha(Opacity=0)';
      } else {
        w.style.opacity = 0.0;
      }

      window.setTimeout(function() { windowSetOpacity(w, 1, 0.0); }, 50);
      windowOpen = w;
    }

    html = document.getElementsByTagName('html')[0];

    w.style.top = Math.max(Math.min((evt.clientY + html.scrollTop - 100), html.clientHeight + html.scrollTop - 10 - windowHeight), html.scrollTop + 10) + 'px';
    w.style.left = Math.max(Math.min((evt.clientX + html.scrollLeft - 100), html.clientWidth + html.scrollLeft - 10 - windowWidth), html.scrollLeft + 10) + 'px';
    w.style.display = 'block';

  }
}

function windowSetOpacity(w, direction, previousOpacity)
{
  if (direction == 1) {
    newOpacity = Math.round((previousOpacity + 0.1) * 10) / 10.0;
  } else {
    newOpacity = Math.round((previousOpacity - 0.1) * 10) / 10.0;
  }

  if (ie6 || ie7) {
    w.style.filter = 'alpha(Opacity=' + Math.round(newOpacity * 100) + ')';
  } else {
    w.style.opacity = newOpacity;
  }

  if ((newOpacity < 1.0) && (newOpacity > 0.0)) {
    window.setTimeout(function() { windowSetOpacity(w, direction, newOpacity); }, 50);
  }
  if (newOpacity == 0.0) {
    w.style.display = 'none';
  }
}

function windowMove(e)
{
  if (windowOpen == null) {
    return;
  }

  if (windowMaximized) {
    return;
  }

  evt = e || window.event;

  windowDrag = true;

  tx = parseInt(windowOpen.style.left);
  ty = parseInt(windowOpen.style.top);

  dx = evt.clientX;
  dy = evt.clientY;

  document.onmousemove = windowMoveMouse;
  document.onmouseup = function() { document.onmousemove = null; document.onmouseup = null; };
}

function windowMoveMouse(e)
{
  if (windowOpen == null) {
    return;
  }

  if (windowDrag == false) {
    return;
  }

  evt = e || window.event;

  windowOpen.style.left = tx + evt.clientX - dx + 'px';
  windowOpen.style.top  = ty + evt.clientY - dy + 'px';

}

function windowMaxMin()
{
  if (windowOpen == null) {
    return;
  }

  if (windowMaximized) {

    windowOpen.style.left = windowLeft;
    windowOpen.style.top = windowTop;

    windowOpen.style.height = windowHeight + 'px';
    windowOpen.style.width = windowWidth + 'px';

    windowGetTitle(windowOpen).style.cursor = 'move';

    if (ie6 || ie7) {
      document.getElementsByTagName('html')[0].style.overflow = 'auto';
    } else {
      document.body.style.overflow = 'auto';
    }

    windowMaximized = false;

    window.scrollTo(windowScrollX, windowScrollY);

  } else {

    windowLeft = windowOpen.style.left;
    windowTop = windowOpen.style.top;

    windowOpen.style.top = '0px';
    windowOpen.style.left = '0px';

    if (ie6 || ie7) {
      windowOpen.style.width = document.documentElement.clientWidth + 'px';
      windowOpen.style.height = document.documentElement.clientHeight + 'px';
      windowScrollY = document.documentElement.scrollTop;
      windowScrollX = document.documentElement.scrollLeft;

      document.getElementsByTagName('html')[0].style.overflow = 'hidden';
    } else {
      windowOpen.style.width = window.innerWidth + 'px';
      windowOpen.style.height = window.innerHeight + 'px';
      windowScrollX = window.pageXOffset;
      windowScrollY = window.pageYOffset;

      document.body.style.overflow = 'hidden';
    }

    windowGetTitle(windowOpen).style.cursor = 'pointer';

    window.scrollTo(0, 0);


    windowMaximized = true;
  }
}

function windowGetTitle(w)
{
  for (i = 0; i < w.childNodes.length; i ++) {
    if (w.childNodes[i].className == 'title') {
      return w.childNodes[i];
    }
  }
}

function windowLoading()
{
  w = document.getElementById('loading');

  if (windowLoadingOpen) {
    w.style.display = 'none';
    windowLoadingOpen = false;
  } else {
    w.style.display = 'block';
    windowLoadingOpen = true;
  }
}