// xFenster r9, Copyright 2004-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xFenster(clientId, iniTitle, iniUrl, iniX, iniY, iniW, iniH, enMove, enResize, enMaxRes, enClose, fnMove, fnResize, fnMaxRes, fnClose, fnFocus)
{
  var me = this;

  // Public Methods

  me.paint = function(dw, dh)
  {
    var m = 1, b = 1;
    var m2 = 2*m, b2 = 2*b;
    W += dw;
    H += dh;
    xResizeTo(con, W, H);
    /*@cc_on
    @if (@_jscript_version <= 5.6)
      xMoveTo(me.tbar, m, m);
      xWidth(me.tbar, W - m2 - b2);
      xLeft(me.sbar, m);
      xWidth(me.sbar, W - m2 - b2);
      xTop(me.sbar, H - xHeight(me.sbar) - m - b2);
    @end @*/
    xMoveTo(me.client, m, m + xHeight(me.tbar));
    xResizeTo(me.client, W - m2 - b2, H - xHeight(me.tbar) - xHeight(me.sbar) - m2 - b2);
  };
  me.focus = function(e) // don't use 'this' here
  {
    con.style.zIndex = xFenster.z++;
    for (var i in xFenster.instances) {
      xFenster.instances[i].tbar.className = 'xfTBar';
      xFenster.instances[i].sbar.className = 'xfSBar';
    }
    me.tbar.className = 'xfTBarF';
    me.sbar.className = 'xfSBarF';
    if (fnFocus) fnFocus(me);
  };
  me.href = function(s)
  {
    var h = '';
    if (isIFrame) {
      //alert('me.client.src: ' + me.client.src + '\nme.client: ' + me.client + '\ndocument: ' + me.client.document + '\ncontentDocument: ' + me.client.contentDocument + '\ncontentWindow: ' + me.client.contentWindow);
      if (me.client.contentWindow) {
        if (s) {me.client.contentWindow.location = s;}
        h = me.client.contentWindow.location.href;
      }
      else if (typeof me.client.src == 'string') { // for Safari/WebKit when iframe exists in html
        if (s) {me.client.src = s;}
        h = me.client.src;
      }
    }
    return h;
  };
  me.hide = function(e) // don't use 'this' here
  {
    var c = true;
    if (fnClose) {
      c = fnClose(me);
    }
    if (c) {
      con.style.display = 'none'
      xStopPropagation(e);
    }
  };
  me.show = function()
  {
    con.style.display = 'block';
    me.focus();
  };
  me.status = function(s)
  {
    if (s) {me.sbar.firstChild.data = s;}
    return me.sbar.firstChild.data;
  };
  me.title = function(s)
  {
    if (s) {me.tbar.firstChild.data = s;}
    return me.tbar.firstChild.data;
  };

  // Private Event Listeners

  function dragStart()
  {
    if (isIFrame) {
      for (var i in xFenster.instances) {
        xFenster.instances[i].client.style.visibility = 'hidden';
      }
    }
    else { me.focus(); }
  }
  function dragEnd()
  {
    if (isIFrame) {
      for (var i in xFenster.instances) {
        xFenster.instances[i].client.style.visibility = 'visible';
      }
    }
  }
  function barDrag(e, mdx, mdy)
  {
    var x = xLeft(con) + mdx;
    var y = xTop(con) + mdy;
    xMoveTo(con, x, y);
    if (fnMove) fnMove(me, x, y);
  }
  function resDrag(e, mdx, mdy)
  {
    me.paint(mdx, mdy);
    if (fnResize) fnResize(me, xWidth(me.client), xHeight(me.client));
  }
  function maxClick()
  {
    if (maximized) { // restore
      maximized = false;
      W = w; H = h;
      xMoveTo(con, x, y);
    }
    else { // maximize
      w = xWidth(con);
      h = xHeight(con);
      x = xLeft(con);
      y = xTop(con);
      xMoveTo(con, xScrollLeft(), xScrollTop());
      maximized = true;
      W = xClientWidth() - 2;
      H = xClientHeight() - 2;
    }
    me.paint(0, 0);
    if (fnMaxRes) fnMaxRes(me, W, H);
  }

  // Constructor Code

  me.tbar = null;
  me.sbar = null;
  me.client = xGetElementById(clientId);
  if (!me.client) {
    me.client = document.createElement(typeof iniUrl == 'string' ? 'iframe' : 'div');
    me.client.id = clientId;
  }
  me.client.className += ' xfClient';
  me.client.style.display = 'block';
  var x, y, w, h, maximized = false;
  var isIFrame = me.client.nodeName.toLowerCase() == 'iframe';
  xFenster.instances[clientId] = me;
  // create elements
  var con = document.createElement('div');
  con.className = 'xfCon';
  if (enResize) {
    var rbtn = document.createElement('div');
    rbtn.className = 'xfRIco';
    rbtn.title = 'Drag to Resize';
  }
  if (enMaxRes) {
    var mbtn = document.createElement('div');
    mbtn.className = 'xfMIco';
    mbtn.title = 'Click to Maximize/Restore';
  }
  if (enClose) {
    var cbtn = document.createElement('div');
    cbtn.className = 'xfCIco';
    cbtn.title = 'Click to Close';
  }
  me.tbar = document.createElement('div');
  me.tbar.className = 'xfTBar';
  if (enMove) {
    me.tbar.title = 'Drag to Move';
    if (enMaxRes) me.tbar.title += ', ';
  }
  if (enMaxRes) me.tbar.title += 'Double-Click to Maximize/Restore';
  me.tbar.appendChild(document.createTextNode(iniTitle));
  me.sbar = document.createElement('div');
  me.sbar.className = 'xfSBar';
  me.sbar.title = 'Click to Focus';
  me.sbar.appendChild(document.createTextNode('|'));
  // append elements
  con.appendChild(me.tbar);
  if (enMaxRes) me.tbar.appendChild(mbtn);
  if (enClose) me.tbar.appendChild(cbtn);
  con.appendChild(me.client);
  con.appendChild(me.sbar);
  if (enResize) me.sbar.appendChild(rbtn);
  document.body.appendChild(con);
  // final initializations
  var W = iniW, H = iniH;
  if (isIFrame) { me.href(iniUrl); }
  xMoveTo(con, iniX, iniY);
  me.paint(0, 0);
  if (enMove) xEnableDrag(me.tbar, dragStart, barDrag, dragEnd);
  if (enResize) xEnableDrag(rbtn, dragStart, resDrag, dragEnd);
  if (isIFrame) { con.onmousedown = me.focus; }
  else { con.onclick = me.focus; }// don't like this but can't use onmousedown here - it prevents dragging thumbnail on native scrollbar!
  if (enMaxRes) mbtn.onclick = me.tbar.ondblclick = maxClick;
  if (enClose) {
    cbtn.onclick = me.hide;
    cbtn.onmousedown = xStopPropagation;
  }
  con.style.visibility = 'visible';
  me.focus();
  xAddEventListener(window, 'unload',
    function () {
      if (enMove) xDisableDrag(me.tbar);
      if (rbtn) xDisableDrag(rbtn);
      con.onmousedown = con.onclick = null;
      if (mbtn) mbtn.onclick = me.tbar.ondblclick = null;
      if (cbtn) cbtn.onclick = cbtn.onmousedown = null;
      xFenster.instances[clientId] = null;
      me = null;
    }, false
  );
} // end xFenster object prototype

// xFenster static properties
xFenster.z = 100;
xFenster.instances = {};

