/*
	UM School of Languages, Literatures, and Cultures
	Updated 2/21/05
*/


// Generic popup window functions
function popup(URL, width, height) {
	popWin = window.open(URL, "popWin", "width=" + width + ",height=" + height + ",resizable=0,scrollbars=0,location=0,toolbar=0");
	popWin.focus();
	return false;
}

function popupResize(URL, width, height) {
	popWin = window.open(URL, "popWin", "width=" + width + ",height=" + height + ",resizable=1,scrollbars=1,location=0,toolbar=0");
	popWin.focus();
	return false;
}


// Clear search keywords
function clearSearch() {
	if (document.getElementById('keywords')) {
		document.getElementById('keywords').onfocus = function() {
			if (this.value == this.defaultValue) this.value = ""; }

		document.getElementById('keywords').onblur = function() {
			if (this.value == "") this.value = this.defaultValue; }
	}
}


// Alternate data table rows
function tableRows() {
	var evenodd, tables, rows;

	tables = document.getElementsByTagName("table");

	for (var i = 0; i < tables.length; i++) {

		// If a data table
		if (tables[i].className.indexOf("data") >= 0) {
			rows = tables[i].getElementsByTagName("tr");
			evenodd = "odd";

			for (var j = 0; j < rows.length; j++) {
				// Append new class name if needed
				rows[j].className += (rows[j].className == null) ? evenodd : " " + evenodd;

				// Alternate even/odd
				evenodd = (evenodd == "even") ? "odd" : "even";
			}
		}
	}
}


// Collapse calendar events (only with <dd><p class="details">)
function collapseEvents() {
	var lists, events, uncollapse, expand;

	var listnum = 1;
	lists = document.getElementsByTagName("dl");

	for (var i = 0; i < lists.length; i++) {

		// Look at thumbnail lists only
		if (lists[i].className.indexOf("events") >= 0) {
			events = lists[i].getElementsByTagName("p");

			for (var j = 0; j < events.length; j++) {

				// If a details paragraph
				if (events[j].className.indexOf("details") >= 0) {

					// Set ID
					events[j].id = "event" + listnum;

					// Create "uncollapse" link
					expand = document.createElement("a");
					expand.href = "#";
					expand.className = "expand";
					expand.id = "expand" + listnum;
					expand.appendChild(document.createTextNode("View Details"));
					expand.onclick = function() {
						var id = this.id.replace("expand", "");
						return show(this, 'event' + id);
					}

					// Add uncollapse link to end of list item
					events[j].parentNode.appendChild(expand);

					// Hide bio
					events[j].style.display = "none";

					listnum++;
				}
			}
		}
	}
}



// Collapse biographies on people pages (only with <p class="bio">)
function collapseBios() {
	var lists, bios, uncollapse, expand;

	var bionum = 1;
	lists = document.getElementsByTagName("ul");

	for (var i = 0; i < lists.length; i++) {

		// Look at thumbnail lists only
		if (lists[i].className.indexOf("thumbs") >= 0) {
			bios = lists[i].getElementsByTagName("p");

			for (var j = 0; j < bios.length; j++) {

				// If a biography paragraph
				if (bios[j].className.indexOf("bio") >= 0) {

					// Set ID
					bios[j].id = "bio" + bionum;

					// Create "uncollapse" link
					expand = document.createElement("a");
					expand.href = "#";
					expand.className = "expand";
					expand.id = "expand" + bionum;
					expand.appendChild(document.createTextNode("View Bio"));
					expand.onclick = function() {
						var id = this.id.replace("expand", "");
						return show(this, 'bio' + id);
					}

					// Add uncollapse link to end of list item
					bios[j].parentNode.appendChild(expand);

					// Hide bio
					bios[j].style.display = "none";

					bionum++;
				}
			}
		}
	}
}


// Show hidden/collapsed element (and hide 'expand' link)
function show(showLink, id) {
	document.getElementById(id).style.display = "block";
	showLink.style.display = "none";
	return false;
}



// Flyout menu functionality for IE
startList = function() {
	if (document.all && document.getElementById) {
		navRoot = document.getElementById("subnav");
		for (i=0; i < navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName == "LI") {
				node.onmouseover = function() {
					this.className += " over";
				}
				node.onmouseout = function() {
					this.className = this.className.replace(" over", "");
				}
			}
		}
	}
}


// Add function to any event handler (multibrowser)
function addEvent(obj, evType, fn) {

	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on" + evType, fn);
		return r;
	} else
		return false;
}

// Run all onload functions
if (document.getElementById) {
	addEvent(window, 'load', startList);
	addEvent(window, 'load', clearSearch);
	addEvent(window, 'load', tableRows);
	addEvent(window, 'load', collapseBios);
	addEvent(window, 'load', collapseEvents);
}