jQuery(document).ready(function($){
	$('ul.ssf-green ul ul', '#top-header').remove();
	
	// CONTRACT PAGE
	//$('div.entry', '#contract').hide();
	$('h2 a', '#contract').click(function(){
	    $(this).parent().next().toggle();
	    return false;
	});
	
	$('#contract').each(search_highlight);
	
	/* Frequently Asked Questions */
	var faq = $('#post-502 > div.entry').get(0);
	$('>:not(h3)', faq).hide();
	$('>h3', faq)
		.hover(
			function() {
				$(this).addClass('hover');
			},
			function() {
				$(this).removeClass('hover');
			}
		).click(
			function() {
				$(this).nextUntil('h3').toggle();
			}
		);
	
});


function search_highlight(){
	var self = this;
	self.input = $("#search");
	
    //handles searching the document
	self.performSearch = function() {
		//create a search string
		var phrase = self.input.val().replace(/^\s+|\s+$/g, "");					
		phrase = phrase.replace(/\s+/g, "|");
		
		//make sure there are a couple letters
		if (phrase.length < 3) { 
			// Display status
			$("span.result-count", "#page_search").text("Query must be 3 characters or more.");
			$("div.entry", '#contract').show();
			return;
		}			
		
		// Display status
		$("span.result-count", "#page_search").text("Searching...");
		
		//append the rest of the expression
		phrase = ["\\b(", phrase, ")"].join("");
		
		//search for any matches
		var count = 0;
		$("h2 a,p", "#contract").each(function(i, v) {
		
			//replace any matches
			var block = $(v);
			block.html(
				block.text().replace(
					new RegExp(phrase, "gi"), 
					function(match) {
						count++;
						return ["<span class='highlight'>", match, "</span>"].join("");
					}));
			
		});
		
		//update the count
		$("span.result-count", '#page_search').text(count + " results found.");
		
		$("div.entry:has(span.highlight)", '#contract').show();
		$("div.entry:not(:has(span.highlight))", '#contract').hide();
		
		//clear this search attempt
		//should be gone anyways...
		self.search = null;
	
	};

	self.search;
	self.input.keyup(function(e) {
		if (self.search) { clearTimeout(self.search); }
		
		//start a timer to perform the search. On browsers like
		//Chrome, Javascript works fine -- other less performant
		//browsers like IE6 have a hard time doing this
		self.search = setTimeout(self.performSearch, 300);
		
	});
}

