ADJUST_BODY_SIZE = false;


function CheckBox( )
{
  var div = document.getElementById( 'output' );
  div.scrollTop = div.scrollHeight;
  var inp = document.getElementById("input");
inp.focus( );
}


function iframe( url ) {
  var idx = url.indexOf( '.php' )
  var thing = url.substring( 0, idx );
  var idstr = 'iframe_' + thing;

  var frame = document.getElementById( idstr );
  var n = 0;
  
  if( !frame || frame.getAttribute('inuse') == 1 ) {
    if( frame && frame.getAttribute('inuse') == 1 ) {  
      //while( document.getElementById( idstr+n ) && document.getElementById( idstr+n ).getAttribute('inuse') == 1 )
      
      while( (frame = document.getElementById(idstr+n)) && frame.getAttribute('inuse') == 1 )      
        n++;
        
      idstr = idstr + n;
    }
    if( !frame ) {
      frame = document.createElement( 'iframe' );
      frame.id = idstr;     
      frame.style.border = '0px';
      frame.style.width = '0px';
      frame.style.height = '0px';
      frame.onload = function() { this.setAttribute('inuse',0); }
      document.body.appendChild( frame )
    }
  }

  frame.src = url;
  frame.setAttribute( 'inuse', 1 );
  
}
/*
function iframe( url ) {
  var idx = url.indexOf( '.php' )
  var thing = url.substring( 0, idx );
  var idstr = 'iframe_' + thing;
  
  var frame = document.getElementById( idstr );
  if( !frame ) {
    frame = document.createElement( 'iframe' );  
    frame.id = idstr;  
    frame.style.border = '0px';
    frame.style.width = '0px';
    frame.style.height = '0px';
    document.body.appendChild( frame )
  }
  
  frame.src = url;
}
*/

function valid(e) {
  var k;

  if( window.event )
    k = e.keyCode; // IE
  else
    k = e.which;   // Firefox

  var t = document.getElementById('input');
//  if( t.value.length > 90 ) {
//    t.value = t.value.substr( 0, 90 );
//  }

  if( k == 13 )
    Send( );

  return true;
}

function Send( )
{
  var inp = document.getElementById('input');
  var buf = inp.value;
  iframe( 'send.php?i=' + encodeURIComponent( buf ) );
  inp.select();
  iframe( 'update.php?o=o' );
}

function RawSend( buf )
{
  iframe( 'send.php?i=' + encodeURIComponent( buf ) );
  iframe( 'update.php?o=o' );
}

function BrowserHeight( )
{ 
  return ( window.innerHeight ? window.innerHeight : document.body.offsetHeight );

}

function BrowserWidth( )
{
  return ( window.innerWidth ? window.innerWidth : document.body.offsetWidth );
}

function AdjustHeight( )
{
  whacker = 1;
  
  var div = document.getElementById("output");
  var inpbox = document.getElementById("inputbox");
  var inp = document.getElementById("input");
  var pr = document.getElementById("prompt");
  
  if( !div )  {
    return;
  }
  
  var h = BrowserHeight( )-50-76;
  var w = BrowserWidth( ) - 30;
  
  div.style.height = h + "px";
  div.style.width = w + "px";

  if( inp ) {
    var tmpt = (76 + h);
    inpbox.style.position = 'relative';
    inpbox.style.top = tmpt + 'px';    
    inpbox.style.left = '0px';
    inpbox.style.width = w + "px";
    inp.style.width = w + "px";
    if( pr ) {
      pr.style.position = 'relative';
      pr.style.top = tmpt + 'px';
      pr.style.left = '0px';
      pr.style.width = w + "px";
    }
  }
  
  div.scrollTop = div.scrollHeight;
//  document.getElementById('scroller').checked = false;
}


