/*   picsi.js
    from pics   Feb2005
    This version creates a more sophisticated window with optional caption
    and makes every effort to size the window correctly.
    BJM Dec2004
    function to generate a new window of optimal size and display a picture
    If the window has already been opened it is just re-positioned since there is no point
    in opening more than one. If its open but minimised it will be un-minimised.
    N.B. File name is used to create window name so must not have spaces or - chars
    or any other strange symbols that might upset javascript or browser. ( Underscore is fine )
    Pictures which are larger than the display size are supported by allowing scrollbars
    Unfortunately latest security stuff doesn't allow them to be switched on and off
    after the image size is known so they have to be allowd for in window sizing
    ( IE always shows vert scroll even if not needed )
    
    When image download is slow sizing may be unreliable. It is not easy to wait
    for completion of download because some browsers do not multi-thread so neither looping nor
    interupt based methods work consistently( e.g. onLoad). Status flags such as image.complete
    are also inconsistent between browsers.
*/ 
   var win ;
   var bestHeight = 0 ;
   var bestWidth = 0 ;
   var theUrl ;
   var picImage = new Image() ;
   var caption = " " ;
   var hascap = false ;
   var wname = " ";
   var cappos ="92%";

   
function imgWindow( url , capst)       // main function to call from html
{

   theurl = url ;

   picImage.src = url ;         // start downloading we hope !
   picImage.onload = null;      // if set here it runs immediately
   var cntr = 0;

   wname = String ( urlName(theurl) ) ;    // name used to ensure uniqueness of window
   //alert( "wname is "+wname);
   if ( arguments.length > 1 )
   {   caption = String(capst) ; hascap = true ;}
   else { caption = " " ; hascap = false ;}     // blank caption strings overwrites loading message
                                                // although picture should also hide it

//   open/create the window
   win = window.open("",wname,
   "dependent=yes,resizable=yes,hotkeys=no,personalbar=no,scrollbars=yes,status=no" ) ;
// N.B. scrollbars yes - change to no if you prefer then make size allowances smaller below
//   alert("window created");
   picImage.onload = wsizer ;            // nb Mozilla/Firefox tries to run this at once
   if ( win.document.images.length > 0 )         //does it exist already ?
   {
//      alert( win.name + "already exists location = "+ win.location );
      win.focus() ;          // effectively bring to front
      if ( win.closed ) win.open();
   }

   else                                                 // need content for new window
   {
        var topmarg = 4 ;
        if ( hascap ) topmarg = 44 ;      // allowance for caption
        win.document.open() ;
        if ( hascap) win.document.write("<body ><center><p id='cap' name='cap' >"+caption+"</p></center>") ;
        win.document.write("<p style='position: absolute;top: 50%;text-indent: 20%' >"
                           +"<font size=+2>loading image ...</font></p>") ;
        win.document.write("<div style='position: absolute;top:"+topmarg+"px;left:4px;'><img src='"
                            +picImage.src+"' "+" alt='"+caption+"' ></div>") ;

        win.document.close();

        wsizer() ;
   }

   win.moveTo(5,5) ;
   return ;
}
function urlName(urln)
{
   parts = urln.split("/") ;
   if ( parts.length > 0 )
   { 
      fnames = parts[ parts.length-1 ].split(".") ;
      if ( fnames.length > 0 ) return fnames[0] ;
   }   
   else return "noname" ;
}


//  the following function is used both directly and by the image.onload event trigger
//  Since the trigger often runs when it shouldn't ( according to documentation )
//  lots of do nothing traps are required to prevent errors when the window is undefined
//  Mozilla also applies resize to all windows so the name check is vital.
function wsizer(ev)       // resize image window
{
//    alert("called wsizer") ;

    if ( win == undefined )  return ; // Mozilla fail trap

    if ( win.closed  ) return;        // IE fail trap

    if ( win.document == undefined ) return ;   // ditto

    if (win.name != wname ) return ;           //ditto

    var capheight = 0;
    if ( hascap ) capheight = 40 ;
    var plusx = 80 ;               // width allowance for scroll bar etc.
    var plusy = 110 ;              // height allowance for window title status bar etc.

    bestHeight = picImage.height ;      // get image size
    bestWidth = picImage.width ;
    if ( bestHeight > 10 && bestWidth > 10 )  // 11 X 11 is minimum picture size allowed
    {
      var scalex = 1.0 ;
      var scaley = 1.0 ;
                  // decide if window needs scaling to fit screen
      if( ( bestWidth + plusx ) > screen.width )        // needs horizontal scrolling
      {
         scalex =  screen.width  / (bestWidth + plusx) ;
         plusy = plusy + 30 ;                           // extra pixels for scroll bar
      }
      if ( ( bestHeight + plusy + capheight ) > screen.height )
      {
         scaley = screen.height / ( bestHeight + plusy + capheight ) ;
      }

      bestHeight = bestHeight * scaley - 20 ;           // scales window to fit screen
      bestWidth = bestWidth * scalex - 20 ;             // not image!!
    }
    else        // use 1/4 screen if you dont know the picture size
    {
       bestHeight = screen.Height/ 2 ;
       bestWidth  = screen.Width / 2 ;
    }
    win.focus() ;    // if you don't do this ie wont resize after deminimizing
    win.resizeTo(bestWidth + plusx, bestHeight + plusy + capheight );  // set size

//    win.status="status caption" ;  could put title in status area

    return ;
}

function nref(){}    // this empty function is to allow a dummy href of
                     // the form href="javascript:nref()" cos
                     // css :hover won't work unless an anchor contains an href attribute
                     // alternative is to use href="javascript:imgWindow(url,caption)"
