//	---------------------------------------------------------------------------
//	FUNCTION:	anchor()
//	AUTHOR:		Ryan J. Salva, http://www.capitolmedia.com
//	REVISED:	July 24, 2004
//
//	Searches document for hyperlinks that go outside the 
//	current domain, then updates those links with 
//	target="_blank"
//	
//	Also adds classes for documents: PDF, DOC and ZIP
//
//	TESTED ON:
//	Windows Firefox 1, IE 6, Opera 8, Netscape 6
//	Mac Safari 1, IE 5.2
//
//	REQUIRED ASSETS:
//	1.) utility.js for addLoadEvent(f) function
//	2.) stylesheet with classes A.PDF, A.ZIP, A.DOC
//
//	IMPLEMENTATION:
//	include this file in the header with utility.js and stylesheet

function anchor() {
	// for DOM compliant browsers
	if (document.childNodes)
	{
		var fileTypes = new Array('pdf','doc','zip','xls');
		var a = document.getElementsByTagName("A");
		for (i=0;i<a.length;i++)
		{
			// open anything not in the current domain in a new window
			if ((a[i].hostname != window.location.hostname) && (a[i].href.indexOf('javascript') == -1)) {
				a[i].setAttribute('target', '_blank'); 
				debug.info('setAttribute(target,_blank) to id='+a[i]+')');
			}

			// If the link is internal to the page (begins in #) 
			// then attach the smoothScroll function as an onclick event handler
			if ((a[i].href && a[i].href.indexOf('#') != -1) && ( (a[i].pathname == location.pathname) || ('/'+a[i].pathname == location.pathname) ) &&  (a[i].search == location.search)) { 
				addEvent(a[i],'click',smoothScroll); 
				debug.info('attach onclick(smoothScroll) to id='+a[i]+')');
			} 

			// search each anchor for a supported file extension
			for(x=0;x<fileTypes.length;x++) {
				ua = a[i].href.toUpperCase();
				ux = fileTypes[x].toUpperCase()
				if (ua.indexOf('.' + ux) != -1) {
					// if it matches, open in a new window and set the class equal to the file extension
					a[i].setAttribute('target', '_blank');
					a[i].className+=(a[i].className.length>0? " ": "") + fileTypes[x].toUpperCase();
					debug.info('set className='+fileTypes[x].toUpperCase()+' to id='+a[i]+')');
				}
			}
		}
	}
}
addLoadEvent(function(){anchor();});

var ss_INTERVAL; 
var ss_STEPS = 25;

function smoothScroll(e) { 

	// This is an event handler; get the clicked on element, 
	// in a cross-browser fashion 
	if (window.event) { 
		target = window.event.srcElement; 
	} else if (e) { 
		target = e.target; 
	} else return; 
  
	// Make sure that the target is an element, not a text node 
	// within an element 
	if (target.nodeType == 3) { 
		target = target.parentNode; 
	} 
	  
	// Paranoia; check this is an A tag 
	if (target.nodeName.toLowerCase() != 'a') return; 
  
	// Find the <a name> tag corresponding to this href 
	// First strip off the hash (first character) 
	anchor = target.hash.substr(1); 
	
	// 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) return true; 
	
	// Find the destination's position 
	var destx = destinationLink.offsetLeft;  
	var desty = destinationLink.offsetTop; 
	var thisNode = destinationLink; 
	while (thisNode.offsetParent && (thisNode.offsetParent != document.body)) { 
		thisNode = thisNode.offsetParent; 
		destx += thisNode.offsetLeft; 
		desty += thisNode.offsetTop; 
	}
  
	// Stop any current scrolling 
	clearInterval(ss_INTERVAL); 
	cypos = ss_getCurrentYPos(); 
	ss_stepsize = parseInt((desty-cypos)/ss_STEPS); 
	ss_INTERVAL = setInterval('ss_scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10); 

	// And 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(); 
	}

} 

function ss_scrollWindow(scramount,dest,anchor) { 
	wascypos = ss_getCurrentYPos(); 
	isAbove = (wascypos < dest); 
	window.scrollTo(0,wascypos + scramount); 
	iscypos = ss_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(ss_INTERVAL); 
		// and jump to the link directly so the URL's right 
		location.hash = anchor; 
	} 
} 

function ss_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;
}