function CreateWindow( wName, wCaption, wWidth, wHeight, ttl )
{
  var div = document.createElement('div');
  div.id = 'window_' + wName;
  div.className = 'window';
  div.ttl = ttl;
  div.ucmd = wName;
//  div.style.height = wHeight + 'px';
//  div.style.width = wWidth + 'px';
  div.style.top = '50px';
  div.style.left = '75px';
  
  var titlediv = document.createElement('div');
  titlediv.className = 'titleBar';
  div.appendChild( titlediv );
  
  var span = document.createElement('span');
  span.className = 'titleBarText';
  var txt = document.createTextNode( wCaption );
  span.appendChild( txt );
  titlediv.appendChild( span );
  
  var img = document.createElement('img');
  img.className = 'titleBarButtons';
  img.alt = '';
  img.src = 'gui/buttons.png';
//  img.usemap = '#window_img_map';
//  img.removeAttribute('usemap');
  img.setAttribute('usemap', '#window_' + wName + '_img_map' ,0)
  img.width = '50';
  img.height = '14';  
  titlediv.appendChild( img );
  
  var map = document.createElement('map');
  map.id = 'window_' + wName + '_img_map';
  map.name = 'window_' + wName + '_img_map';
  var area = document.createElement('area');
  area.shape = 'rect';
  area.coords = '0,0,15,13';
  area.href = '';
  area.alt = '';
  area.title = 'Minimize';
  area.onclick=function() {this.parentWindow.minimize();return false; }
  map.appendChild( area );
  area = document.createElement('area');
  area.shape = 'rect';
  area.coords = '16,0,31,13';
  area.href = '';
  area.alt = '';
  area.title = 'Restore';
  area.onclick= function() {this.parentWindow.restore();return false; }
  map.appendChild( area );
  area = document.createElement('area');
  area.shape = 'rect';
  area.coords = '34,0,49,13';
  area.href = '';
  area.alt = '';
  area.title = 'Close';
  area.onclick=function() {this.parentWindow.close();return false; }
  map.appendChild( area );
  
  titlediv.appendChild( map );
  div.appendChild( titlediv );
  
  var cdiv = document.createElement('div');
  cdiv.className = 'clientArea';
//  cdiv.style.height = '200px';  
  //cdiv.innerHTML = '<iframe frameborder=\'0\' class=\'winfer\' src=\'' + wName + '.php\'></iframe>';
  div.appendChild( cdiv );  
  cdiv.style.height = wHeight + 'px';  
  // cdiv.width
  document.body.appendChild( div );

  cdiv.innerHTML = '<iframe onload="resizer(\'window_' + wName + '\', this.contentDocument.body.scrollWidth, this.contentDocument.body.scrollHeight ); " frameborder=\'0\' class=\'winfer\' src=\'' + wName + '.php\'></iframe>';
//  cdiv.innerHTML = '<iframe frameborder=\'0\' class=\'winfer\' src=\'' + wName + '.php\'></iframe>';
//this.style.width = this.contentDocument.body.scrollWidth+\'px\';
  
  winInit();
  
  winList['window_'+wName].open();
}

function resizer( dest, w, h ) 
{ 
  w += 25;
  
  winCtrl.active.frame.style.width = w + "px";
  
  if (browser.isIE)
    winCtrl.active.clientArea.style.width = (w - winCtrl.active.widthDiff) + "px";
    
  winCtrl.active.clientArea.style.height = h + "px";

  // Clip the title bar text, if necessary.


  if (w < winCtrl.active.clipTextMinimumWidth)
    winCtrl.active.titleBarText.style.width = (winCtrl.minimizedTextWidth + w - winCtrl.active.minimumWidth) + "px";
  else
    winCtrl.active.titleBarText.style.width = "";  
}

function UpdateMap( )
{
  var map = document.getElementById( 'map_window' );
  var wMap;  
  
  if( !map ) {
    wMap = CreateWindow( 'Map', 156, 186, 1 ); // 163
    var e = document.getElementById( wMap.nodeId );
    var img = document.createElement( 'img' );
    img.id = 'map_window';
    img.style.position = 'relative';
    img.style.top = '20px';
    img.style.left = '3px';
    img.boink = wMap; // wMap.nodeId;
    var div = document.createElement( 'div' );
    div.id = 'map_room_info';
    div.style.position = 'absolute';
    div.style.bottom = '0px';
    div.style.left = '0px';
    div.style.textAlign = 'center';
    div.style.width = '100%';
    e.appendChild( img );   
    e.appendChild( div );    
  } else {
    map.boink.hide( );
  }

  RawSend( '/map' );
}

function UpdateTells( )
{
  var tells = document.getElementById( 'tells_box' );
  var win;
  
  if( !tells ) {
    win = CreateWindow( 'Tells', 400, 250, 2 );
    var div = document.createElement( 'div' );
    div.style.position = 'relative';
    div.style.top = '20px';
    div.style.left = '3px';
    div.boink = win;
    div.id = 'tells_box';
    
    document.getElementById( win.nodeId ).appendChild( div );
  }
  
  RawSend( '/tell' );
}


function Spawn( cmd ) {
  if( cmd == 'who' ) {
    window_name = 'Who List';
    window_width = 450;
    window_height = 300;
    target = 'who_new.php';
    ttl = 10;
  } else if( cmd == 'map' ) {
    UpdateMap( );
    return;
  } else if( cmd == 'tells' ) {
    UpdateTells( );
    return;    
  } else {
    window_name = cmd;
    window_width = 300;
    window_height = 300;
    ttl = 30;
  }  
    
  if( winList['window_'+cmd] ) {
    winList['window_'+cmd].open();
  } else {
    var myWin = CreateWindow( cmd, window_name, window_width, window_height, ttl );
  }
}


function handle_menu( a )
{
  Spawn( a.id );  
}
