// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download.
// If you wish to share this code with others, please just point them
// to the URL instead.
// Please DO NOT link directly to my .js files from your site. Copy
// the files to your server and use them there. Thank you.
// ===================================================================
/*
AnchorPosition.js
Author: Matt Kruse
Last modified: 10/11/02
DESCRIPTION: These functions find the position of an <A> tag in a document,
so other elements can be positioned relative to it.
COMPATABILITY: Netscape 4.x,6.x,Mozilla, IE 5.x,6.x on Windows. Some small
positioning errors - usually with Window positioning - occur on the
Macintosh platform.
FUNCTIONS:
getAnchorPosition(anchorname)
  Returns an Object() having .x and .y properties of the pixel coordinates
  of the upper-left corner of the anchor. Position is relative to the PAGE.
getAnchorWindowPosition(anchorname)
  Returns an Object() having .x and .y properties of the pixel coordinates
  of the upper-left corner of the anchor, relative to the WHOLE SCREEN.
NOTES:
1) For popping up separate browser windows, use getAnchorWindowPosition.
  Otherwise, use getAnchorPosition
2) Your anchor tag MUST contain both NAME and ID attributes which are the
  same. For example:
  <A NAME="test" ID="test"> </A>
3) There must be at least a space between <A> </A> for IE5.5 to see the
  anchor tag correctly. Do not do <A></A> with no space.
*/
// getAnchorPosition(anchorname)
// This function returns an object having .x and .y properties which are the coordinates
// of the named anchor, relative to the page.
function getAnchorPosition(obj) {
  var useWindow=false;
  var y=0;
  // Browser capability sniffing
  var use_gebi=false, use_css=false, use_layers=false;
  if (document.getElementById) { use_gebi=true; }
  else if (document.all) { use_css=true; }
  else if (document.layers) { use_layers=true; }
  // Logic to find position
  if (use_gebi && document.all) {
  y=AnchorPosition_getPageOffsetTop(obj);
  }
  else if (use_gebi) {
  y=AnchorPosition_getPageOffsetTop(obj);
  }
  else if (use_css) {
  y=AnchorPosition_getPageOffsetTop(obj);
  }
  else if (use_layers) {
  y=obj.y;
  }
  else {
  y=0;
  }
  return y;
}
function AnchorPosition_getPageOffsetTop (el) {
  var ot=el.offsetTop;
// var alertstring = el.tagName + "1: " + el.offsetTop + "\n";
  while((el=el.offsetParent) != null) {
// alertstring += el.tagName + "2: " + el.offsetTop + "\n";
  ot += el.offsetTop;
  }
// alert( alertstring );
  return ot;
}
var currentArticleIndex = 0;
function gotoNextItem(nav4) {
  var i=0;
  var posY;
  var documentPosY = nav4 ? window.pageYOffset : document.documentElement.scrollTop;
  // find matching article
  for (i=0; i < document.anchors.length; i++) {
    if( document.anchors[i].name.substring(0, 8) == "article-" ) {
      posY = getAnchorPosition(document.anchors[i]);
      // find the next article
      if( posY > documentPosY + 20) {
        window.location.hash=document.anchors[i].name;
        break;
      }
    }
  }
  return;
}
function gotoPrevItem(nav4) {
  var flg="";
  var i=0;
  var documentPosY = nav4 ? window.pageYOffset : document.documentElement.scrollTop;
  var posY;
  // find matching article
  for (i=document.anchors.length - 1; i >= 0; i--) {
    if( document.anchors[i].name.substring(0, 8) == "article-" ) {
      posY = getAnchorPosition(document.anchors[i]);
      // find the next article
      if( posY < documentPosY - 5) {
        window.location.hash=document.anchors[i].name;
        break;
      }
    }
  }
  return;
}

