/* 

	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 our 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 = "eluminate, web site, academic challenge, district staff, st peters, staff, webmail, mail, aesop, careers, sister anita brockman, brockman, oleary, myecsd, superintendent's message, second languages, focus programs, how to register, early learning, international student program, registrations, bilingual, aboriginal learning, open house dates, guide to schools, schools, new school registration information, calendar, h1n1, annual reports and results, district budget, new ward system, st mark, high school completion, year round schooling, early learning programs, disaster in haiti, scholarships, preschool outreach, bus routes, elementary, junior high, high school, district map, map, boundaries, catchment areas, school calendars, school designations, student transportation, transportation, alternative and adult education, esl, english as a second language, online web courses, links, special education programs, back to school information kit, bursaries, awards, at risk, children at risk, curriculum, cold weather policy, weather policy, dealing with conflict management, district plan for continuous growth, FOIP, inclusion parent interest group, kindergarten registration, software, microsoft software for students, operational practices, parent family programs, powerschool, what's new, about us, upcoming events, news releases, archives, catholic advocacy, contact us, core values, curriculum and program descriptions, department list, organizational chart, patron saint, district song, district videos, eight characters of education, fast facts, history of catholic education, JUA, join use agreement, our mission, policies and regulations, social justice, website links, board charter, board governence policy index, catholic education conference, district budget process, district satisfaction survery results, trustees meeting dates, organizational bylaw, our foundation, school council, report cards, student artwork, artwork, school councils, parent tools and links, capital plan, Anne Fitzgerald, Annunciation, Archbishop Joseph MacNeil, Archbishop MacDonald, Archbishop O'Leary, Archbishop Oscar Romero, Austin O'Brien, Ben Calf Robe - St. Clare, Bishop Greschuk, Bishop Savaryn, Blessed Kateri, Cardinal Leger, Father Leo Green, Father Michael Troy, Frere Antoine, Good Shepherd, Grandin, HE Beriault, Holy Cross, Holy Family, Holy Trinity, JH Picard, JJ Bowlen, Jean Forest Leadership Academy, John Paul I, Katherine Therrien, Louis St Laurent, Mary Hanley,  Monsignor Fee Otterson, Monsignor William Irwin, Mother Teresa, Our Lady of Mount Carmel, Our Lady of Peace, Our Lady of the Prairies, Our Lady of Victories, Sir John Thompson, Sister Annata Brockman, St Alphonsus, St Angela, St Anne, St Augustine, St Basil, St Benedict, St Bernadette, St Bonaventure, St Boniface, St Brendan, St Catherine, St Cecilia, St Charles, St Clement, St Dominic, St Edmund, St Elizabeth, St Elizabeth Seton, St Francis of Assisi, St Francis Xavier, St Gabriel, St Gerard, St Hilda, St James, St Jerome, St John Bosco, St Joseph, St Justin, St Kevin, St Leo, St Lucy, St Maria Goretti, St Mark, St Martha, St Martin, St Mary, St Matthew, St Monica, St Nicholas, St Paul, St Philip, St Pius X, St Richard, St Rose, St Stanislaus, St Teresa, St Thomas More, St Timothy, St Vincent, St Vladimir, Trustee Elections, Religious Education, new and expanded programs, board of trustees, transportation cancellations, all boys academy, expanded early learning   "; 
	
	// END CONFIG 	
	// 
	// 

	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);


