function initialize() {
	// build arrays of the sections and pages from the menu and submenu content
	divs = document.getElementsByTagName("div");
	for (i=0; i<divs.length; i++) {
		name = divs[i].id;
		name_parts = name.split("_");
		if (name_parts[0] == "menu") {
			add_to_array("sections", name_parts[1]);
		} else if (name_parts[0] == "submenu") {
			add_to_array("section_" + name_parts[1] + "_pages", name_parts[2]);
		}
	}
	sections_count = sections.length;
	
	// make sure there is at least one page listed for each section; if not, add a default page (e.g. Contact)
	for (i=0; i<sections.length; i++) {
		eval("array_exists = isset('section_" + sections[i] + "_pages', 'object')");
		if (!array_exists) {
			add_to_array("section_" + sections[i] + "_pages", sections[i]);
		}
	}
	
	// assemble the section content so it's ready to place into the selected rows
	for (i=0; i<sections.length; i++) {
		add_to_array("section_content", build_section(sections[i]));
	}
	
	// initialize some variables
	row_open = 0;
	photos_open = new Array;
}

function click_row(row) {
	// close the previous set of rows
	if (row_open) {
		close_row(row_open);
		clear_content("row_" + row_open);
		for (i=0; i<sections.length; i++) {
			close_row(row_open + i + 1);
			clear_content("row_" + (row_open + i + 1));
		}
	}
	
	// open the clicked row and load the sections into the subsequent rows
	// note that passing a row of 0 will effectively close all the rows
	if (row) {
		if ((row + sections_count) > row_count) { row = row_count - sections_count; } // don't start with a row that is too far down to accommodate all the sections
		open_row(row);
		add_content("row_" + row, get_content("title"));
		for (i=0; i<sections.length; i++) {
			open_row(row + i + 1);
			add_content("row_" + (row + i + 1), section_content[i]);
		}
	}
	
	// remove the extra IE6 hover events and then rebuild them
	if (navigator.appVersion.indexOf("MSIE 6") != -1) {
		for (i=0; i<document.styleSheets.length; i++) {
			for (j=(document.styleSheets[i].rules.length - 1); j>=0; j--) {
				if (document.styleSheets[i].rules[j].selectorText.indexOf("onhover") != -1) {
					document.styleSheets[i].removeRule(j);
				}
			}
		}
		document.body.style.behavior = "url(scripts/blank.htc)";
		document.body.style.behavior = "url(scripts/csshover.htc)";
	}

	row_open = row;
}

function click_section(section) {
	// show or hide the selected section
	if (document.getElementById("section_" + section).style.display == "none") {
		document.getElementById("section_" + section).style.display = "block";
	} else {
		document.getElementById("section_" + section).style.display = "none";
		eval("pages = section_" + section + "_pages;");
		for (i=0; i<pages.length; i++) {
			// when hiding a section, also hide all the pages within a section
			document.getElementById("page_" + section + "_" + pages[i]).style.display = "none";
		}
	}
}

function click_page(section_page) {
	// show or hide the selected page
	if (document.getElementById("page_" + section_page).style.display == "none") {
		document.getElementById("page_" + section_page).style.display = "block";
	} else {
		document.getElementById("page_" + section_page).style.display = "none";
	}
}

function click_photo(section_page, filename, width, height) {
	// show or hide the photo div, and load the selected photo into it
	if ((document.getElementById("photo_" + section_page).style.display == "none")||(photos_open[section_page] != filename)) {
		document.getElementById("photo_" + section_page).innerHTML = build_photo(section_page, filename, width, height);
		document.getElementById("photo_divider_" + section_page).style.display = "block";
		document.getElementById("photo_" + section_page).style.display = "block";
	} else {
		document.getElementById("photo_divider_" + section_page).style.display = "none";
		document.getElementById("photo_" + section_page).style.display = "none";
	}
	photos_open[section_page] = filename;
}

