/*
	Smooth scrolling anchor tags
	Kenneth Kufluk 04/2k8
	Digitas
	Version 2.0
*/

var dtas_INTERVAL;
var dtas_STEPS = 25;
var dtas_LOADED = false;
var dtas_LOADEDTimer = 0;
var dtas_scrolled=false;

function dtas_scrollOnPageLoad() {
	// if we have a hash when we load, go straight to it.
	if (location.hash!="") dtas_scrollToHash(location.hash);
}

function dtas_stopScroll() {
	if (dtas_scrolled) return (dtas_scrolled=false);
	if (!dtas_LOADED) {
		clearTimeout(dtas_LOADEDTimer);
	}
	clearInterval(dtas_INTERVAL);
}

function dtas_updateLinks() {
	dtas_LOADED = true;
	var pageLinks = document.getElementsByTagName('a');
	for (var i=0;i<pageLinks.length;i++) {
		var lnk = pageLinks[i];
	    if (!lnk.href.match(/\#$/) && (lnk.href && lnk.href.indexOf('#') != -1)) {
			var anch = lnk.href.split('#')[1];
			if ($(anch)) $(anch).name = anch;
			if (( (lnk.pathname == location.pathname) ||
				('/'+lnk.pathname == location.pathname) ) &&
	        	(lnk.search == location.search)) {
				dtas_addEvent(lnk,'click',dtas_smoothScroll);
//				$(lnk).observe('click',dtas_smoothScroll);
			}
		}
	}
}
function bing() {
	alert('bing');
}

function dtas_smoothScroll(e) {
  if (window.event) {
    target = window.event.srcElement;
  } else if (e) {
    target = e.target;
  } else return;

  if (target.nodeType == 3) {
    target = target.parentNode;
  }
  if (dtas_scrollToHash(target.hash)) {
		return true;
  } else {
	  // stop the actual click happening
	  if (window.event) {
	    window.event.cancelBubble = true;
	    window.event.returnValue = false;
	  }
	  if (e && e.preventDefault && e.stopPropagation) {
	    e.preventDefault();
	    e.stopPropagation();
	  }
  }
}
var hashTarget = 0;
function dtas_scrollToHash(hash) {
  // Find the <a name> tag corresponding to this href
  // First strip off the hash (first character)
   
  if(typeof(hash)=="undefined")
  {
  	hash= "";
  }
  
  anchor = hash.substr(1);
//  if (anchor=="dtas_1") anchor = "dtas_top";
  // Now loop all A tags until we find one with that name
  var allLinks = document.getElementsByTagName('a');
  var destinationLink = null;
  for (var i=0;i<allLinks.length;i++) {
    var lnk = allLinks[i];
    if (lnk.name && (lnk.name == anchor)) {
      destinationLink = lnk;
      break;
    }
  }

  // If we didn't find a destination, give up and let the browser do
  // its thing
  if (!destinationLink) {
  	if (anchor=="dtas_top" && document.body) {
		destinationLink = document.body;
	} else {
	  	return true;
	}
  }
	hashTarget = destinationLink;
  // Find the destination's position
  var destx = destinationLink.offsetLeft;
  var desty = destinationLink.offsetTop;
  var thisNode = destinationLink;
  var hiddenNodes = new Array();
  while (thisNode.parentNode &&
        (thisNode.parentNode != document.body)) {
	if (thisNode.style.display == 'none') {
		hiddenNodes[hiddenNodes.length] = thisNode;
		thisNode.style.display = '';
	}
    thisNode = thisNode.parentNode;
  }
  thisNode = destinationLink;
  while (thisNode.parentNode &&
        (thisNode.parentNode != document.body)) {
    thisNode = thisNode.parentNode;
    destx += thisNode.offsetLeft;
    desty += thisNode.offsetTop;
  }
  var lastEffect = null;
  for (var i=0;i<hiddenNodes.length;i++) {
  	hiddenNodes[i].style.display = 'none';
	// custom id for ThrombosisAdviser
	if (hiddenNodes[i].id='references') {
		effectScrollToRefs = new Effect.ScrollTo($('referenceHeader'), {queue: { position: 'end', scope: 'references'}});
		lastEffect = effectScrollToRefs;
		if (!$('referenceLink').hasClassName('on')) {
			$('referenceLink').addClassName('on');
			$('references').showNext = destinationLink;
			effectDisplayRefs = new Effect.BlindDown($('references'), {queue: { position: 'end', scope: 'references'},
				afterFinish:function() {
			  		smoothScrollToAnchor($('references').showNext);
				}
			});
			lastEffect = effectDisplayRefs;
		}
	}
  }
  if (!lastEffect && destinationLink) smoothScrollToAnchor(destinationLink);
}

function smoothScrollToAnchor(destinationLink) {
	var effectScrollToAnch = new Effect.ScrollTo($(destinationLink), {queue: { position: 'end', scope: 'scroll'}});
	var effectHighlight = new Effect.Highlight($(destinationLink).parentNode, {queue: { position: 'end', scope: 'scroll'}});
}

function dtas_scrollWindow(scramount,dest,anchor) {
  wascypos = dtas_getCurrentYPos();
  isAbove = (wascypos < dest);
  dtas_scrolled=true;
  window.scrollTo(0,wascypos + scramount);
  iscypos = dtas_getCurrentYPos();
  isAboveNow = (iscypos < dest);
  if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
	dtas_scrolled=true;
    window.scrollTo(0,dest);
    clearInterval(dtas_INTERVAL);
	new Effect.Highlight(hashTarget.parentNode);
  }
}

function dtas_getCurrentYPos() {
  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 dtas_addEvent(elm, evType, fn, useCapture) {
	// addEvent and removeEvent
	// cross-browser event handling for IE5+,  NS6 and Mozilla
	// By Scott Andrew
	if (elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent){
		var r = elm.attachEvent("on"+evType, fn);
		return r;
	} else {
//		alert("Handler could not be removed");
	}
}

document.observe('dom:loaded', function() {
	dtas_scrollOnPageLoad();
	dtas_updateLinks();
});
document.observe('scroll', function() {
	dtas_stopScroll();
});

