// JavaScript Document
//<![CDATA[

function libsearch(query)
{
	// clear any old search results
	document.getElementById("ajaxresults").innerHTML = '&nbsp;';

	/* MAKE THAT URL */
	// begin
	url = 'http://opencoconut.com/includes/libsearch.php?query=';
    // separate terms of passed variable, query, by + signs
    urlarray = query.split(' ');
	// append to url the string of + separated values
	for (var i = 0; i < urlarray.length; i++)
	{
		url += urlarray[i];
		if (i != urlarray.length - 1)
			url += '+';
	}
	
	/* PREPARE FOR AJAX REQUEST */
	// instantiate XMLHttpRequest object
	try
	{
		request = new XMLHttpRequest();
	}
	catch (e)
	{
		request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	// handle old browsers
	if (request == null)
	{
		alert("Ajax not supported by your browser, upgrade!");
	}
	
	// show progress
	document.getElementById("progress").style.display = "block";
	
	/* RUN THE QUERY */
	request.onreadystatechange = handler;
	request.open("GET", url, true);
	request.send(null);
}

function handler ()
{
	// xhtml
	xhtml = "";

	// only handle request in "loaded" state
	if (request.readyState == 4)
	{
		// ensure request succeeded 
		if (request.status == 200)
		{
			// stop the loading image
			document.getElementById("progress").style.display = "none";
			
			// begin processing XML document
			var videos = request.responseXML.getElementsByTagName("dvd");
			
			xhtml = '<br /><h1>search results</h1>';
			
			/*if (request.responseXML.getElementsByTagName("hulu") != null)
			{
				var hulu = request.responseXML.getElementsByTagName("hulu");
				var hulutag = hulu[0];
				var huluurl = hulu.getAttribute("videolink");
				xhtml += '<h2>Try searching hulu.com: <a href="' + huluurl + '">' + query + '</a></h2>';
			}*/
			
			// iterate over dvds
			for (var i = 0; i < videos.length; i++)
			{
				// encode city information
				var video = videos[i];
				
				// encode the city location
				var title = video.getAttribute("title");
				var hollislink = video.getAttribute("link");
				var found = video.getAttribute("found");
				var missing = video.getAttribute("missing");

				// grab current city's set of articles
				var copies = video.getElementsByTagName("copy");
				
				// append current dvd result and title
				xhtml += '<div class="result">' + '<div class="result-message">' + '<img src="images/reel.png" />';
				xhtml += '<h2>' + title + '</h2></div>';
				xhtml += '<div class="result-tools"><ul>';
				// missing
				var copys = (missing == 1) ? 'copy' : 'copies';
				if ( missing == 1)
					xhtml += "<li><img src='images/gears.png' />'" + missing + " missing " + copys + "</li>";
				if ( missing > 1)
					xhtml += "<li><img src='images/gears.png' />" + missing + " missing " + copys + "</li>";
				// found
				var copys = (found == 1) ? ' copy' : ' copies';
				if ( found == 1)
					xhtml += "<li><img src='images/gem.png' />" + found + " " + copys + " found</li>";
				if ( found > 1)
					xhtml += "<li><img src='images/gem.png' />" + found +  " " + copys + " found</li>";
				// for each copy
				for (var j = 0; j < copies.length; j++)
				{
					var copy = copies[j];
					xhtml += '<li><img src="images/library.png" />' +  " " + copy.getAttribute("location") + '</li>';
					// open late
					if (copy.getAttribute("openlate") != null)
						xhtml += '<li><img src="images/moon.png" />' + ' library open late' + '</li>';
					// reserve item
					if (copy.getAttribute("loantype") != null)
						xhtml += '<li><img src="images/hourglass.png" />' + copy.getAttribute("loantype") + '</li>';
				}
				// links
				xhtml += '<li><img src="images/hourglass.png" /><a href="' + hollislink + '">Request online @hollis</a></li>';
				
				// end unordered list
				xhtml += "</ul></div></div>";
			}// closes dvd loop
			
			document.getElementById("ajaxresults").innerHTML = xhtml;
		}
		else
			alert ("AJAX error -> HTTP error: " + request.readyState);
	}

}
//]]>