//addLoadEvent(prettySearch);
addLoadEvent(setSearch);

function prettySearch(){
	if(!document.getElementById || !document.getElementsByTagName || !document.getElementById("main")) return false;
	var isSafari3 = false;
   if(window.devicePixelRatio){ isSafari3 = true; }
   //find the input!
   if (!document.getElementById) return false;
   var placeholder='';
	var theSearch = document.getElementById("main").getElementsByTagName("input")[0];
	//Check to see if it is safari. if so, then we set our guy as search and move on.
	var theuserAgent = navigator.userAgent.toLowerCase();
	//ok But are we SAFARI?
	if(theuserAgent.match(/AppleWebKit/i) && !isSafari3){
		theSearch.setAttribute('type', 'search');
	}
	else{
			//alert(theSearch[i].getAttribute("type"));
			//alert(theSearch[i].className);
		var left = document.createElement('span');
		left.className = "left";
		var right = document.createElement('span');
		right.className = "right";
		var wrapper = document.createElement('div');
		wrapper.className = "search-wrapper";
		var tempSearch = document.createElement('input');
		tempSearch.setAttribute("type", "text");
		theSearch.parentNode.replaceChild(tempSearch, theSearch);
		if (isSafari3){
			wrapper.className = wrapper.className + " safari";
			$$('#secondary-content h2 a img').addClass('safari');
	   }
		wrapper.appendChild(left);
		wrapper.appendChild(theSearch);
		wrapper.appendChild(right);
		if(tempSearch){
			tempSearch.parentNode.replaceChild(wrapper, tempSearch);	
		}	
	}
	return true;
}

/* Oh yeah? Search THIS! */
function hideResults(){
	if(!document.getElementById || !document.getElementById("results")){ return false}
	document.getElementById("results").className = "hide";
	return true;
}
function showResults(){
	if(!document.getElementById || !document.getElementById("results")){ return false}
	document.getElementById("results").className = "found";
	return true;
}
function setSearch() {
	if(!document.getElementById || !document.getElementsByTagName || !document.getElementById("results") || !document.getElementById("main")) return false;
		var theBox = document.getElementById("main").getElementsByTagName("input")[1];
		var theResults = document.getElementById("results");
		theBox.setAttribute("focus", "0");
		theResults.setAttribute("focus", "0");
		if(theBox.value){
				var defaultphrase = theBox.value;
			} else var defaultphrase = ""; 
			
		theResults.onmouseover = function(){
			theResults.setAttribute("focus", "1");
		}
		theResults.onmouseout = function(){
			theResults.setAttribute("focus", "0");	
			if(theBox.getAttribute("focus") == "0"){
				hideResults();	
			}
		}
		theBox.onkeyup = function(){search_this();}
		theBox.onfocus = function(){
			if (this.value != defaultphrase && this.value != ""){search_this();}
			theBox.setAttribute("focus", "1");
			
		}
		theBox.onblur = function(){
			if(theResults.getAttribute("focus") == "0"){
				hideResults();
			}
			theBox.setAttribute("focus", "0");
			
		}
		
	return true;
}

function search_this() {
	var searchBox = document.getElementById("main").getElementsByTagName("input")[1];		
	
	var xmlhttp = getHTTPObject();

	//PATH to your SEARCH FILE WOOHOOO 
	//search root is passed in from the .aspx page for fully qualifing the site root
	xmlhttp.open("POST", searchRoot + "AjaxHandlers/SearchResults.aspx", true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			
			//THE HTML WAY!
			var results = xmlhttp.responseText;
			//alert(results);

			// The first line is "search_term:matches:time_it_took" and everything after that are HTML search results
			var lines = results.split("\n");
			//alert(lines);
			// Get the first line meta-data
			var data = lines.shift();
			lines.shift();
			// The HTML search data is everything left over
			var search_results = lines.join("");
			
			data = data.split(":");
			search_term = data[0];
			match_count = data[1];
			search_time = data[2];

			var header_str = "Searched for \"" + search_term + "\" and found " + match_count + " matches in " + search_time + " seconds";
			if (match_count == 0) { header_str = ""; search_results = "<ul><li>" + search_term + " was not found</li></ul>" }		
			// no! innerhtml is bad! 
			 document.getElementById("results").innerHTML = search_results;
			
		}
	}

	var search_term = searchBox.value;
	search_term = escape(search_term);

	//MIKEY! This is where you get your FABULOUS QUERYTSSITNGRLOLCATS
	var params = "search=" + search_term + "&search_mode=raw" //+ body_include;
	xmlhttp.send(params);
	showResults();
	return true;
}

//function initTactics() {    
//	if (!document.getElementById) return
//			
//	var aLinks = document.getElementById('primary-inner').getElementsByTagName('a');	

//	for (var i = 0; i < aLinks.length; i++) {		
//		if (/tactics/.test(aLinks[i].className)) {
//			var src = aLinks[i].getAttribute('href');						
//			
//			aLinks[i].onclick = function() {			  
//			    //saveText(this.getAttribute('rel'));	
//			    goNew(this.getAttribute('rel'))		    
//			    return false;		
//			}						
//		}
//	}		
//}

//function initPreviousTactic() {    
//	if (!document.getElementById) return
//			
//	var aLinks = document.getElementById('primary-inner').getElementsByTagName('a');	

//	for (var i = 0; i < aLinks.length; i++) {		
//		if (/previous-tactic/.test(aLinks[i].className)) {
//			var src = aLinks[i].getAttribute('href');						
//			
//			aLinks[i].onclick = function() {			  
//			    saveText(document.getElementById('originalSelectedValue').value);			    
//			    //alert(document.getElementById('originalSelectedValue').value);
//			    return false;		
//			}						
//		}
//	}		
//}