/*

	Utah Valley State College
	http://www.uvu.edu
	
	search guide javascript (ver. 1.0)
	
	
	
	
	Initial Developer:		Matt Chatterton
							Technical Support
							School of Business
							x 6167
	
	
	Testing runs:			WIN IE 6 & 7.0
							WIN Firefox 1.5 & 2.0
							WIN Opera 8.5 & 9.0
							MAC Firefox
							MAC Safari
	
	
	CHANGE LOG
	--------------
	
	ver (1.0)	Initial release
	
	
	
	CONCERNS
	--------------
	- Keyboard press performance on Mac browsers seems laggy, could possibly optimize onkeyup code
	- Guide's vertical positioning is based off "header2ndLevel"; workaround exists but... still
	
	
	OPTIONS
	--------------
	- A show/hide animation (instead of instant show/hide) would help with gui/user understanding



*/



var LOADED_search_guide_JS;



if(typeof LOADED_search_guide_JS == "undefined") {


	var uvGuide;
	
	
	// load the uvGuide array from js file
	if (document.getElementsByTagName) {
		try {
			var t=document.createElement("script");
			t.setAttribute("language", "JavaScript");
			t.setAttribute("type", "text/javascript");
			//t.setAttribute("src", "http://www.uvsc.edu/search/guide/uvguide.php");
			t.setAttribute("src", "http://www.uvu.edu/lib/php/livesearch.php");
			document.getElementsByTagName("head").item(0).appendChild(t);
			
		} catch (e_error) {}
	}
	// EXAMPLE:
	//	var uvGuide = new Array();
	//	uvGuide.push(new Array("Registration Tutorial", "http://www.uvsc.edu/registration/tutorial/", "az", "registration", "tutorial"));
	
	
	
	
	
	
	function findPos(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curleft,curtop];
	}
	
	
	
	
	// function when we click on a guide link
	// prepare hidden form by copying values, then submit
	// this allows us to log user click actions
	function goGuide(a) {
		try {
			// begin to hide guide (if the user presses 'back' it will be gone)
			hintGuideDisplay(false);
			
			var frm = document.getElementById("uvGuide_form");
			var frm_ttl = document.getElementById("uvG_title");
			var frm_hrf = document.getElementById("uvG_href");
			var frm_inp = document.getElementById("uvG_input");
			var frm_cur = document.getElementById("uvG_curr");
			var srch_in = document.getElementById("uv_search_input");
			if(frm) {
				if(frm_ttl) frm_ttl.value = a.innerHTML;
				if(frm_hrf) frm_hrf.value = a.href;
				if(frm_inp && srch_in) if(srch_in.value != "Search...") frm_inp.value = srch_in.value; else frm_inp.value = "";
				if(frm_cur) frm_cur.value = document.location;
				frm.submit();
				return false;
			} else {
				return true;
			}
		} catch(e_error) { return true; }
	}
	
	
	
	// function which positions the guide x,y coordinates on the screen
	// currently, runs during onload and during onresize
	function positionGuide() {
	
		var g = document.getElementById("uv_guide");
		
		if(g) {
		
			// find document width so we don't place guide outside of it
			var docw;
				if(document.documentElement) docw = document.documentElement.clientWidth;
				else if(document.body && document.body.clientWidth) docw = document.body.clientWidth;
				else docw = document.innerWidth;
			
			// guide width
			var guiw = 209;
			
			// offset width
			var offw = 14;
			
			// offset top, used when header2ndLevel isn't found
			var offh = 31; //31-32 depending on browser
			
			
			// position guide element according to uv_search_input position
			var searchInput = document.getElementById("uv_search_input");
			if(searchInput) {
				var loc = findPos(searchInput);
				
				// position left
				if((loc[0] - offw) + guiw <= docw) {
					// position element offset from searchInput's left edge
					g.style.left = (loc[0] - offw) + "px";
				} else {
					// position element to right edge of window
					g.style.left = (docw - guiw) + "px";
				}
				
				// position top
				var h2Level = document.getElementById("header2ndLevel");
				if(h2Level && h2Level.offsetTop) {
					// use the position of the 2nd level header
					g.style.top = (h2Level.offsetTop) + "px";
				} else {
					// use the top offset to the location of the search input box
					g.style.top = (loc[1] + offh) + "px";		// relative to box.. (off by pixel on some)
					//g.style.top = "43px";						// absolute (only on current lcms pages)
				}
				
				
			} else {
				// (should never happen on compatible pages)	
				
				// no search input so we place guide to the right edge of screen
				g.style.left = (docw - guiw) + "px";
				
				// place the guide vertically at absolute 43px; position problems are likely if the template changes, esp. if non-lcms page
				g.style.top = "43px";
			}
			
		}
		
	}
	
	
	function hideGuide() {
		var g = document.getElementById("uv_guide");
		if(g) {
			g.style.display = "none";
		}
	}
	
	
	function showGuide() {
		var g = document.getElementById("uv_guide");
		if(g) {
			g.style.display = "";
		}
		
		var searchField = document.getElementById("uv_search_input");
		if(searchField) {
			searchField.onkeyup();
		}
	}
	
	
	var uv_guide_display_timeout;
	function hintGuideDisplayTimeout() {
		uv_guide_display_timeout = null;
		hideGuide();
	}
	function hintGuideDisplay(display_request) {
	
		// if timeout is running
		if(typeof uv_guide_display_timeout == "number") {
		
			if(display_request) {
				clearTimeout(uv_guide_display_timeout);
				uv_guide_display_timeout = null;
			} else {
				// do nothing...  waiting to hide
			}
			
		} else {
			if(display_request) {
				// nothing to reset
			} else {
				uv_guide_display_timeout = setTimeout(hintGuideDisplayTimeout, 1000);
			}
			
		}
	}
	
	// add guide positioning to onresize event
	var onresize_before_search_guide;
	if(typeof onresize != "undefined") {
		onresize_before_search_guide = onresize;
	}
	onresize = function() {
		positionGuide();
		
		if(typeof onresize_before_search_guide == "function") {
			onresize_before_search_guide();
		}
	}
	
	
	
	// add our own code to onload event
	var onload_before_search_guide;
	if(typeof onload != "undefined") {
		onload_before_search_guide = onload;
	}
	onload = function() {	
		
		// perform all previous onload event actions
		if(typeof onload_before_search_guide == "function") {
			onload_before_search_guide();
		}
		
		
		
		// perform our own actions:
		
		
		
		var g = document.createElement("div");
		g.setAttribute("id", "uv_guide");
		g.style.position = "absolute";
		g.style.height="auto";
		g.style.padding="5px";
		g.style.background = "#005328 url(http://www.uvsc.edu/search/guide/s_bg.gif) repeat-x top left";
		g.style.borderTop = "0px";
		g.style.fontSize = ".75em"
		g.style.zIndex = "500";
		
		g.innerHTML = "<form id='uvGuide_form' action='http://www.uvu.edu/search/' method='post' style='margin:0;padding:0;'><input type='hidden' name='uvG_title' id='uvG_title' value=''/><input type='hidden' name='uvG_href' id='uvG_href' value=''/><input type='hidden' name='uvG_input' id='uvG_input' value=''/><input type='hidden' name='uvG_curr' id='uvG_curr' value=''/></form><table border='0' cellspacing='0' cellpadding='0' style='width:199px;background-color:#61947a'><tr><td style='background-image:url(http://www.uvsc.edu/search/guide/s_top.gif);background-repeat:no-repeat;height:11px;'></td></tr><tr><td id='uv_guide_cont' style='padding:11px 6px 11px 11px;display:none;'><div id='uv_guide_data' style='padding:0 5px 0 0;max-height:200px;overflow:auto;display:none;'></div></td></tr><tr><td style='background-image:url(http://www.uvsc.edu/search/guide/s_bot.gif);background-repeat:no-repeat;height:22px;text-align:right;padding-right:8px;font-size:8pt;' align='right'><a href='http://www.uvu.edu/search/' style='color:#ebf8ef;' onclick='return(goGuide(this))'>Complete A-Z Index</a></td></tr></table>";
		
		g.onmouseover = function() {
			hintGuideDisplay(true);
		}
		
		g.onmouseout = function() {
			hintGuideDisplay(false);
		}
		
		g.style.display = "none";
		try {
			document.getElementsByTagName("body").item(0).appendChild(g);
		} catch(e_error){}
		
		
		
		// fix max-height incompatibility (namely ie6)
		var d = document.getElementById("uv_guide_data");
		if(d) {
			try {
				if(typeof d.style.maxHeight == "undefined") {
					d.style.height = "200px";
				}
			} catch(e_error){}
		}
		
		
		
		// position the newly created guide div
		positionGuide();
		
		
		
		// attach mouse events to search form elements (includes child objects individually)
		var searchForm = document.getElementById("uv_search_form");
		if(searchForm) {
			searchForm.onmouseover = function() {
				hintGuideDisplay(true);
				showGuide();
			}
			searchForm.onmouseout = function() {
				hintGuideDisplay(false);
			}
			
			// overwrite onsubmit function instead of add to it (reason for this)
			searchForm.onsubmit = function() {
				try {
					// begin to hide guide (if the user presses 'back' it will be gone)
					hintGuideDisplay(false);
					
					// visit search page if input box is 'empty'
					t=document.getElementById('uv_search_input');
					if(t && (t.value=='Search...'||t.value=='')) {
						location.href='http://www.uvu.edu/search/';
						return false;
					}
				} catch(error){}
			}
		}
		
		
		
		//
		var searchField = document.getElementById("uv_search_input");
		if(searchField) {
		
			
			// overwrite onfocus event
			searchField.onfocus = function() {
				hintGuideDisplay(true);	// will kill any hide timeouts
				showGuide();
				
				if(this.value=='Search...') this.value='';
				else this.select();
			}
			
			
			// overwrite onblur event
			searchField.onblur = function() {
				if(this.value=='') this.value='Search...';
			}
			
			
			// overwrite onkeydown event
			searchField.onkeydown = function() {
				// for the case when a user's mouse moved out while typing and a timeout is set to hide the guide
				hintGuideDisplay(true);
				
				// show the guide in the case it has already hidden
				showGuide();
			}
			
			
			// overwrite onkeyup event
			searchField.onkeyup = function() {
			
				// if the uvGuide array hasn't loaded, return
				//  an option here would be to create a "loading..." element and attach it before returning
				if(typeof uvGuide == "undefined") return;
				
				
				// return if the data element doesn't exist
				var dat = document.getElementById("uv_guide_data");
				if(!dat) return;
				
				
				// the container of the guide data
				var con = document.getElementById("uv_guide_cont");
				
				
				// get the current input box value and perform an initial check return
				var val = this.value.toLowerCase();
				if(val == "" || val == "search..." || val.length > 31) {
					dat.style.display = "none";
					if(con) con.style.display = "none";
					return;
				}
				
				// combine words with single quotation: o'brian, o'connor
				val = val.replace(/[']+/g, "");
				
				// convert underscores to spaces
				val = val.replace(/[_]+/g, " ");
				
				// split at non-characters
				var vals = val.split(/\W/);
				
				
				// set up the array of keywords we'll be using
				// some browsers (incl. firefox) will index empty strings into array, so this cleans array
				var keywords = new Array();
				for(var x=0; x<vals.length; x++) {
					if(vals[x] != "") {
						keywords.push(vals[x]);
					}
				}
				
				
				// ******DEBUG ONLY
				//window.status = vals.length + ", " + keywords.length + ": " + keywords.join(", ");
				
				
				// return if no keywords
				if(keywords.length < 1) {
					dat.style.display = "none";
					if(con) con.style.display = "none";
					return;
				}
				
				var found_string = "";
				var total_count = 0;
				
				
				// search uvGuide array
				for(var x=0; x<uvGuide.length; x++) {
					var match_count = 0;
					var ffilled = new Array();
					
					for(var y=3; y<uvGuide[x].length; y++) {
					
						// find the number of matches
						for(var z=0; z<keywords.length; z++) {
						
							if(ffilled[z]) continue;
							if(uvGuide[x][y].indexOf(keywords[z]) == 0) {
								match_count++;
								ffilled[z] = true;
								break;
							}
						}
					}
					
					
					if(match_count >= keywords.length) {
						var color = (uvGuide[x][2] == "pp") ? "#edfaa7" : "#ffffff";
						found_string += "<li style='padding-bottom:0.8em;'><a href='" + uvGuide[x][1] + "' onclick='return goGuide(this);' style='color:" + color + "'>" + uvGuide[x][0] + "</a></li>";
						//found_string += "<li style='padding-bottom:0.8em;'><a href='" + uvGuide[x][1] + "' onclick='return goGuide(this);' title='" + uvGuide[x][1] + "' style='color:" + color + "'>" + uvGuide[x][0] + "</a></li>";
						total_count++;
						
						if(total_count >= 100) {
							found_string += "<li style='font-size:.85em;color:#ffffff'>(Max limit of " + total_count + " reached)</li>";
							break;
						}
					}
					
				}
				
				
				try {
					if(found_string != "") {
						dat.innerHTML = "<ul style='list-style:none;padding:0;margin:0;'>" + found_string + "</ul>";
						dat.style.display = "";
						if(con) con.style.display = "";
					} else {
						if(con) con.style.display = "none";
						dat.style.display = "none";
						dat.innerHTML = "";
					}
				} catch(e_error) {}
				
			}
			
			
		}
		
		
	}
	
	
}



// set marker to true so we don't run code again (if system attaches this js multiple times)
LOADED_search_guide_JS = true;




