//              ====================================================================================================
//              ======================================= miscellaneous utilities ====================================
//              ====================================================================================================

function detectMac()                                             // find if the user is on a Mac machine
{
//__OS Detection Code modified from www.mozilla.org/
  macOS = (navigator.userAgent.toLowerCase().indexOf("mac")!=-1) ? true: false;
  mac68k= (macOS && ((navigator.userAgent.toLowerCase().indexOf("68k")!=-1) || (navigator.userAgent.toLowerCase().indexOf("68000")>-1)))  ? true: false;
  macppc= (macOS && ((navigator.userAgent.toLowerCase().indexOf("ppc")!=-1) || (navigator.userAgent.toLowerCase().indexOf("powerpc")>-1)))? true: false;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// requireded when sorting arrays numerically
function numOrdA(a, b)      //    ascending order
{
  return (a-b); 
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function numOrdD(a, b)      //    descending order
{
  return (b-a); 
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function imgLoaded(img)
{                                                     // During the onload event, IE correctly (!) identifies any images that
  if (!img.complete)                                  // weren't downloaded as not complete. Gecko-based browsers act like 
  {                                                   // NS4 in that they report this incorrectly.
    return false;
  }        
  if (typeof img.naturalWidth != "undefined"          // However, they do have two very useful properties: naturalWidth and
      && img.naturalWidth == 0)                       // naturalHeight. These give the true size of the image. If it failed
  {                                                   // to load, either of these should be zero.                                    
    return false;
  }
  return true;                                        // No other way of checking: assume it's ok.
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function captureScroll()                               // called by HTML
{
  if (firstTime)                                       // some browsers raise an onscroll event on pageload; if so...
  {
    firstTime = false;                                 // reset flag
  }  
  else
  {
    window.scrollTimer = setTimeout('areWeStillScrolling(0);', 500);    // wait 0.5 sec then test if scrolling has stopped 
  }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function areWeStillScrolling(prevTop)                  // called by captureScroll()
{
  var scrlAmt = getScrollY();
  clearTimeout(window.scrollTimer);
  
  if (prevTop == scrlAmt)                              // if scrolling has stopped...
  {
//    alert('prevTop = ' + prevTop + '\n' + 'scrlAmt = ' + scrlAmt + '\n' + 'roundedWk = ' + roundedWk)
    scrollBy(0, closestWeek(scrlAmt));
  }
  else
  {
    window.scrollTimer = setTimeout('areWeStillScrolling(' + scrlAmt  + ');', 500);    // check again after 0.5 sec
  }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 function getScrollY()                                 // called by areWeStillScrolling()
{                                                      // this function returns the amount scrolled down from top of page
  if (document.body && document.body.scrollTop)
    return document.body.scrollTop;
  if (document.documentElement && document.documentElement.scrollTop)
    return document.documentElement.scrollTop;
  if (window.pageYOffset)
    return window.pageYOffset;
  return 0;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function closestWeek(scrlAmt)                           // called by areWeStillScrolling()
{ 
  //var heading = 30;
  var rowHt = 120;
  var hidden= (scrlAmt % rowHt) - 14;
  
  return (hidden<(rowHt/2))?(hidden*-1):(rowHt-hidden); // return closest value
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function getViewportSize()    // to be replaced: Call vPortSizes() for returned array instead of using global var.
{
  if (typeof window.innerWidth != 'undefined')
  {
    vPortWide = window.innerWidth;
    vPortHigh = window.innerHeight;
  }
  else
  {
    if (typeof  document.documentElement              != 'undefined'	&&
        typeof  document.documentElement.clientWidth  != 'undefined'	&& 
                document.documentElement.clientWidth  !== 0)
    {
      vPortWide = document.documentElement.clientWidth; 
      vPortHigh = document.documentElement.clientHeight;
    }
    else
    {
      vPortWide = document.getElementsByTagName('body')[0].clientWidth;
      vPortHigh = document.getElementsByTagName('body')[0].clientHeight;
    }
  }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function vPortSizes()
{
  vPortWide = 0;
  vPortHigh = 0;
  if (typeof window.innerWidth != 'undefined')
  {
    vPortWide = window.innerWidth;
    vPortHigh = window.innerHeight;
  }
  else
  {
    if (typeof  document.documentElement              != 'undefined'	&&
        typeof  document.documentElement.clientWidth  != 'undefined'	&& 
                document.documentElement.clientWidth  !== 0)
    {
      vPortWide = document.documentElement.clientWidth;
      vPortHigh = document.documentElement.clientHeight;
    }
    else
    {
      vPortWide = document.getElementsByTagName('body')[0].clientWidth;
      vPortHigh = document.getElementsByTagName('body')[0].clientHeight;
    }
  }
  return[vPortWide,vPortHigh];
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function centerCoords(o)
{                                                               // in which quadrant of the viewport is object o?
  var c = $(o).getCoordinates();
  return [Math.floor(c.left + (c.width/2)),                     // return x,y coords as an array
          Math.floor(c.top  + (c.height/2))];
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function flyDirection(flyDiv)
{
  var vpSize  = vPortSizes();
  var limRt   = Math.round(vpSize[0]/2);
  var limDn   = Math.round(vpSize[1]/2);
  var imgRt   = posInPage($(flyDiv))[0] - window.getScrollLeft()  + Math.round($(flyDiv).getElement('img').width /2);  
  var imgDn   = posInPage($(flyDiv))[1] - window.getScrollTop()   + Math.round($(flyDiv).getElement('img').height/2);
  var flyDir  = (imgDn > limDn)?  'up-' : 'down-';
      flyDir += (imgRt > limRt)?  'left': 'right';
  return flyDir;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function flyDirections(flyDiv)
{
  var vpSize  = vPortSizes();
  var limRt   = Math.round(vpSize[0]/2);
  var limDn   = Math.round(vpSize[1]/2);
  var imgRt   = posInPage($(flyDiv))[0] - window.getScrollLeft()  + Math.round($(flyDiv).getElement('img').width /2);  
  var imgDn   = posInPage($(flyDiv))[1] - window.getScrollTop()   + Math.round($(flyDiv).getElement('img').height/2);
  var flyDirX = (imgRt > limRt)?  'fly-left': 'fly-right';
  var flyDirY = (imgDn > limDn)?  'fly-up'  : 'fly-down';
  return [flyDirX, flyDirY];
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function posInPage(obj)
{
	var oL = oT = 0;
	if (obj.offsetParent)
  {
		oL = obj.offsetLeft;
		oT = obj.offsetTop;
		while (obj = obj.offsetParent)    // NB: Not a syntax error but an assignment which returns 'null' at top of tree (!obj.offsetParent)
    {
			oL += obj.offsetLeft;
			oT += obj.offsetTop;
		}
	}
	return [oL,oT];
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function posInVP(obj)                                // returns array of obj's left & top w/ respect to viewport
{
	return [posInPage(obj)[0],(posInPage(obj)[1])-getScrollY()];
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//====================================================================================================
//========================================    diagnostic tools   =====================================
//====================================================================================================
//
function write2Status(msgTxt)   // for diagnostic displays on the status line when Firebug unavailable
{
  window.status = msgTxt;
  return true;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  just for testing...
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function randOrd()                                              // not used at this time
{
  return (Math.round(Math.random())-0.5);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//====================================================================================================
//====================================  detecting mouse position  ====================================
//====================================================================================================
//
function mouser(event)
{
  mouseX = event.clientX;
  mouseY = event.clientY;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function detectMouseCoordinates(e)
{
  mouseX  = 0;                         
  mouseY  = 0;                                  

  if(!e) var e=window.event;

  if(e.pageX||e.pageY)
  {
    mouseX=e.pageX;
    mouseY=e.pageY;
  }
  else
  {  
    if(e.clientX||e.clientY)
    {
      mouseX = e.clientX + document.documentElement.scrollLeft;
      mouseY = e.clientY + document.documentElement.scrollTop;
    }
  }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  document.Show.MouseX.value = tempX
  document.Show.MouseY.value = tempY
  return true
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//              ====================================================================================================
//              ====================================     detecting SHIFT key    ====================================
//              ====================================================================================================
//
function keyOn(event)
{
  keyShift = true;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function keyOff(event)
{
  keyShift = false;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function getY(elem)                                    // not called
{                                                      // this function returns the distance of an element from top of page
  var y = 0;
  for( var e = elem; e ; e = e.offsetParent)
  {
    y +=  e.offsetTop;
  }
  for (e = elem.parentNode; e && e !=  document.body; e = e.parentNode)
  {
    if (e.scrollTop)
    {
      y -=  e.scrollTop;
    }
  }
  return y;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
