/*------------------------------------------------------------------------------
Functions:      footnoteLinks() / jsUtilities Library excerpts
Author:         Aaron Gustafson (aaron at easy-designs dot net)
                unless otherwise noted
Creation Date:  4 June 2005 / 8 May 2005
Version:        2.1 / 1.3
Homepage:       http://www.easy-designs.net/code/footnoteLinks/
License:        Creative Commons Attribution-ShareAlike 2.0 License
                http://creativecommons.org/licenses/by-sa/2.0/
Note:           If you change or improve on this script, please let us know by 
                emailing the author (above) with a link to your demo page.
------------------------------------------------------------------------------*/



// ---------------------------------------------------------------------
//                      array.push (if unsupported)
// ---------------------------------------------------------------------

if(Array.prototype.push == null) {
	Array.prototype.push = function(item) {
		this[this.length] = item;
		return this.length;
	};
};


// ---------------------------------------------------------------------
//                  function.apply (if unsupported)
//           Courtesy of Aaron Boodman - http://youngpup.net
// ---------------------------------------------------------------------

if (!Function.prototype.apply) {
	Function.prototype.apply = function(oScope, args) {
		var sarg = [];
		var rtrn, call;
		if (!oScope)
			oScope = window;
		if (!args)
			args = [];
		for (var i = 0; i < args.length; i++) {
			sarg[i] = "args["+i+"]";
		};
		call = "oScope.__applyTemp__(" + sarg.join(",") + ");";
		oScope.__applyTemp__ = this;
		rtrn = eval(call);
		oScope.__applyTemp__ = null;
		return rtrn;
	};
};


// ---------------------------------------------------------------------
//                               inArray()
//                           [Port from PHP]
//               Hunts for a value in the specified array
// ---------------------------------------------------------------------

function inArray(needle) {
	for (var i=0; i < this.length; i++) {
		if (this[i] === needle) {
			return i;
		}
	}
	return false;
}
Array.prototype.inArray = inArray;


// ---------------------------------------------------------------------
//                      lastChildContainingText()
//  finds the last block-level text-containing element within an object
// ---------------------------------------------------------------------

function lastChildContainingText() {
	var testChild = this.lastChild;
	var contentCntnr = ['p','li','dd'];
	while (testChild.nodeType != 1) {
		testChild = testChild.previousSibling;
	} 
	var tag = testChild.tagName.toLowerCase();
	var tagInArr = inArray.apply(contentCntnr, [tag]);
	if (!tagInArr && tagInArr!==0) {
		testChild = lastChildContainingText.apply(testChild);
	}
	return testChild;
}
Object.prototype.lastChildContainingText = lastChildContainingText;


// ---------------------------------------------------------------------
//                           footnoteLinks()
//        insert footnotes for the targets of links in a document
//            adapted by Lucas Sanders for the Scott Arboretum
// ---------------------------------------------------------------------

