function getHTTPObject() {
	var xmlhttp;
	// native
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest();
	}
	// Windows IE
	else if (window.ActiveXObject) {
    	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   	} else { 
   		return false;
   	}
   	return xmlhttp;
}

var http = getHTTPObject();
var xmlDoc;

http.onreadystatechange = function() {
	if(http.readyState == 4) {
  		if(http.status == 200) {
   			xmlDoc = http.responseXML;
			if (navigator.userAgent.indexOf("Firefox") == -1) displayMenus(null);
  		}
 	}
}
http.open("GET", "gallery.xml");
http.send(null);


var curLevel = 1;
// findSubject(which) - sorts through all subjects in xmlDoc
// and finds the one specified
// ---
// which - string specifying desired subject node
function findSubject(which) {
	var subjects = xmlDoc.getElementsByTagName("subject");
	for(i = 0; i < subjects.length; i++) {
		var type = subjects[i].getElementsByTagName("type")[0].firstChild.nodeValue;
		// if the current node's attribute is the same as
		// the parameter passed to the function
		if (type == which) {
			// this is the correct node, set curNode to this node
			// and break for loop
			var curNode = subjects[i];
			break;
		}
	}
	return curNode;
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
  		c_start=document.cookie.indexOf(c_name + "=");
  		if (c_start!=-1) { 
    		c_start=c_start + c_name.length+1; 
    		c_end=document.cookie.indexOf(";",c_start);
    		if (c_end==-1) c_end=document.cookie.length;
    		return unescape(document.cookie.substring(c_start,c_end));
    	} 
  	}
	return "";
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" + escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

// displayMenus(which) - displays drop down menus by sorting through
// xml file and building new select tags based on previous selections
// ---
// which - string denoting name of category chosen
function displayMenus(which) {
	// get parent element for selects
	var menu = document.getElementById("menu");
	var subjects = xmlDoc.getElementsByTagName("subject");
	// if no parameter was passed (body's onload="display(null)")
	// this is the first time, create the first menu
	if(!which) {
		var lastCat=getCookie('lastCat');
		if (lastCat!=null && lastCat!="") {
			menu.appendChild(document.createTextNode("Welcome back, when you left, you were viewing the " + lastCat + " category."));
			menu.appendChild(document.createElement("br"));
			menu.appendChild(document.createElement("br"));
		}
		// create basic select and set properties
		var newSelect = menu.appendChild(document.createElement("select"));
		newSelect.setAttribute("onchange","displayMenus(this.value)");
		var opt = newSelect.appendChild(document.createElement("option"));
		opt.setAttribute("value","null");
		opt.appendChild(document.createTextNode("--choose an option--"));
		// create options
		for (i = 0; i <= subjects.length - 1; i++) {
			if(subjects[i].getElementsByTagName("level")[0].firstChild.nodeValue == 1) {
				// create options
				var option = newSelect.appendChild(document.createElement("option"));
				option.setAttribute("value",subjects[i].getElementsByTagName("type")[0].firstChild.nodeValue);
				option.appendChild(document.createTextNode("" + subjects[i].getElementsByTagName("type")[0].firstChild.nodeValue));
			}
		}
	} else {
		var curSub = findSubject(which);
		// if curSub is defined
		if (curSub) {
			// create subsubjects array
			var subsubjects = curSub.getElementsByTagName("subject");
		}
		// if requested subject is lower in the hierarchy than
		// the menu that is currently displayed...
		if (curSub.getElementsByTagName("level")[0].firstChild.nodeValue < curLevel) {
			// remove all excess menus
			for (i = 0; i <= (curLevel - curSub.getElementsByTagName("level")[0].firstChild.nodeValue); i++) {
				try {
					menu.removeChild(menu.lastChild);
				} catch(e) {
					alert("you broked it");
				}
				// correct current menu level
				curLevel--;
			}
		}
		
		if (subsubjects.length > 0) {
			// create basic select and set properties
			var newSelect = menu.appendChild(document.createElement("select"));
			newSelect.setAttribute("onchange","displayMenus(this.value)");
			var opt = newSelect.appendChild(document.createElement("option"));
			opt.setAttribute("value","null");
			opt.appendChild(document.createTextNode("--choose an option--"));
				
			for (i = 0; i < subsubjects.length; i++) {
				if(curSub.getElementsByTagName("level")[0].firstChild.nodeValue == subsubjects[i].getElementsByTagName("level")[0].firstChild.nodeValue - 1) {
					// create options
					var option = 	newSelect.appendChild(document.createElement("option"));
					option.setAttribute("value",subsubjects[i].getElementsByTagName("type")[0].firstChild.nodeValue);
					option.appendChild(document.createTextNode("" + subsubjects[i].getElementsByTagName("type")[0].firstChild.nodeValue));
				}
			}
			curLevel++;
		}
	}
	// call function to display images
	showImages(which);
	if (which) {
		setCookie("lastCat",which,5);
	} else {
		setCookie("lastCat","initial",5);
	}
}

// showImages(which) - displays images from xml file
// depending on menu option selected
// ---
// which - string denoting name of category chosen
function showImages(which) {
	// retrieve and store parent element for images
	var imageDiv = document.getElementById("images");
	// retrieve and store all currently displayed images
	var curImages = document.getElementsByTagName("span");
	// remove all currently displayed images
	while (curImages.length > 0) {
		try {
			imageDiv.removeChild(curImages[curImages.length-1]);
		} catch(e) {
			break;
		}
	}
	// if parameter is defined...
	if (which) {
		// go through subjects array to check if the parameter is
		// one of its elements
		var curSubject = findSubject(which);
		// retrieve and store images to be displayed
		// based on the subject requested
		var images = curSubject.getElementsByTagName("image");
	// if parameter is not defined, all images
	} else {
		// retrieve and store images to be displayed
		var images = xmlDoc.getElementsByTagName("image");
	}
	
	// create html gallery markup
	for (i = 0; i < images.length; i++) {
		// new image div
		var newDiv = imageDiv.appendChild(document.createElement("span"));
		newDiv.setAttribute("id","image");
		
		// new img inside image div
		var newImage = newDiv.appendChild(document.createElement("img"));
		newImage.setAttribute("src",images[i].getElementsByTagName("url")[0].firstChild.nodeValue);
		
		// new p tag for image info
		var newP = newDiv.appendChild(document.createElement("p"));
		newP.appendChild(document.createTextNode(images[i].getElementsByTagName("title")[0].firstChild.nodeValue + " - " + images[i].getElementsByTagName("location")[0].firstChild.nodeValue));
	}
}