/* 

	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 = "q";
	
	// 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 = "academic, Academic Affairs, Academic Enrichment Services, Accounting, Accounting/Systems, accreditation, Admissions, Admissions Student staff, aerobics room, air band, Alumni, alumni council, American Studies Program, Applied Missions, Applied Missions/Ireland, Applied Music, Apply Now, apply online, Archives, Art, Art Education, Art/Systems, Asian awareness association, astronomy club, Athletics, Australia Studies Centre, baseball , Biblical Languages, Biblical Literature, Biblical Literature/System, Biblical Literature/Systems, Biochemistry, Biology, Biology Science Education, Biology/Pre-medicine, Biology/Systems, Blogs, Bookstore, boy's basketball camp, Bursar, C. S. Lewis, calendar, campus interviewing, Campus Pastor, Campus Safety, Career Development, Career Development Connections , catalog, Center for C. S. Lewis and Friends, Center for research and Innovation, Chapel, Chapel Audio/Video, Chapel Coordinators, Chapel Program, chapel schedule, Charles Simeon, Chemistry, Chemistry Education, China Studies Program, Christian College Consortium Student Visitor Program, Christian Education, Christian Education Ministries, Christian Educational Ministries/Systems, Christian Educational Ministries/Systems, Christian Stewardship, Christmas fandango, church matching grant, Church Music, class schedule, commencement, Communication Studies, Communication Studies/Systems, Community Life Board, Composition, Computer Engineering, Computer Science, computer science club, Computer Science-New Media, Computer Science-New Media/Systems, Computer Science/Systems, Computer Sciences and Engineering, conference, Conference Services, Contemporary Music Center, Corporate Communications, cost, Counseling Center, courses, cover letters, Cram Summer Honors Program, CRI, cultural diversity, Daystar University, Development, Developmental Economics, Developmental Economics/Systems, dining commons, Dining Services, discipleship assistant, Discipleship coordinators, discipleship program, Dr. Eugene Habecker, Dr. Jay Kesler, Dr. Steve Bedi, Early Childhood Education, Earth and Environmental Science, echo, Economics, Economics/Systems, ECRU, Educational Technology center, Elementary Education, employee directory, Employment Opportunities, Engineering Physics, English, English Education, English/Systems-Literature, English/Systems-Writing, entrance requirements, Entrepreneurship/Business Majors, Entrepreneurship/Non-Business Majors, Environmental Engineering, Environmental Law and Policy, Environmental Science, Environmental Studies, equestrian club, Escape to Reality, Ethnic Students, Evan Bergwall Hall, exam, Exercise and Fitness, Exercise Science, Exercise Science/Exercise and Fitness, faculty, Fairlane Apartments, Family Forum Radio, field house, Finance, Financial Aid, Focus on the Family Institute, football, French, French Education, Fresh Perspectives, Freshman, freshman orientation, general education requirements, Geography, girl's basketball camp, Giving, golf, Grace Olsen Hall, graduate, graduate school placement and resources, graduation, graphic design, health, Health and Physical Education, health center, heritage, heritage weekend, History, History/Systems, Home School, Hong Kong Baptist University, Honors Program, Housing, Housing Policy, Human Resources, human resources, Huron University USA in London, important dates, Information Technology, Institutional Research, Instrumental Performance, Integration of Faith and Culture Programs (IFC), Inter-Class Council (ICC), Intercultural Studies, Intercultural Studies/Missions, International Business, International Business/Systems, International Student Orientation, International Students, International Studies, internships, intramurals, Ireland, Irish studies program, j-term, Jerusalem University College, John Wengatz Hall, Kesler Student Activities Center, Keyboard Pedagogy, ksac, Latin American Studies Program, leadership, leadership conference, Leadership Development Cabinet, leadership initiative of Taylor, Les Gerig Hall, library, life together covenant, lighthouse, Literature, Lithuanian Christian College, Los Angeles Film Studies Center, Los Angeles Term, LTC, Magazine, mahe, majors, Management, Management Information Systems, Management/Systems, map, Marketing, Marketing/Systems, mars, Mary Tower English Hall, Master of Business Administration, Master of Environmental Science, Master of higher education, Master of religious Studies, Math, math club, Mathematics, Mathematics Education, Mathematics- Interdisciplinary, Mathematics/Systems, matriculation fee, MBA, meal plans, Media and Marketing Services Board, Media Communication/Systems, Media Communications, men's basketball, men's cross country, men's lacrosse club, men's soccer, men's tennis, men's track, mes, Middle East Studies Program, ministry and service, mission reps, mission statement, Missionary Kids, Missions, MLK Jr. Day, Modern Languages, Multi-Ethnic Student Association, Music, Music Education, my generation night, mytaylor, national student leadership conference, nostalgia night, Nussbaum, off campus housing, off-campus Housing, Off-campus Study Programs, Office of Finance, Office of the President, Office of the Provost, Online Learning, orientation leader, parents, parents council, Pastor's day, Personnel assistants, Philosophy, Philosophy/Systems, Physical Education, Physical Education and Human Performance, Physical Science Education, Physical Science Education/Chemistry, Physical Science Education/Physics, Physics, Physics Science Education, Physics/Mathematics Education, Piano Performance, plagiarism, Podcasts, Political Science, Political Science/Systems, practicum, Pre-law, Pre-Medicine, Pre-professional, Professional Writing, Psychology, Psychology/Pre-medicine, Psychology/Systems, Psychology/Systems-Pre-Medicine, Public Relations, Randy Gruendyke, Registrar, registrar, requirements, residence halls, residence life, Residential Network, RESNET, resources, resumes, RSS, Russian Studies Program, Samuel Morris Hall, Sammy Morris, Samuel Morris, schedule, Schedule a Visit, Scholarships, School of Liberal Arts, School of Natural and Applied Sciences, School of Professional and Graduate Studies, Semester in Spain, simeon sermons, social justice week, Social Studies Education, Social Work, Sociology, softball, Spanish, Spanish Education, Special Education, Sport Management, sports camps, spring break, statement of faith, Stewards of creation, student, student activities, student activities council (SAC), student clubs, Student Development, student handbook, student life handbook, Student senate, student services, Student Services Council, Students in Free Enterprise (SIFE), students right to know, Studio Art, study abroad, summer camps, Summer Orientation, Swallow Robin Hall, Systems, taylathon, Taylor fund, Taylor Magazine, Taylor Online, Taylor Stories, Taylor Student Organization, Taylor World Organization, Taylor world outreach, TESOL, the guild, The Oregon Extension, The Orvieto Semester, The Scholars' Semester in Oxford, the well, Theatre, Theatre Arts, Theatre Arts/Systems, Third Culture Kids, tower, transcript, Transfer Students, transition to teaching, Trojan club, Trojan film festival, tuition, Uganda Studies Program, University Advancement Ambassador, University Relations, upland, venture for victory, Visit, Visit Campus, Visual Arts, Visual Arts/Systems, Vocal Performance, Wallpapers, Washington Journalism Center, WBCL, Webcam, Welcome Weekend, wellness, wheeler, William Taylor Foundation, women's basketball, women's cross country, women's lacrosse club, women's soccer, women's tennis, women's track, world opportunities week (wow), wow, wow cabinet, writer's club, Writing, writing, writing center, youth, Youth Ministry, Zondervan Library"; 
	
	// END CONFIG (do not edit below this line, well unless you really, really want to change something :) )
	
	// Peace, 
	// Alen

	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", "off");
			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);	
					};	
					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);

