/* Smooth scrolling
   Changes links that link to other parts of this page to scroll
   smoothly to those links rather than jump to them directly, which
   can be a little disorienting.
   
   sil, http://www.kryogenix.org/
   
   v1.0 2003-11-11
   v1.1 2005-06-16 wrap it up in an object
*/

var SmoothScroll = {
  STEPS: 50, // smaller the number, faster the scroll
  
  init: function() {
    // lets get all the anchors elements on the page
    var links = $A(document.getElementsByTagName('a'));
    links.each(function(link) {
      // we need to make sure that we are dealing with anchors
      // that are only pointing to references within the page
      if ((link.href && link.href.indexOf('#') != -1)
      && ((link.pathname == location.pathname) 
      || ('/' + link.pathname == location.pathname)) 
      && (link.search == location.search)) {
        Event.observe(link, 'click', SmoothScroll.smoothScroll, false);
      }
    });
  },

  smoothScroll: function(e) {
    var initiator = Event.element(e);

    // make sure that the target is an element
    // and not a text node within an element
    if (initiator.nodeType == 3) {
			initiator = initiator.parentNode;
			
			// lets check to make sure that this is an A tag
			if (initiator.nodeName.toLowerCase() != 'a') { return; }
		}

    // lets get the target element
    var target = initiator.hash.substr(1)
    var targetObj = $(target);
    var destination = null;
    if (targetObj != null) {
			destination = targetObj;
    } else {
			// our target is now most likely an <a name>
			var anames = $A(document.getElementsByTagName('a'));
			anames.each(function(aname) {
				if (aname.name && (aname.name == targetObj)) {
					destination = aname;
				}
			});
    }

    // if we didn't find a destination, give 
    // up and let the browser do its thing
    if (!destination) { return true; }
  
    // find the destination's position
    var destx = destination.offsetLeft; 
    var desty = destination.offsetTop;
    var thisNode = destination;
   
    while (thisNode.offsetParent && (thisNode.offsetParent != document.body)) {
      thisNode = thisNode.offsetParent;
      destx += thisNode.offsetLeft;
      desty += thisNode.offsetTop;
    }
  
    // stop any current scrolling
    clearInterval(SmoothScroll.INTERVAL);
    cypos = SmoothScroll.getCurrentYPos();
  
    ss_stepsize = parseInt((desty-cypos)/SmoothScroll.STEPS);
    SmoothScroll.INTERVAL = setInterval('SmoothScroll.scrollWindow(' + ss_stepsize + ',' + desty +'," '+ target + '")', 10);
  
    // stop the actual click happening
    Event.stop(e);
  },

  scrollWindow: function(scramount,dest,anchor) {
    wascypos = SmoothScroll.getCurrentYPos();
    isAbove = (wascypos < dest);
    window.scrollTo(0,wascypos + scramount);
    iscypos = SmoothScroll.getCurrentYPos();
    isAboveNow = (iscypos < dest);
    if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
      // if we've just scrolled past the destination, or
      // we haven't moved from the last scroll (i.e., we're at the
      // bottom of the page) then scroll exactly to the link
      window.scrollTo(0,dest);
      // cancel the repeating timer
      clearInterval(SmoothScroll.INTERVAL);
      // and jump to the link directly so the URL's right
      location.hash = anchor;
    }
  },

  getCurrentYPos: function() {
    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;
  }
};