function gotoMainIndex(nav4) {
  var i=0;
  var documentPosY = nav4 ? window.pageYOffset : document.documentElement.scrollTop;
  var posY;
  // find matching article
  for (i=document.anchors.length - 1; i >= 0; i--) {
    if( document.anchors[i].name.substring(0, 4) == "main" ) {
      window.location.href=document.anchors[i].href;
    }
  }
  return;
}

function gotoPrevIndex(nav4) {
  var i=0;
  var documentPosY = nav4 ? window.pageYOffset : document.documentElement.scrollTop;
  var posY;
  // find matching article
  for (i=document.anchors.length - 1; i >= 0; i--) {
    if( document.anchors[i].name.substring(0, 4) == "prev" ) {
      window.location.href=document.anchors[i].href;
    }
  }
  return;
}

function gotoNextIndex(nav4) {
  var i=0;
  var documentPosY = nav4 ? window.pageYOffset : document.documentElement.scrollTop;
  var posY;
  // find matching article
  for (i=document.anchors.length - 1; i >= 0; i--) {
    if( document.anchors[i].name.substring(0, 4) == "next" ) {
      window.location.href=document.anchors[i].href;
    }
  }
  return;
}

function shouldIgnoreInput(e) {
 var el = e.target || e.srcElement
 return el && el.tagName && (el.tagName.toUpperCase() == "INPUT" || el.tagName.toUpperCase() == "SELECT" || el.tagName.toUpperCase() == "TEXTAREA");
}
function hotkey(e) { 
  var nav4 = window.Event ? true : false;
  if(shouldIgnoreInput(e)) {
    return true;
  }
  
  if ((typeof e.ctrlKey != 'undefined') ? e.ctrlKey : e.modifiers & Event.CONTROL_MASK > 0) {
  return true;
  }
  if ((typeof e.altKey != 'undefined') ? e.altKey : e.modifiers & Event.ALT_MASK > 0) {
  return true;
  }
  // detect meta
  if ((typeof e.metaKey != 'undefined') && e.metaKey) {
  return true;
  } else if((typeof Event != 'undefined') && (e.modifiers & Event.META_MASK > 0)) {
  return true;
  }
  if (nav4) // Navigator 4.0x
  var whichCode = e.which
  else // Internet Explorer 4.0x
  if (e.type == "keypress") // the user entered a character
  var whichCode = e.keyCode
  else
  var whichCode = e.button;
  if( whichCode == 114 ) {
  // r - refresh
  cancelEvent(e);
  window.location.reload();
  return false;
  } else if( whichCode == 102 ) {
  // f - next folder
  cancelEvent(e);
//  if(parent && parent.treeframe && parent.treeframe.loadNextFolder) parent.treeframe.loadNextFolder();
  return false;
  } else if( whichCode == 115 ) {
  // s - next sub
  cancelEvent(e);
//  if(parent && parent.treeframe && parent.treeframe.loadNextSub) parent.treeframe.loadNextSub();
  return false;
  } else if( whichCode == 65 ) {
  // A - show all unread
  cancelEvent(e);
//  if(parent && parent.treeframe && parent.treeframe.doLoadAll) parent.treeframe.doLoadAll();
  return false;
  } else if( whichCode == 109 ) {
  // m - Main
  cancelEvent(e);
  gotoMainIndex(nav4);
  return true;
  } else if( whichCode == 110 ) {
  // n - Next
  cancelEvent(e);
  gotoNextIndex(nav4);
  return true;
  } else if( whichCode == 112 ) {
  // p - Prev
  cancelEvent(e);
  gotoPrevIndex(nav4);
  return true;
  } else if( whichCode == 106 ) {
  // j - scroll down pane
  cancelEvent(e);
  gotoNextItem(nav4);
  return false;
  } else if( whichCode == 107 ) {
  // k - scroll up pane
  cancelEvent(e);
  gotoPrevItem(nav4);
  return false;
  }
  return true;
}
function cancelEvent(event) {
  if (event.preventDefault) {
  event.preventDefault();
  event.stopPropagation();
  } else {
  event.returnValue = false;
  }
}
