/*
	gallery.js
	11/13/07 Mark Esterly
*/

// -------- INIT GLOBAL VARIABLES --------

// NOTE: change this global base url when launching the site
var urlBase = "http://localhost/~mark/staging/markesterly/photos/";

var desc = "";

// window.onload = initAll();

// ---------------- FUNCTIONS ----------------

//----------------
// <body> onload calls initAll() to preload the content
//----------------
function initAll()	{

	// display information about the default project
	makeRequest(urlProj("proj"), "projDescription");

	// display the thumbnail images for the default project
	makeRequest(urlBase + "proj_thumbs_mich.php", "mainContent");

}

//----------------
// use <select>.value to build a filename
//----------------
function urlProj(selectID)	{

	var tmp = document.getElementById(selectID);
	var pageName = tmp.options[tmp.selectedIndex].value;
	url = urlBase + "proj_" + pageName + ".htmlf";

//	alert ("DEBUG: urlProj -- pageName,url: [" + pageName + "], [" + url + "]");

	return url;
}

//----------------
// 
//----------------
function makeRequest (url,destID)	{

	var httpRequest;	// NOTE: local variable to avoid race condition
	
//	alert ("DEBUG: makeRequest -- url,destID: [" + url + "], [" + destID + "]");

	if (window.XMLHttpRequest)	{
		httpRequest = new XMLHttpRequest();
	}
	else	{
		if (window.ActiveXObject)	{
			try	{
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)	{ }
		}
	}

	if (httpRequest)	{

//		alert ("DEBUG: makeRequest -- httpRequest=TRUE");

		httpRequest.onreadystatechange = function() { displayIt(httpRequest,destID); };
		httpRequest.open("GET", url, true);
		httpRequest.send("");
	}
	else	{

//		alert ("DEBUG: makeRequest -- httpRequest=FALSE");

		document.getElementById(destID).innerHTML = "Error creating XMLHTTPRequest for: " + url;
	}
}

//----------------
// display the file from the server
//----------------
function displayIt(httpRequest,destID) {

	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
//			alert(httpRequest.responseText);
			document.getElementById(destID).innerHTML = httpRequest.responseText;
		} else {
			alert('There was a problem with the request.');

		}
	}
}

//----------------
function hiliteProj(selectID)	{

	var tmp = document.getElementById(selectID);
	var projName = tmp.options[tmp.selectedIndex].value;

//	alert ("DEBUG: hiliteProj: projName: '" + projName + "'");

	// de-highlight all images
	unHiliteAllImgs();
	// highlight images with this project name
	hiliteImgs(projName);

	return;
}

//----------------
function unHiliteAllImgs()	{

	var imgs = document.getElementsByTagName("img");	// FIX -- selects ALL images on page
	for (var i = 0; i <  imgs.length; i++)	{
		var img = imgs[i];
		img.style.display = "none";
	}
	// hide all named project description divs
	var divs = document.getElementsByName("desc");
	for (var i = 0; i < divs.length; i++)	{
		var div = divs[i];
		div.style.display = "none";
	}
}

//----------------
function hiliteImgs(imgName)	{

//	alert ("DEBUG: hiliteImgs: imgName: '" + imgName + "'");

	if(imgName !== "all")	{
		var imgs = document.getElementsByName(imgName);
	}
	else	{
		var imgs = document.getElementsByTagName("img");	// FIX -- selects ALL images on page
	}
	for (var i = 0; i < imgs.length; i++)	{
		var img = imgs[i];
		img.style.display = "block";
		img.style.visibility = "visible";
	}
	// display the proper project description (unless "all")
	if(imgName !== "all")	{
		desc = document.getElementById("desc_" + imgName);
		desc.style.display = "block";
	}
}

//------------------------------------------------------
// reset the <select> option's initial value for Firefox
//------------------------------------------------------
function selectThis()	{

//	alert ("DEBUG: resetSelect: value: '" + value + "'");

	this.selected = "selected";
}
