/*
	Concept-Oriented Reading Instruction Scripts
	Developed by Noah Lazar -- noahlazar.com
*/


// 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; }
	}
}

// Set hover state on navigation tabs in IE (which does not support the hover selector)
function navHover() {
	if (document.all && document.getElementById('navigation')) {
		var tabs = document.getElementById('navigation').getElementsByTagName("LI");

		for (var i = 0; i < tabs.length; i++) {
			var thisTab = tabs.item(i);

			// Set hover class based on active state
			if (thisTab.className == "active") {
				thisTab.onmouseover = function() { this.className = "hover active"; }
				thisTab.onmouseout = function() { this.className = "active"; }
			} else {
				thisTab.onmouseover = function() { this.className = "hover"; }
				thisTab.onmouseout = function() { this.className = ""; }
			}
		}
	}
}

// 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");

			for (var j = 0; j < rows.length; j++) {
				evenodd = (j % 2 == 0) ? "odd" : "even";

				// Insert/append new class name
				rows[j].className += (rows[j].className == null) ? evenodd : (rows[j].className == "odd" || rows[j].className == "even") ? "" : " " + evenodd;
			}
		}
}



// 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', clearSearch);
	addEvent(window, 'load', navHover);
	addEvent(window, 'load', tableRows);
}
