// external.js

/***************************************
 * GENERAL FUNCTIONS                   *
 * Used in gallery.html slideshow.html *
 *   qsobj                             *
 *   spawnJimcoPopup                   *
 *   showPage                          *
 *   newWindow                         *
 *   getPic                            *
 *   getPic2                           *
 *   printPic                          *
 ***************************************/
// Allow use of getElementById in all browsers
if (!document.getElementById) {
  document.getElementById =
    function(id){
      return   (document.all) ? document.all[id]
             : (document.layers) ? document.layers[id]
             : alert('getElementById is not supported.\n'
                    + 'Parts of this site will not work as intended')
    } // end function
} // end if

// Alert if window.print not available
if (!window.print)
  { window.print = function(){ alert("Print function not available"); } }

function qsobj(parm) {
  // get url string after '?' and split by "&" into an array
  var qpairs = location.search.substring(1).split("&");
  if (!qpairs) { return null; }

  // split qpairs[parm] by "=" into an array
  var qvbl = qpairs[parm].split("=");

  // return result if it exists, else blank string
  return qvbl[1] ? unescape(qvbl[1].replace(/%20|\ +/g," ")) : '';
} // ---- end qsobj() ------------

function spawnJimcoPopup(url, name, options, h, w, x, y, scaleType) {
/**********************************************************
 * NOTE: name = '' or null === name = '_blank'            *
 *       options = '' or null sets all options to default *
 *       options = 'no' sets all options = no             *
 *       h, w = height and width                          *
 *       x, y = a number or 'center'                      *
 *       scaleType = 'percent' or 'pixel'                 *
 * FOR FULL SCREEN, call as                               *
 *       spawnJimcoPopup(url, name, options)              *
 *    OR spawnJimcoPopup(url, name)                       *
 *    OR spawnJimcoPopup(url)                             *
 *    OR call window.open(...) with same parameters       *
 **********************************************************/
  if (!options)
    { options = ''; }
  else if (options=='no') {
    options = 'toolbar=no,location=no,directories=no,status=no'
            + ',menubar=no,scrollbars=no,resizable=no';
  }

  if (scaleType == 'percent') { 
     h = (h * screen.availHeight) / 100;
     w = (w * screen.availWidth)  / 100;
  }
  if (x == 'center')
    { x = (screen.availWidth  - w) / 2; }
  if (y == 'center')
    { y = (screen.availHeight - h) / 2; }

  options += (w) ? ',width='  + w : '';
  options += (h) ? ',height=' + h : '';
  options += (x) ? ',left='   + x : '';
  options += (y) ? ',top='    + y : '';

  window.open(url, name, options);
} // ---- end spawnJimcoPopup() --

function showPage(show) {
  var T = document.getElementById("wait");
  if (!T) { return; }
  
  T.style.display = (show) ? '' : 'none';
} // ---- end showPage() ---------

function newWindow(img, caption) {
  var imageToLoad = new Image(), nw_count = 1;
  var bigpic = document.getElementById("bigpic");
    
  //----Internal function ---
  function loadchk() {
    var h = 0, w = 0;
    var html_1 = '', html_2 = '', html_3 = ''; 
    
    if (!imageToLoad.complete && nw_count ++ < 10)
      { setTimeout(loadchk,1000); }
    else {
      // Hide Loading GIF
      showPage();
      
      if (nw_count >= 10)
        { alert('Image load failed after ' + (nw_count -1)  + ' tries.' );  }
      else { // complete
        h = imageToLoad.height;
        w = imageToLoad.width;
    
        if (bigpic) {
          getPic(img, caption, h, w);
          document.getElementById("hiddenpic").src=img;
          
          if (pic_fs != 'yes') {            
            bigpic.style.height = h + 75;
            bigpic.style.width = w;
            bigpic.style.left = (screen.availWidth  - w) / 2;
          }
          bigpic.style.display = 'block';
        } // end (bigpic)
        
        else { // (!bigpic) 
          spawnJimcoPopup
              (   'picture.html?picture=' + img
                + '&amp;caption=' + caption
                + '&amp;height=' + h
                + '&amp;width=' + w
                 ,'' ,'resizable=yes' ,h + 75 ,w ,'center' ,'0' ,'pixel');
        } // end (!bigpic) 
      } // end complete
    } // end !(!imageToLoad.complete && nw_count ++ < 10)
  } //---- end loadchk() -----------
  
  // Display Loading GIF
  showPage('show');
  
  imageToLoad.src = img;    
  setTimeout(loadchk,500);
} // ---- end newWindow() ---------

function getPic(pic, caption, height, width) {
  var T = document.getElementById("picname").style;

  // set table styles
  T.background = 'url(' + pic + ') no-repeat';
  T.height     = height + "px";
  T.width      = width  + "px";

  // set width of table cell "tc" = Row1, Col2
  document.getElementById("tc").style.width  = (width - 40) + "px";

  // set height of table cell "mc" = Row2, Col1/2/3
  document.getElementById("mc").style.height = (height - 40) + "px";

  // Set caption and titles
   document.getElementById("piccaption").innerHTML
     = document.getElementById("mc").title = caption;

  // Set copyright
  if (copyrt)
    { document.getElementById("copyrt").innerHTML = copyrt; }
} //---- end getPic() ------------

function getPic2(pic, caption, height, width) {
  var T = document.getElementById("picname");

  // Set image
  T.innerHTML = '<img src="' + pic + '" alt="" />';

  // set size
  T.style.height = height + "px";
  T.style.width  = width  + "px";

  // Set caption
  document.getElementById("piccaption").innerHTML = caption;

  // Set copyright
  if (copyrt)
    { document.getElementById("copyrt").innerHTML = copyrt; }
} //---- end getPic2() ------------

function printPic(pic, caption, height, width) {
  if (!pic) {
    var imageToPrint = new Image();	
    
    pic = imageToPrint.src = document.getElementById("hiddenpic").src;
    caption = document.getElementById("piccaption").innerHTML;  
    height = imageToPrint.height;  
    width = imageToPrint.width;
    if (!height) {
      alert('Image not loaded.\nTry again')	
      return;
    }
  } 
  
  spawnJimcoPopup
      (  'printpic.html?picture=' + pic
       + '&amp;caption=' + caption
       + '&amp;height=' + height
       + '&amp;width=' + width
        ,'' ,'' ,100 ,100 ,'center' ,'10000' ,'percent');
} //---- end printPic() -----------
