// xFenster r4, 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(id, iniX, iniY)
{
  var _i = this;

  // Public Methods
  _i.unload = function()
  {
    xDisableDrag(_i.tbar);
    xDisableDrag(rbtn);
    mbtn.onclick = con.onmousedown = null;
    _i.tbar = _i.sbar = _i.client = con = rbtn = mbtn = null;
    _i = null;
    // unload not complete
  };
  _i.paint = function(dw, dh)
  {
    var m = 1, b = 1;
    var m2 = 2*m, b2 = 2*b;
    W += dw;
    H += dh;
    xResizeTo(con, W, H);
    xMoveTo(_i.tbar, m, m);
    xWidth(_i.tbar, W - m2 - b2);
    xLeft(_i.sbar, m);
    xWidth(_i.sbar, W - m2 - b2);
    xTop(_i.sbar, H - xHeight(_i.sbar) - m - b2);
    xMoveTo(_i.client, m, m + xHeight(_i.tbar));
    xResizeTo(_i.client, W - m2 - b2, xTop(_i.sbar) - xTop(_i.client));
  };
  _i.focus = function(e) // don't use 'this' here
  {
    con.style.zIndex = xFenster.z++;
    for (var i = 0; i < xFenster.instances.length; ++i) {
      xFenster.instances[i].tbar.className = 'xfTBar';
      xFenster.instances[i].sbar.className = 'xfSBar';
    }
    _i.tbar.className = 'xfTBarF';
    _i.sbar.className = 'xfSBarF';
  };
  // Private Event Listeners
  function dragStart()
  {
    if (isIframe) {
      for (var i = 0; i < xFenster.instances.length; ++i) {
        //xFenster.instances[i].client.style.visibility = 'hidden';
      }
    }
    else { _i.focus(); }
  }
  function dragEnd()
  {
    if (isIframe) {
      for (var i = 0; i < xFenster.instances.length; ++i) {
        //xFenster.instances[i].client.style.visibility = 'visible';
      }
    }
  }
  // añadido salvapc, para centrar la ventana despues de resize
  function resdragEnd() {centrar(con);xShow(_i.client);}
  //////////////////////////////////////////////////////////////
  // añadido salvapc, para cerrar la ventana
  function closeOnClick() {xHide(con); xHide(_i.client); }
  //////////////////////////////////////////////////////////////
  function barDrag(e, mdx, mdy)
  {
    xMoveTo(con, xLeft(con) + mdx, xTop(con) + mdy);
  }
  function resDrag(e, mdx, mdy)
  {
    _i.paint(mdx, mdy);
  }
  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();
      H = xClientHeight();
    }
    _i.paint(0, 0);
  }
  // Public Properties
  _i.tbar = null;
  _i.sbar = null;
  _i.client = xGetElementById(id);
  _i.client.className += ' xfClient';
  _i.client.style.display = 'block';
  // Private
  var W = xWidth(_i.client);
  var H = xHeight(_i.client);
  var x, y, w, h, maximized = false;
  var isIframe = _i.client.nodeName.toLowerCase() == 'iframe';
  xFenster.instances[xFenster.instances.length] = _i;
  // create elements
  var con = document.createElement('div');
  // añadido salvapc, para poner un id al contenedor y lo pueda utilizar
  // la funcion slide
  if (isIframe) {
  	con.id = "contenedorxfenserI";
  } else {
  	con.id = "contenedorxfenserD";
  }
  _i.conID = con.id;
  //
  con.className = 'xfCon';
  var rbtn = document.createElement('div');
  rbtn.className = 'xfRIco';
  rbtn.title = 'Arrastra para Redimensionar';
  var mbtn = document.createElement('div');
  mbtn.className = 'xfMIco';
  mbtn.title = 'Click para Maximizar/Restaurar';
  // añadido salvapc, añado el boton de cerrar
  var cbtn = document.createElement('div');
  cbtn.className = 'xfCIco';
  cbtn.title = 'Click para Cerrar';
  ///////////////////////////////////////////
  _i.tbar = document.createElement('div');
  _i.tbar.className = 'xfTBar';
  _i.tbar.title = 'Arrastra para Mover';
  _i.tbar.appendChild(document.createTextNode(_i.client.title));
  _i.client.title = "";
  _i.sbar = document.createElement('div');
  _i.sbar.className = 'xfSBar';
  _i.sbar.title = 'Click to Focus';
  _i.sbar.appendChild(document.createTextNode('')); // temporary //
  // append elements
  con.appendChild(_i.tbar);
  _i.tbar.appendChild(mbtn);
  // Añadido salvapc, añado el boton de cerrar
  _i.tbar.appendChild(cbtn);
  ///////////////////////////////////////////
  con.appendChild(_i.client);
  con.appendChild(_i.sbar);
  _i.sbar.appendChild(rbtn);
  document.body.appendChild(con);
  // final initializations
  xMoveTo(con, iniX, iniY);
  _i.paint(0, 0);
  xEnableDrag(_i.tbar, dragStart, barDrag, dragEnd);
  // Modificado por salvapc, para centrar la ventana despues de un resize
  //xEnableDrag(rbtn, dragStart, resDrag, dragEnd);
  xEnableDrag(rbtn, dragStart, resDrag, resdragEnd);
  ///////////////////////////////////////////////////////////////////////
  if (isIframe) { con.onmousedown = _i.focus; }
  else { con.onclick = _i.focus; }// don't like this but can't use onmousedown here - it prevents dragging thumbnail on native scrollbar!
  mbtn.onclick = maxClick;
  // añadido salvapc, evento para cerrar la ventana
  cbtn.onclick = closeOnClick;
  /////////////////////////////////////////////////
  xShow(con);
  _i.focus();
  if (isIframe) {
    /* Try to add a mousdown listener to the iframe's document so the user can
       click 'anywhere' and focus the xIFrame. The following works for me in
       Opera 9.10, IE 6 and IE 7. It does not work in Firefox 2.0 - why?
       This will not work if the iframe's src is not from the same domain as its parent page.
    */
/*
    try {
      var doc = _i.client.contentWindow || _i.ifrm.contentDocument;
      if (doc.document) { doc = doc.document; }
      xAddEventListener(doc, 'mousedown', _i.focus, false);
    }
    catch(err) {;}
*/
  }
} // end xFenster object prototype

// xFenster static properties
xFenster.z = 0;
xFenster.instances = [];
