//  Create pop up windows
//  Options and their defaults 


var windowoptions = {
  directories:"no"
  , height:0
  , width:0
  , location:"no"
  , menubar:"no"
  , scrollbars:"no"
  , resizable:"no"
  , status:"no"
  , toolbar:"no"
  , screenX:0
  , screenY:0
} ;

function popup( options, _href) {
  var pop;
  var href = (_href) ? _href : "" ;
   var _options = adjustoptions( options) ;
   pop = window.open( href, "", _options.popoptions);
   resize2( pop, _options) ;
   move( pop, _options) ;
   return pop ;
}

// depends on exactsize.js
function resize2( ww,options) {
   if (nIE4) 
      if (!exactsize( ww,options)) 
         resize( ww,options) ;
}

function resize( ww, options) {
  if (nIE4 && ww && ww.resizeTo && options.width &&  options.height) ww.resizeTo(options.width, options.height) ;
}

function move( ww, options) {
  if (nIE4 && ww && ww.moveTo && options.screenX && options.screenY)  ww.moveTo(options.screenX,options.screenY); 
}


function adjustoptions( options) {
  var _options = [] ;
  var popoptions = [] ;
  for (var o in windowoptions) { 
    var opt = windowoptions[ o] ;
    if (options[ o]) opt = options[ o] = pval( options, o) ;
    _options[ o] = opt ;
    popoptions[popoptions.length] = ( o + "=" + opt) ;
  }
  _options.popoptions = popoptions.join(",") ;
  return _options;
}

/*
  Allow things to be specified by % with an offset (+/-)
*/

var percents = {} ;
percents.width = percents.screenX = new Percent("screen.availWidth")  ;
percents.height = percents.screenY = new Percent("screen.availHeight") ;

function pval( options, opt) {
  if (percents[ opt]) return percents[ opt]( options[ opt]) ;
  return options[ opt] ;
}

 function Percent( measure) {
   var _measure = measure ;
   var fn =  function( x) {
     if (/(\d+)%([+-]\d+)?/.test( x)) {
       var m = eval( _measure) ;
       var pc = Math.floor( m * ((0 + RegExp.$1) / 100.0 )) ;
       var offset = (RegExp.$2) ? eval( RegExp.$2) : 0 ;
       return (pc + offset) ;
     }
     return x ;
   }
   
   return fn ;
 }


 
 /*
   Set the page to an exact size
   Try and deal with browser quirks
*/
   
function exactsize( win, _size) {
   
   // otherwise Opera displays a useless scrollbar
   var size = {
       height:+_size.height + 2
      ,width:+ _size.width + 2
      } ;
      
   var csize ;
   // get current size
   if (win.innerHeight) {
      csize = {
          w:win.innerWidth
         ,h:win.innerHeight
         } ;
   }
   else if (win.document.documentElement 
            && 
            win.document.documentElement.clientWidth) {
      csize = {
          w:win.document.documentElement.clientWidth
         ,h:win.document.documentElement.clientHeight
         } ;
   }
   else if (win.document.body) {
      csize = {
          w:win.document.body.clientWidth
         ,h:win.document.body.clientHeight
         } ;
   }
   /*
   seems to fuck up in IE, don't know why
   if (csize.w && csize.h && win.resizeBy) {
      win.resizeBy(
          (size.width - csize.w)
         ,(size.height - csize.h)
         );
      return true ;
   } */
   return false ;
}   
