// JavaScript Document

var imagePath = '../images';

var xmlHttp;

// the XMLHttpRequest.responseXML object
var res;


// the set that we want to format and return
var requestedSet = " ";

// Takes a set name, and and url for the xml
/*

	USED ON LOCAL MACHINE WITH FIREFOX TO TEST

*/
function loadSetFromXML (sn, URL) {
	// set the global var to the set name we need to find
	requestedSet = sn;

	// EXCELLENT way of testing from within mozilla without needing a server response!
	xmlHttp = document.implementation.createDocument("", "", null);
	xmlHttp.onload = processRequestLocal;
	xmlHttp.load(URL);
}



// Takes a set name, and and url for the xml
function loadSetFromXMLRemote (sn, URL) {
	try {
		// Opera 8+, Safari, Mozilla
		xmlHttp = new XMLHttpRequest ();
	}
	catch (e) {
		try {
			// for IE
			xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert ("Your browser does not support AJAX.");
			}
		}
	}
	
	if (xmlHttp != null) {
		// set the global var to the set name we need to find
		requestedSet = sn;
		
		xmlHttp.onreadystatechange = processRequestRemote;
		xmlHttp.open ("GET", URL, true);
		xmlHttp.send (null);
	} else {
		alert ("Failed");
	}
}

function processRequestRemote () {
	if (xmlHttp.readyState == 4) {
		// load is complete
		if (xmlHttp.status == 200) {
			// it's okay
			res = xmlHttp.responseXML;
			processRequest ();
		} else {
			alert ("An unknown error occured contacting the XML: "+xmlHttp.status);
			return;
		}// end if status ok
	}// end if ready state == 4
}

function processRequestLocal () {
	res = xmlHttp;
	processRequest ();
}

function processRequest () {
	var collections = res.getElementsByTagName ("collection");
	// if the file has a 'collection' then continue.
	if (collections.length > 0) {
		// get an array of 'set's to search thru.
		var sets = collections[0].getElementsByTagName ("set");
	
		// loop thru the sets and find the one we like!!
		var found = false;
		var i=0;
		while ((i < sets.length) && (!found)) {
			if (sets[i].getAttribute ("title").toLowerCase () == requestedSet) {
				found = true;
			}
			i++;
		}
		// the value of 'i' will be 1 too large, so correct it.
		i--;
	
		if (found) {
			var thumbZoneObj = document.getElementById ("thumbZone");
			var instructions = document.createElement ("p");
			instructions.innerHTML = "CLICK IMAGE TO ENLARGE";
			
			thumbZoneObj.innerHTML = " ";
			thumbZoneObj.appendChild (instructions);
			
	
			// now process the images.
			var images = sets[i].getElementsByTagName ("image");
			for (n=0; n < images.length; n++) {
	
				var filename = images[n].getAttribute ("filename");
				//DEBUG: add some error checking here!!!
				var prefixsuffix = filename.split (".");
				
				var newAnchor = document.createElement ("a");
				newAnchor.setAttribute ('rel', 'lightbox[set]');
				newAnchor.setAttribute ('href', imagePath+'/full/'+filename);
				newAnchor.setAttribute ('class', 'cut_out');
				// For stupid IE
				newAnchor.setAttribute ('className', 'cut_out');
				
				//background: url(images/cutout.gif) no-repeat center #FFFFFF;
				//var imageRefIndex = 4;//Math.round(Math.random() * 2)+1;
				//DEBUG: Remove next line and re-implement comments
				//newAnchor.style.background = "#770000";//"#991818;"
				newAnchor.style.background = "url(../images/thumbbg.gif) bottom left repeat-x";
				//newAnchor.style.background = "url(../images/cutout"+imageRefIndex+".gif) no-repeat center transparent";
				newAnchor.style.position = "relative";
				if (images[n].getElementsByTagName("title").length > 0) {
					// might need error checking
					// grabs the CDATA section and extracts the text (data) from it.
					var ntitle = "";
					
					var titl = images[n].getElementsByTagName("title")[0];
					if (titl != null) {
						if (titl.firstChild.nodeType == 3 || titl.firstChild.nodeType == 4) {
							ntitle += titl.firstChild.nodeValue;
							// used for the hover tool tip
							newAnchor.setAttribute ('title', ntitle);
						}
					}
	
					var med = images[n].getElementsByTagName("medium")[0];
					
					if (med != null) {
						ntitle += "<br />";
						// Text Node or CDATA Section Node
						if (med.firstChild.nodeType == 3 || med.firstChild.nodeType == 4) { ntitle += med.firstChild.nodeValue; }
					}
					
					var dim = images[n].getElementsByTagName("dimensions")[0];
					if (dim != null) {
						ntitle += "<br />";
						// Text Node or CDATA Section Node
						if (dim.firstChild.nodeType == 3 || dim.firstChild.nodeType == 4) { ntitle += dim.firstChild.nodeValue; }
					}
				}
										
				
				var newImage = document.createElement ("img");
				
				//newImage.setAttribute ('src', 'images/thumb/template.jpg');
				newImage.setAttribute ('src', imagePath+'/thumb/'+prefixsuffix[0]+'_thumb.'+prefixsuffix[1]);
				newImage.setAttribute ('class', 'thumb_image');
				// For stupid IE
				newImage.setAttribute ('className', 'thumb_image');
				// DEBUG
				if (ntitle) { newImage.setAttribute ('alt', ntitle); }
				// END DEBUG
				//
				newImage.setAttribute ('width', '60px');
				newImage.setAttribute ('height', '60px');
				newAnchor.appendChild (newImage);
			
				thumbZoneObj.appendChild (newAnchor);
				
			}
			// now that elements are available, initialize the lightbox functionalities.
			//initLightbox ();
			new Lightbox ();
			
		} else {
			document.getElementById ("thumbZone").innerHTML = "<p>O snap! No data!</p>";
		}// end if found
	}// end if collection exists
}
