/*
	Identifies external links on a page by comparing them to an internalURL parameter passed in.
	Adjusts these links so that they will open in a new window/tab when clicked. Will open all external links in the same window/tab until that window/tab is closed.
*/
function markExternalLinks(internalURL) {
	if (!document.getElementsByTagName) return;

	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.href && anchor.href.substr(0,internalURL.length) != internalURL) {
			// Make sure that the "external" link is not actually opening a lightwindow within the page.
			var classes = anchor.className;
			if (classes.indexOf("gallery") == -1 && classes.indexOf("lightwindow") == -1) {
				anchor.onclick = openExternalLink;
			}
		}
	}
}

function openExternalLink() {
	window.open(this.href,"externalToSite");
	return false;
}

//LiveFilter creates a live search into an li-style list of items
//you can put a <span> (probably of class keyword) into the list to expand what matches a line
//create instance after page loads; for example, <body onload="filterer = new LiveFilter('ID OF LIST', 'ID OF FORM CONTAINER', 'NAME OF VARIABLE')">
//where you want the form to be, put a div of the specified ID; the form itself will have the ID search_CONTAINERID
function LiveFilter(listID, formID, filterVariableName) {
	//function to make the form for the live filter
	this.makeForm = function(formID, filterVariableName) {
		var encloser = document.getElementById(formID);
		encloser.innerHTML = '<form onsubmit=\"return false;\" action=\"\"><p><strong><label for=\"search_' + formID + '\">Search:</label></strong> <input type=\"text\" id=\"search_' + formID + '\" onkeyup=\"' + filterVariableName + '.filterList(this, event);\" /></p></form>';
	}

	//actually make the form (for browsers that do not have JavaScript disabled)
	this.makeForm(formID, filterVariableName);

	//initialize by storing the full list for future filtering
	this.haystack = document.getElementById(listID);

	//convert the NodeList to the two kinds of innerHTML we need to use
	//strawItems is the innerHTML of each <li> list item
	//strawText is the innerHTML of each <a> tag (the text being searched)
	var straw = this.haystack.getElementsByTagName('li');
	this.strawText = new Array();
	this.strawItems = new Array();
	this.strawClasses = new Array();
	for (var i=0; i<straw.length; i++) {
		var needle = straw[i];
		this.strawItems.push(needle.innerHTML);

		//needle.textContent would be better, but it isn't well supported
		var needleText = needle.innerHTML.replace(/<[^>]*>/g, '');
		this.strawText.push(needleText);

		//store the classes, too
		var needleClass = needle.className;
		this.strawClasses.push(needleClass);
	}

	this.itemHTML = function(itemIndex) {
		var needleHTML = this.strawItems[itemIndex];
		var needleClass = this.strawClasses[itemIndex];
		if (needleClass) {
			var listItemStart = '<li class="' + needleClass + '">';
		} else {
			var listItemStart = '<li>';
		}

		return listItemStart + needleHTML + '</li>';
	}

	this.filterList = function(textField, event) {
		//escape key (27), clear key (12) clears the text box
		if (event.which == 27 || event.which == 12) {
			textField.value = '';
		}

		//filter the full list into needles if there is searchText
		//otherwise, restore the full list if they have cleared their searchText
		var searchText = textField.value;
		var needles = new Array();
		var numMatches = 0;
		var resultsMsg = document.getElementById('results_message');

		if (searchText) {
			searchText = new RegExp(searchText, 'i');

			for (var i=0;i<this.strawText.length;i++) {
				var needleText = this.strawText[i];

				if (needleText.match(searchText)) {
					needles.push(this.itemHTML(i));
					numMatches++;
				}
			}
		} else {
			for (var i=0;i<this.strawItems.length;i++) {
				needles.push(this.itemHTML(i));
			}
		}

		this.haystack.innerHTML = needles.join("\n");

		//display a "no results" message when the user has entered text in the search box, but there are no matching results
		if (numMatches == 0 && searchText) {
			resultsMsg.innerHTML = 'There are no results that match your search. Try to delete some of the characters you entered to broaden your search.';
		} else {
			resultsMsg.innerHTML = '';
		}
	}
}