function footnoteLinks(containerID,targetID) {
	if (!document.getElementById || !document.getElementsByTagName || !document.createElement)
		return false;
	if (!document.getElementById(containerID) || !document.getElementById(targetID))
		return false;
	// back/forward buttons in IE/Mac preserve the content created by this script,
	// then run the script again when redisplaying the page.  let's not do that.
	if (document.getElementById('footnoteLinks'))
		return false;
	var container = document.getElementById(containerID);
	var target = document.getElementById(targetID);
	var div = document.createElement('div');
	div.id = 'footnoteLinks';
	var h4 = document.createElement('h4');
	var h4_txt = document.createTextNode('Links');
	h4.appendChild(h4_txt);
	var coll = container.getElementsByTagName('*');
	var ol = document.createElement('ol');
	var myArr = [];
	var thisLink;
	var offset = window.location.protocol.length + 2 + window.location.hostname.length;
	var num = 1;
	var linksFound = false;
	for (var i=0; i<coll.length; i++) {
		var thisClass = coll[i].className;
		if ( coll[i].getAttribute('href') || coll[i].getAttribute('cite') ) { 
			var currentPath = window.location.pathname;
			thisLink = coll[i].getAttribute('href') ? coll[i].href : coll[i].cite;
			if (thisLink.substring(offset, offset+currentPath.length+1) == currentPath + '#') {
				continue;
			}
			if (thisLink.substring(0,7) == "mailto:") {
				thisLink = thisLink.substring(7);
			}
			var note = document.createElement('sup');
			note.className = 'footnoteRef';
			var note_txt;
			var j = inArray.apply(myArr,[thisLink]);
			if ( j || j===0 ) {
				note_txt = document.createTextNode(j+1);
			} else {
				var li     = document.createElement('li');
				var li_txt = document.createTextNode(thisLink);
				li.appendChild(li_txt);
				ol.appendChild(li);
				myArr.push(thisLink);
				note_txt = document.createTextNode(num);
				num++;
			}
			note.appendChild(note_txt);
			if (coll[i].tagName.toLowerCase() == 'blockquote') {
				var lastChild = lastChildContainingText.apply(coll[i]);
				lastChild.appendChild(note);
			} else {
				coll[i].parentNode.insertBefore(note, coll[i].nextSibling);
			}
			linksFound = true;
		}
	}
	if (linksFound) {
		div.appendChild(h4);
		div.appendChild(ol);
		target.appendChild(div);
	}
	return true;
}



// ---------------------------------------------------------------------
//                               sfFocus()
//              removes the background image from querybox
//              while the user is inserting a search query
//
//           adapted by Lucas Sanders for the Scott Arboretum
//      based on http://www.htmldog.com/articles/suckerfish/hover/
// ---------------------------------------------------------------------

function sfFocus() {
	if (!document.getElementById) { return; }
	var querybox = document.getElementById("querybox");
	if (querybox.value == "") {
		querybox.className = "notEditing";
	} else {
		querybox.className = "whileEditing";
	}
	querybox.onfocus=function() {
		querybox.className = "whileEditing";
	}
	querybox.onblur=function() {
		if (querybox.value == "") {
			querybox.className = "notEditing";
		}
	}
}



// ---------------------------------------------------------------------
//                          fillPreviousQuery()
//           prefills querybox with the current search query
//
//           adapted by Lucas Sanders for the Scott Arboretum
//      based on http://www.htmldog.com/articles/suckerfish/hover/
// ---------------------------------------------------------------------

function fillPreviousQuery() {
	if (!document.getElementById) { return; }
	var querybox = document.getElementById("querybox");
	if (querybox.value.length > 0) { return; }  // don't clobber a user-defined value
	var queryString = window.location.search.substring(1);
	var vars = queryString.split("&");
	for (var i=0; i < vars.length; i++) {
		if (vars[i].substring(0,2) == "q=") {
			var words = vars[i].substring(2).split("+");
			var theString = words[0];
			for (var j=1; j < words.length; j++) {
				theString = theString + " " + words[j];
			}
			querybox.value = theString;
			querybox.className = "whileEditing";
		}
	}
}



// ---------------------------------------------------------------------
//                          e-mail link writer
// ---------------------------------------------------------------------

function scott_email() {
	if (!document.getElementsByTagName) { return; }
	var scott_uname = 'scott';
	var scott_email = scott_uname + unescape('%40') + 'swarthmore.edu';
	var scott_email_link = '<a href="mailto:' + scott_email + '">' + scott_email + '</a>';
	var all_spans = document.getElementsByTagName('span');
	for (var i=0; i < all_spans.length; i++) {
		if (all_spans[i].className == 'scott_email') {
			all_spans[i].innerHTML = scott_email_link;
		}
	}
}



// ---------------------------------------------------------------------
//                            onload handler
// ---------------------------------------------------------------------

window.onload = function() {
	if (document.getElementById && document.getElementById("fullYearNavScript")) {
			navlinksOnload();
	}
	scott_email();
	sfFocus();

	var bodyId = document.getElementsByTagName('body')[0].id;
	if (bodyId == 'search') {
		fillPreviousQuery();
	} else if (bodyId != 'printform') {
		footnoteLinks('content','content');
	}
};
