// Add new onload events to the window without overwriting any that may already exist
function addLoadEvent(f) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = f;
	}
	else {
		window.onload = function() {
			oldonload();
			f();
		}
	}
}

addLoadEvent(externalLinks);
addLoadEvent(addPopUps);
addLoadEvent(addTopLink);
addLoadEvent(setCurrentLink);

// Make external links open in new windows without using deprecated HTML to do it
function externalLinks() {
	// only proceed if getElementsByTagName is supported
	if (!document.getElementsByTagName) {
		return false;
	}
	// create an array of links
	var links = document.getElementsByTagName('a');
	// loop through each link
	for (var i=0;i<links.length;i++) {
		if (
			links[i].getAttribute("href") && ( 
			links[i].getAttribute("rel") == "external" || 
			links[i].getAttribute("rel") == "external nofollow" || 
			links[i].getAttribute("rel") == "nofollow external" )
			)
		links[i].target = "_blank";
	}
}

// Create popup windows that degrade gracefully
function addPopUps() {
	// only proceed if getElementsByTagName is supported
	if (!document.getElementsByTagName) {
		return false;
	}
	// create an array of links
	var links = document.getElementsByTagName('a');
	// loop through each link
	for (var i=0;i<links.length;i++) {
		if (links[i].getAttribute('type') == 'popup') {
			 //  add an onclick event to the link
			links[i].onclick = function () {
				openPopUp(this.href, this.className);
				return false;
			}
		}
	}
}

// Open popups in a set of standard sizes
function openPopUp(linkURL, linkClass) {
	var width;
	var height;
	switch(linkClass) {
		// 4 standard sizes to choose from
		case "S":
			width=300;
			height=300;
			break;
		case "M":
			width=500;
			height=500;
			break;
		case "L":
			width=700;
			height=700;
			break;
		case "XL":
			width=screen.width;
			height=screen.height;
			break;
		default:
			width=screen.width * .75;
			height=screen.height * .75;
			break;
	}
	var screenX = (screen.width/2 - width/2);
	var screenY = (screen.height/2 - height/2);
	var features= "width=" + width + ",height=" + height;
	// center the popup on the screen
	features += ",screenX=" + screenX + ",left=" + screenX;
	features += ",screenY=" + screenY  +",top=" + screenY;
	// disable most of the chrome for usability
	features += ",resizable=yes,";
	features += ",scrollbars=yes";
	
	var myWindow = window.open(linkURL, 'myWindow', features);
	// bring it to the front if it exists (it may have lost focus if it already existed)
	if (myWindow) {
        myWindow.focus();
     	return myWindow;
	}
}

// Add a "top of page" link if contents scroll vertically beyond viewport. Must have parent element named "top-of-page"
function addTopLink() {
	// only proceed if getElementById is supported
	if (!document.getElementById('top-of-page')) {
		return false;
	}
	if (document.documentElement.scrollHeight > document.documentElement.clientHeight) {
		document.getElementById('top-of-page').innerHTML = '<a href="javascript:scroll(0,0)" id="top">top of page</a>';
	}
}

function setCurrentLink() {
	// only proceed if getElementsByTagName is supported
	if (!document.getElementsByTagName) {
		return false;
	}
	// create an array of links
	var links = document.getElementsByTagName('a');
	var u = document.location.href;
	// loop through each link
	for (var i=0;i<links.length;i++) {
		if (links[i].href == u) {
			 //  assign a CSS class as a hook
			 links[i].className = "current";
		}
	}
}
