// external.js

// ******************
// Global variables *
// ******************
// Used in addfav(), mailThisUrl() in index.js
// (called from index.html)  
var homepage = 'http://tandcl.homemail.com.au/';

// Used in sendEmail()
// and in addfav() in index.js 
// (called from index.html)
var hometitle = "Trevor and Carole's Home Page";

// Used in getHeading()
// Date on which the site was last updated
var site_update = "Saturday, 31 January 2009";

// Used in testNews() in index.js 
// (called from index.html)
// and in addNews() in news.html
// Date on which news was last updated
var news_date = new Date("Saturday, 25 October 2008");

// *************************
// General javascript code *
// *************************
// Host object testing 
// FROM http://peter.michaux.ca:80/articles/feature-detection-state-of-the-art-browser-scripting
// NOTE: These are "conservative" AND result in JS error
/********
function isHostMethod(object, property) {
  var t = typeof object[property];  
  return (t == 'function') 
      || (!!(t == 'object' && object[property])) 
      || (t == 'unknown');
}

function isHostCollection(object, property) {
  var t = typeof object[property];  
  return (!!(t=='object' && object[property])) 
      || (t == 'function');
}

function isHostObject(object, property) {
  return !!(typeof object[property] == 'object' && object[property]);
}

/* example call
alert ('isHostObject(document, getElementById): ' 
   + isHostObject('document', 'getElementById'))
*******/

// ******************************

// ********************************
// Used mainly in:                *
//   gallery.html, slideshow.html *
// But also used in many .html    *
// to display and print images    *
// ********************************
/***************************************
 * GENERAL FUNCTIONS                   *
 * Used in gallery.html slideshow.html *
 *   qsobj                             *
 *   spawnJimcoPopup                   *
 *   showPage                          *
 *   newWindow                         *
 *   getPic                            *
 *   printPic                          *
 *   printPage                         *  
 ***************************************/
// 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("To Print: Click on main window, then click the Print icon"); } 
}
  
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, action) {
  var T = document.getElementById("picname");
  
  // set dimensions
  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; }
  
  if (action) 
    { T.innerHTML = '<img src="' + pic + '" alt="" />'; }  
  else { 
    // set table background
    T.style.background = 'url(' + pic + ') no-repeat';    
    // 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 title of "mc"
    document.getElementById("mc").title = caption;    
    
    // Load details  into hidden <div>s
    document.getElementById("hiddenpicx").innerHTML = pic;
    document.getElementById("hiddencap").innerHTML = caption;
    document.getElementById("hiddenheight").innerHTML = height;
    document.getElementById("hiddenwidth").innerHTML = width;    
  }
} //---- end getPic() ------------

function printPic(pic, cap, height, width) {
  if (!pic) { 	
    pic = document.getElementById("hiddenpicx").innerHTML;
    cap = document.getElementById("hiddencap").innerHTML;
    height = document.getElementById("hiddenheight").innerHTML;
    width = document.getElementById("hiddenwidth").innerHTML;    
  }  
  spawnJimcoPopup
      (  'printpic.html?picture=' + pic
       + '&amp;caption=' + cap
       + '&amp;height=' + height
       + '&amp;width=' + width
        ,'' ,'' ,100 ,100 ,'center' ,'10000' ,'percent');
} //---- end printPic() -----------

// ********************
// Used in many .html *
// ********************
function DateTime() {
  // Called from getHeading () in external.js 
  var now = new Date();

  document.getElementById("hdate").innerHTML = now.toLocaleDateString();
  document.getElementById("htime").innerHTML
  	= now.toLocaleTimeString() + ' (UTC+' + (-now.getTimezoneOffset()*100/60) + ')';
} //---- end DateTime() ----------

function getHeading(story) {
  var T = document.getElementById("banner");
  T.innerHTML = document.title;
  
  if (story)
    { T.innerHTML += "<h5>By Carole M. Lawrence</h5>";}
  else {
    document.getElementById("updated").innerHTML = site_update;
    setInterval(DateTime,1000);
  }
} //---- end getHeading() --------

function breadCrumbs() {
  // Were passed parameters
  var base   = 'tandcl.homemail.com.au';
  var delStr = '>>';
  var defp   = 'index.html';
  var cStyle = 'sz16';
  var tStyle = 'sz16';
  var dStyle = 'sz16';
  var nl     = 0;
  // added by TL
  var localweb = 'myweb';
  var bc_text  = '';
  // -- end added ----
  var loc, subs, a, i;

  //----Internal functions ---
  function getLoc(c) {
    var k, d = "";

    if (c > 0){ 
      for (k = 0; k < c; k++)
        { d += "../"; }
    }    
    return d;
  } //---- end getLoc() --------
  function makeCaps(a) {
    var l, g = a.split(' ');

    for (l = 0; l < g.length; l++)
      { g[l] = g[l].toUpperCase().slice(0,1) + g[l].slice(1); }
    return g.join(" ");
  } //---- end makeCaps() ------
  //-- End Internal functions --

  loc = window.location.toString();
  if (loc.indexOf('file') != -1)
    { base = loc.substring(0, loc.indexOf(localweb) + localweb.length + 1); }

  subs = loc.substr(loc.indexOf(base) + base.length).split("/");
  bc_text
    = '<a href="' + getLoc(subs.length - 1) + defp + '"'
    +   ' target="_self"'				// Added by TL 20-09-2008
    +   ' class="'    + cStyle + '">Home</a>'
    + '<span class="' + dStyle + '">' + delStr + '</span>';

  a = (loc.indexOf(defp) == -1) ? 1 : 2;
  for (i = 0; i < (subs.length - a); i++)
    {
      subs[i] = makeCaps(unescape(subs[i]));
      bc_text
       += '<a href="' + getLoc(subs.length - i - 2) + defp + '"'
       +  ' target="_self"'				// Added by TL 20-09-2008 
       +  ' class="'      + cStyle + '">' + subs[i] + '</a>'
       +  '<span class="' + dStyle + '">' + delStr  + '</span>';
    }

  if (nl == 1)
    { bc_text += "<br>"; }
  bc_text
    += '<span class="' + tStyle + '">'
    +  document.title + '</span>'
    + "<b> (Note: Click on an underlined link to go to that page)</b>";

  document.getElementById("bcrumbs").innerHTML = bc_text;
} //---- end breadCrumbs() -------

function sendEMail(to) {
  var userTo, title, text, subject, body, CC, BCC;
  var userCC  = '', domainCC = '';
  var userBCC = '', domainBCC = '';

  var domainTo  = 'homemail';
  var domainext = '.com.au';
  
  if  (to == 'cml') {
    userTo = 'caroleml';
    title = 'Carole\'s Stories';
    text =  'Please send your comments';
  }
  else {
    userTo = 'tandcl';
    title = hometitle;
    text =  'Email Us';
  }

  subject   = 'Response from ' + title;
  body      = 'Please enter message here';

  CC  = (userCC  != '') ? userCC  + '@' + domainCC  : userCC;
  BCC = (userBCC != '') ? userBCC + '@' + domainBCC : userBCC;

  document.write
   (   '<a onmouseover="window.status=\'' + text + '\';return true;"'
     + '	 onmouseout="window.status=\' \';return true;"'
     + ' 	 href="mailto:' + userTo + '@' + domainTo + domainext
     + '?subject=' + subject
     + '&cc='   + CC
     + '&bcc='  + BCC
     + '&body=' + body + '">'
     + '<img src="images/display/mailto.gif"><br />'
     + text + '</a>') ;
} //---- end sendEmail() ---------