function open_row(row) {
	document.getElementById("row_" + row).className = "row row_" + row + " row_open";
	document.getElementById("row_" + row).onclick = "";
}

function close_row(row) {
	document.getElementById("row_" + row).className = "row row_" + row + " row_" + row + "_hover";
	document.getElementById("row_" + row).onclick = function() { click_row(row); };
}

function get_content(ID) {
	content = document.getElementById(ID).innerHTML;
	return content;
}

function add_content(ID, content) {
	document.getElementById(ID).innerHTML += content;
}

function clear_content(ID) {
	document.getElementById(ID).innerHTML = "";
}

function build_section(section) {
	output = "";
	output += get_content("menu_" + section);
	output += "<div style=\"display: none;\" id=\"section_" + section + "\">\n"; // this starts out invisible, until we click the section
	eval("pages = section_" + section + "_pages;");
	if (pages.length == 1) {
		// if the section only has one page, just show it immediately
		// this doesn't support photos, which would require using build_page but passing in a "visible" argument
		output += get_content("content_" + section + "_" + pages[0]);
	} else {
		for (j=0; j<pages.length; j++) {
			output += build_divider();
			output += get_content("submenu_" + section + "_" + pages[j]);
			output += build_page(section + "_" + pages[j]);
		}
	}
	output += "</div>\n\n";
	return output;
}

function build_page(section_page) {
	output = "";
	output += "<div style=\"display: none;\" id=\"page_" + section_page + "\">\n"; // this starts out invisible, until we click the page
	output += get_content("content_" + section_page);
	output += "<div style=\"display: none;\" class=\"photo_divider\" id=\"photo_divider_" + section_page + "\"></div>\n"; // this starts out invisible, until we open the first photo
	output += "<div style=\"display: none;\" class=\"photo\" id=\"photo_" + section_page + "\"></div>\n"; // this starts out invisible, until we open the first photo
	output += "</div>\n\n";
	return output;
}

function build_photo(section_page, filename, width, height) {
	output = "";
	output += "<a href=\"JavaScript:click_photo('" + section_page + "', '" + filename + "', " + width + ", " + height + ");\">";
	output += "<img src=\"" + filename + "\" width=\"" + width + "\" height=\"" + height + "\" border=\"0\">";
	output += "</a>";
	return output;
}

function build_divider() {
	return "<div class=\"page_divider\"></div>\n";
}

// add a value to an array, creating the array first if it does not already exist
function add_to_array(array_name, value) {
	eval("array_exists = isset('" + array_name + "', 'object');");
	if (!array_exists) {
		eval(array_name + " = new Array();");
	}
	eval("value_index = get_position(value, " + array_name + ");");
	if (value_index == -1) {
		eval(array_name + ".push(value);");
		return 1;
	} else {
		// if the value was already in the array, return false
		return 0;
	}
}

// remove an element from an array
function remove_from_array(array_name, value) {
	eval("value_index = get_position(value, " + array_name + ");");
	if (value_index > -1) {
		eval(array_name + ".splice(value_index, 1);");
		return 1;
	} else {
		// if the value wasn't in the array, return false
		return 0;
	}
}

// return the index of an array element; return -1 if not present
function get_position(string, array) {
	for (n=0; n < array.length; n++) {
		if (array[n] == string) {
			return n;
			break;
		}
	}
	return -1;
}

// show an email link on a page in a way that spam harvesters can't see
function show_email(user, domain, tld, label) {
	if (!label) { label = user + "@" + domain + "." + tld; }
	document.write("<a href='mailto:" + user + "@" + domain + "." + tld + "'>" + label + "<\/a>");
}

// figure out whether a variable has been set or not without generating an undefined error if it hasn't
// possible types are "undefined", "object", "boolean", "number", "string" or "function", with everything else being "object"
function isset(variable, type) {
	if (type) {
		eval("result = (typeof(" + variable + ") == type)");
	} else {
		eval("result = (typeof(" + variable + ") != 'undefined')");
	}
	return result;
}
