$(document).ready(function() { //when the document is loaded
	$('#sitesearchresults').hide(); //hide the search results area

/*Function to show search results when the search input gets typed in (keyup):*/
$('#s').keyup(function() { //when the search box gets focus
	var oldsearch = $('#s').val(); //get the value of the search box
	
	/*Replace spaces with plus signs (+) in the search query, for variable calling:*/
	var search = ''; //create a new variable to store the results
	for(var n=1; n <= oldsearch.length; n++) { //go through all the parts of oldsearch
		if(oldsearch.substring(n-1,n) == ' ') { //if the character is a space
			search+='+'; //replace with a plus
		}
		else {
		search+=oldsearch.substring(n-1,n); //otherwise, keep the old character
		}
	}
	
	/*Now check if the reults are something to search for, and search:*/
	if((search != 'Search Results') && (search.length != 0)) { //if the search text is not the default or blank
		if(this.timer) { //if we already have an event timer
			clearTimeout(this.timer); //clear that timer
		}
		
		//Start a new request to load results:
		this.timer = setTimeout(function() {
            pageTracker._trackPageview(location.pathname+'/search-box/'+search);
			//alert('/?site_search=1&header_search=1&s='+search);
			$("#sitesearchresults").load('/?site_search=1&header_search=1&s='+search,function() { //load the search page with the search query into the search results div, then call:
			$("#sitesearchresults").fadeIn(100); //show the search results on loading the page
			});
		},300); //and set the delay for 300 ms, so we wait until the user is done typing
	}
	else if(search.length == 0) { //if the search length is now zero (no text)
		if(this.timer) { //if we already have an event timer
			clearTimeout(this.timer); //clear that timer
		}
		
		$('#sitesearchresults').fadeOut(100); //fade out the div
	}
});	

/*Function to display results when the user 'submits' the form.*/
$('#sitesearchform').submit(function() {
	var oldsearch = $('#s').val(); //get the value of the search box
	
	/*Replace spaces with plus signs (+) in the search query, for variable calling:*/
	var search = ''; //create a new variable to store the results
	for(var n=1; n <= oldsearch.length; n++) { //go through all the parts of oldsearch
		if(oldsearch.substring(n-1,n) == ' ') { //if the character is a space
			search+='+'; //replace with a plus
		}
		else {
		search+=oldsearch.substring(n-1,n); //otherwise, keep the old character
		}
	}
	
	if((search != 'Search Results') && (search.length != 0)) {
		if(this.timer) { //if we already have an event timer
			clearTimeout(this.timer); //clear that timer
		}
        pageTracker._trackPageview(location.pathname+'/search-box/'+search);
		
		$("#sitesearchresults").load('/?site_search=1&header_search=1&s='+search,function() { //load the search page with the search query into the search results div, then call:
		$("#sitesearchresults").fadeIn(100); //show the search results on loading the page
		});
	}
	else if(search.length == 0) { //if the search length is now zero (no text)
		if(this.timer) { //if we already have an event timer
			clearTimeout(this.timer); //clear that timer
		}
		$('#sitesearchresults').fadeOut(100); //fade out the div
	}
	return false;
});

/*Function for when the search "go" link is clicked:*/
$('#sitesearchgo').click(function() {
	var oldsearch = $('#s').val(); //get the value of the search box
	
	/*Replace spaces with plus signs (+) in the search query, for variable calling:*/
	var search = ''; //create a new variable to store the results
	for(var n=1; n <= oldsearch.length; n++) { //go through all the parts of oldsearch
		if(oldsearch.substring(n-1,n) == ' ') { //if the character is a space
			search+='+'; //replace with a plus
		}
		else {
		search+=oldsearch.substring(n-1,n); //otherwise, keep the old character
		}
	}
	
	if((search != 'Search Results') && (search.length != 0)) {
		if(this.timer) { //if we already have an event timer
			clearTimeout(this.timer); //clear that timer
		}
        pageTracker._trackPageview(location.pathname+'/search-box/'+search);
		
		$("#sitesearchresults").load('/?site_search=1&header_search=1&s='+search,function() { //load the search page with the search query into the search results div, then call:
		$("#sitesearchresults").fadeIn(100); //show the search results on loading the page
		});
	}
	else if(search.length == 0) { //if the search length is now zero (no text)
		if(this.timer) { //if we already have an event timer
			clearTimeout(this.timer); //clear that timer
		}
		$('#sitesearchresults').fadeOut(100); //fade out the div
	}
	return false;
});
	
/*Check if there is a click outside the search results:*/
$(document.body).click(function(event) {
	var clicked = $(event.target); //get the target of the click
	
	if(!(clicked.is('#sitesearchresults') || clicked.parents('#sitesearchresults').length || clicked.is('#sitesearchgo') || clicked.is('#s')) && ($('#sitesearchresults').is(":visible"))) { //if we didn't click in the search results or a child component
		$('#sitesearchresults').fadeOut(100); //fade out the div
	}
});
});
