var isIE = (navigator.userAgent.indexOf("MSIE")>=0);

function openwin( name, hide ) {
  var ob = {
    name: name,
    title: _getel( name + '_title' ),
    win:   _getel( name + '_win'   ),
    content: _getel( name + '_content' ),
    open: 1,
    gray: _getel( name + '_gray' ) || ''
  };
  if( !ob.gray ) {
    ob.title.onmousedown = tdown.bind(ob);
    ob.title.onmouseup   = tup.bind(ob);
    ob.title.onmousemove = tmove.bind(ob);
  }
  _getel( name+'_close').onclick = closewin.bind( ob );
  if(!hide ) _show( ob.win );
  centerwin( ob );
  checkrange( ob );
  return ob;
}

function centerwin( ob, wid ) {
  var dim = getBounds( ob.win );
  if( !wid ) ob.wid = dim.right-dim.left;
  else ob.wid = wid;
  ob.heg = dim.bottom-dim.top;
  
  // scrollHeight is whole document; clientHeight is view
  ob.left = document.body.clientWidth /2 - ( ob.wid )/2;
  var vs = document.all ? document.body.scrollTop : window.pageYOffset; // vertical scroll
  
  ob.top  = /*document.body.clientHeight/2 - ob.heg/2 - 50 +*/ vs + 100;
  ob.win.style.left = ob.left + 'px';
  ob.win.style.top  = ob.top + 'px';
  ob.win.style.width = ob.wid + 'px';
  ob.win.style.zIndex = 2;
}

function closewin() {
  if( this.gray ) {
    _del( this.gray );
  }
  _del( this.win );
  _hide( this.win );
  this.open = 0;
}

var starty, startx;
var isdown = 0;
function tdown( e ) {
  // track global mouse movement ( window does not keep up with mouse... )
  if( isIE ) {
    document.onmousemove = tmove.bind(this);
    document.onmouseup   = tup.bind(this);
  }
  else {
    window.onmousemove = tmove.bind(this);
    window.onmouseup   = tup.bind(this);
  }
  if( !e ) e = event;
  starty = e.clientY;
  startx = e.clientX;
  this.top  = nopx( this.win.style.top );
  this.left = nopx( this.win.style.left );
  isdown = 1;
  return false;
}
function nopx(val) { return val.replace('px','')*1; }
function tup() {
  // stop mouse tracking
  if( isIE ) document.onmousemove = document.onmouseup = null;
  else window.onmousemove = window.onmouseup = null;
  isdown = 0;
}
function tmove(e) {
  if( !e ) e = event;
  if( e.stopPropagation ) e.stopPropagation();
  else e.cancelBubble = true;
  if( isIE && !event.button ) { tup(); return false; } // mouseup occured outside of window
  if( !isdown ) return false;
  var newy = this.top  + e.clientY - starty;
  var newx = this.left + e.clientX - startx;
  var maxx = document.body.clientWidth - this.wid - 10; // maximum left edge that is in window
  if( newx > maxx ) newx = maxx; // if off right edge, set within
  if( newx < 0 ) newx = 0; // if off left edge, set within
  this.win.style.top  = newy + 'px';
  this.win.style.left = newx + 'px';
  //_hide( this.content );
  return false;
}
function checkrange( ob ) { // ensure popup has not escaped from window
  if( !isdown ) {
    var maxx = document.body.clientWidth - ob.wid - 10;
    if( nopx( ob.win.style.left ) > maxx ) ob.win.style.left = maxx +'px';
  }
  if( ob.open ) setTimeout( checkrange.bind( 0, ob ), 200 );
}

function getBounds( el ) {
  var $top    = 0;
  var $left   = 0;
  var $right  = el.clientWidth || 0;
  var $bottom = el.clientHeight || 0;
  do {
    $top  += el.offsetTop  || 0;
    $left += el.offsetLeft || 0;
    if( el = el.offsetParent ) {
      if( el.tagName == 'BODY' ) break;
      var p = el.style.position;
      if( isIE && ( p == 'relative' || p == 'absolute') ) break;
    }
  } while( el );
  return { top: $top, left: $left, right: $right + $left, bottom: $bottom + $top };
}

/*<div class='mainbox' id='test_win' style='display:none'>
  <img id='test_close' src='cross.png' class='close'/><div id='test_title' class='title'>Quick Search</div>
  <div style='padding: 10px'>
  </div>
</div>*/
function genpop( name, title, width, height, gray ) {
  var win = _newdiv('t_win');
  if( width ) win.style.width = width + 'px';
  win.id = name + '_win';
  var close = _newimg( '/template/wcm/imgs/cross.png' );
  close.className = 't_close';
  close.id = name + '_close';
  var titled = _newdiv('t_title');
  //_append( titled, _newtext( title ) );
  titled.innerHTML = title;
  titled.id = name + '_title';
  var content = _newdiv('t_content');
  content.id = name + '_box';
  if( height ) {
    content.style.height = height+'px';
  }
  
  var d = _newel('div');
  d.id = name + '_content';
  _append( content, d );
  _append( win, close, titled, content );
  _hide( win );
  _append( document.body, win );
  
  if( gray ) {
    var div = _newdiv('grayover');
    div.id = name + '_gray';
    _append( div, _newnbsp() );
    //_hide( div );
    //_append( document.body, div );
    _insertbefore( document.body.firstChild, div );
  }
  
  return d;
}

//--

function popfile( url, wid, heg ) {
  var win = genpop( 'gal','&nbsp;', wid, heg, 1 );
  var ifr = _newel('iframe');
  ifr.src = url;
  ifr.width = wid;
  ifr.height = heg;
  ifr.frameBorder = 0;
  _append( win, ifr );
  openwin('gal');
}