/* 

	SearchField 
	written by Alen Grakalic, provided by Css Globe (cssglobe.com)
	please visit http://cssglobe.com/post/1202/style-your-websites-search-field-with-jscss/ for more info
	
*/

this.searchfield = function(){
	
	// CONFIG 
	
	// this is id of the search field you want to add this script to. 
	// You can use your own id just make sure that it matches the search field in your html file.
	var id = "searchfield";
	
	// Text you want to set as a default value of your search field.
	var defaultText = "Search the site...";	
	
	// set to either true or false
	// when set to true it will generate search suggestions list for search field based on content of variable below
	var suggestion = true;
	
	// static list of suggestion options, separated by comma
	// replace with your own
	var suggestionText = "About Western, Alpha Kappa Alpha, Academic & Student Affairs, Alpha Kappa Psi, Academic Calendar, Academic Computing, Alpha Mu Gamma, Accounting Services, Alpha Omega Sorority, Administration, Alpha Phi Alpha, Admissions, Alumni Services, Advancement Office, Applied Learning, Alchemist Club, Art Department, Alliance Française, Athletics, Alpha Gamma Delta, Banner, Board of Governors, Baptist Student Union, Bookstore, Barnes & Noble, Beta Beta Beta, Business Office, Biology Department, Business Department, Business, Calendar, Clery Report, Dining, Clubs, Directory, Insurance, Risk Management, Map, College of Liberal Arts & Sciences, Campus Ponds, College of Professional Studies, Campus Printing, Color Guard, Golden Griffon, Career Development, Commencement, Catalog, Communication Studies, Theatre, Catering Services, Computer Labs, Center for Academic Support, Computer Science, Math, Physics, Center for Community Arts, Community Arts, Center for Multicultural Education, Multicultural Education, Conferences and Special Programs, Center for Student Engagement, Student Engagement, Convocation on Critical Issues, Chemistry Department, Counseling Center, Childhood Studies, Craig School of Business, Christian Campus Fellowship, Criminal Justice, Legal Studies, Class Schedule, Critical Thinking, Classroom Information Guide, Dance Team, Distance Education, Deans, Division of Student Affairs,  Delta Phi Upsilon, Downtown University Center, Development Office, Driver Education, Disability Services, Economics Department, E-Mail Lookup (Student), Education Department, Employment Opportunities, Electronic Voting, Engineering Technology Department, E-Mail Lookup (Faculty/Staff), English, Foreign Languages, Journalism, Faculty Senate, Financial Aid, Faculty E-Mail & Phone Directory, Staff E-Mail & Phone Directory, Financial Planning & Administration, Final Examinations Schedule, Finals, Fitness Center, Final Grades, Foundation, Getting to Western, Grants & Sponsored Programs, German Club, Graphic Standards Manual, Giving to Western, Greek Life, GoldLink, Griffon Alert, Government, Social Work, Sociology, Griffon Arts Society, Grades, Cheer Squad, Graduate Studies, Griffon Edge, Graduation Application, Griffon News, Graduation Information, Health, Physical Education, Recreation, History, Philosophy, Geography,  Health Center, Honors Program, Health Information Technology, Hire A Griffon, Housing & Residential Life, Residential Life, High School College Credit, Human Resources, Information Technology Services, Inter-Greek Council, Institute for Industrial and Applied Life Sciences, Interdisciplinary Studies, Instructional Media Center, International Student Services, Internships (Hire A Griffon), Journalism Club, Kansas City Northland, Northland, Key Office, Law Enforcement Academy, Liberal Arts & Sciences,  Learning Communities, Library, Legal Studies Association, Magazine, Military Science, Major Minor Forms, Mission & Values, Matrix Club, Music Department, Midterm Grades, My Western, Newman Club, Non-Traditional Student Services, News Releases, Nursing Department, Non-Traditional Student Association, Omega Electronics Association, Online Classes, Omicron Delta Epsilon, Organization of Student Social Workers, Omicron Psi, Organizations, Enrollment for MWSU Classes, P: Drive Access, Prairie Lands Writing Project, President, Partners in Prevention (PIP), Pride Alliance, Pass The Power Adult Literacy, Professional Studies, Phi Epsilon Kappa, Professional Training, Phi Mu, Psi Xi, Phi Mu Alpha Sinfonia, Psychology Department, Physical Plant, Public Relations/Marketing, Points of Pride, Purchasing, Police Department, Recreation Services, Registration, Regional Community Policing Institute, Residence Council, Registrar, ROTC, Safety at Western, ACT Scores, Scheduling, Address Information, Scribes and Muses, Student Affairs, Sigma Gamma Rho, Student Consumer Information, Sigma Sigma Sigma, Course Schedule, Sigma Tau Delta, Small Business Institute, Student Employment, SNCTE, Student Government Association, Society for Technical Communication, Student Governor, Speaker's Bureau, Student Handbook, St. Joseph, Missouri, Student Honors Association, Staff Association, Student Nurses' Association, Strategic Plan, Study Away, Tau Kappa Epsilon, Transcripts, Telephone Services, Testing Center, Transfer Guidelines, Theta Xi, Tuition, Tower Topics, Tutoring Services, University Catalog, Veterans Affairs, Voting, Web Policies & Guidelines, Western Institute, Wesley Foundation, Wildlife Society, Western Activities Council (WAC), Wireless Information, Y's Kids World"; 
	
	// END CONFIG (do not edit below this line, well unless you really, really want to change something

	var field = document.getElementById(id);	
	var classInactive = "sf_inactive";
	var classActive = "sf_active";
	var classText = "sf_text";
	var classSuggestion = "sf_suggestion";
	this.safari = ((parseInt(navigator.productSub)>=20020000)&&(navigator.vendor.indexOf("Apple Computer")!=-1));
	if(field && !safari){
		field.value = defaultText;
		field.c = field.className;		
		field.className = field.c + " " + classInactive;
		field.onfocus = function(){
			this.className = this.c + " "  + classActive;
			this.value = (this.value == "" || this.value == defaultText) ?  "" : this.value;
		};
		field.onblur = function(){
			this.className = (this.value != "" && this.value != defaultText) ? this.c + " " +  classText : this.c + " " +  classInactive;
			this.value = (this.value != "" && this.value != defaultText) ?  this.value : defaultText;
			clearList();
		};
		if (suggestion){
			
			var selectedIndex = 0;
						
			field.setAttribute("autocomplete", "on");
			var div = document.createElement("div");
			var list = document.createElement("ul");
			list.style.display = "none";
			div.className = classSuggestion;
			list.style.width = field.offsetWidth + "px";
			div.appendChild(list);
			field.parentNode.appendChild(div);	

			field.onkeypress = function(e){
				
				var key = getKeyCode(e);
		
				if(key == 13){ // enter
					selectList();
					selectedIndex = 0;
					return false;
				};	
			};
				
			field.onkeyup = function(e){
			
				var key = getKeyCode(e);
		
				switch(key){
				case 13:
					return false;
					break;			
				case 27:  // esc
					field.value = "";
					selectedIndex = 0;
					clearList();
					break;				
				case 38: // up
					navList("up");
					break;
				case 40: // down
					navList("down");		
					break;
				default:
					startList();			
					break;
				};
			};
			
			this.startList = function(){
				var arr = getListItems(field.value);
				if(field.value.length > 0){
					createList(arr);
				} else {
					clearList();
				};	
			};
			
			this.getListItems = function(value){
				var arr = new Array();
				var src = suggestionText;
				var src = src.replace(/, /g, ",");
				var arrSrc = src.split(",");
				for(i=0;i<arrSrc.length;i++){
					if(arrSrc[i].substring(0,value.length).toLowerCase() == value.toLowerCase()){
						arr.push(arrSrc[i]);
					};
				};				
				return arr;
			};
			
			this.createList = function(arr){				
				resetList();			
				if(arr.length > 0) {
					for(i=0;i<arr.length;i++){				
						li = document.createElement("li");
						a = document.createElement("a");
						a.href = "javascript:void(0);";
						a.i = i+1;
						a.innerHTML = arr[i];
						li.i = i+1;
						li.onmouseover = function(){
							navListItem(this.i);
						};
						a.onmousedown = function(){
							selectedIndex = this.i;
							selectList(this.i);		
							return false;
						};					
						li.appendChild(a);
						list.setAttribute("tabindex", "-1");
						list.appendChild(li);	
					};
						if(navigator.appName == "Netscape"){
						  list.style.left = 0;
						 }else{
						 list.style.left = 0;
					 }
					list.style.display = "block";				
				} else {
					clearList();
				};
			};	
			
			this.resetList = function(){
				var li = list.getElementsByTagName("li");
				var len = li.length;
				for(var i=0;i<len;i++){
					list.removeChild(li[0]);
				};
			};
			
			this.navList = function(dir){			
				selectedIndex += (dir == "down") ? 1 : -1;
				li = list.getElementsByTagName("li");
				if (selectedIndex < 1) selectedIndex =  li.length;
				if (selectedIndex > li.length) selectedIndex =  1;
				navListItem(selectedIndex);
			};
			
			this.navListItem = function(index){	
				selectedIndex = index;
				li = list.getElementsByTagName("li");
				for(var i=0;i<li.length;i++){
					li[i].className = (i==(selectedIndex-1)) ? "selected" : "";
				};
			};
			
			this.selectList = function(){
				li = list.getElementsByTagName("li");	
				a = li[selectedIndex-1].getElementsByTagName("a")[0];
				field.value = a.innerHTML;
				clearList();
			};			
			
		};
	};
	
	this.clearList = function(){
		if(list){
			list.style.display = "none";
			selectedIndex = 0;
		};
	};		
	this.getKeyCode = function(e){
		var code;
		if (!e) var e = window.event;
		if (e.keyCode) code = e.keyCode;
		return code;
	};
	
};

// script initiates on page load. 

this.addEvent = function(obj,type,fn){
	if(obj.attachEvent){
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn](window.event );}
		obj.attachEvent('on'+type, obj[type+fn]);
	} else {
		obj.addEventListener(type,fn,false);
	};
};
addEvent(window,"load",searchfield);

