jQuery(document).ready(function($) {

	jQuery.expr[':'].contains = function(a,i,m){
	    return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
	};

	$("#content h2:first").css("border-top","none");

	var speed = "normal";
	
	$("#content div").slideUp(speed);
	//$("#content p:first").show(speed);
	
	$("#content h2").click(function() {
		
		$("#content h2.active").removeClass("active");
		$(this).addClass("active");
		
		var index = $("#content h2").index(this);
		var theP = $("#content div").eq(index);
		
		if(theP.is(":visible")){
			theP.hide(speed);
			$("#content h2.active").removeClass("active");
		} else {
			$("#content div").hide(speed);
			theP.show(speed);
		}
		
	});
	
	$("#content h1").after('<input type="text" name="search" value="search" id="search" /><input type="submit" name="submit" value="submit" id="submit" />');
	
	$('input#search').each(function() {

	       var default_value = this.value;

	       $(this).focus(function(){
	               if(this.value == default_value) {
	                       this.value = '';
	               }
	       });

	       $(this).blur(function(){
	               if(this.value == '') {
	                       this.value = default_value;
	               }
	       });

	});
	
	function normal(){
		$(".close-search").remove();
		$("#content h2").show(speed);
		$("#content div:visible").hide(speed);
		$("#content div").removeClass("contains");
		$("*").removeHighlight();
	}
	
	$("input#search").keyup(function(event){
		
		var theVal = $("input#search").val();
		
		if(theVal == ""){
			normal();
		}
		
		if(event.keyCode == 13){
			
			if(theVal == ""){
				
			} else {
				normal();
				$("#content h2, #content div").hide(speed);
				
				if($("#content div:contains(" + theVal + ")").length != 0){
					$("#content div:contains(" + theVal + ")").addClass("contains").show(speed);

					$("#content div.contains").each(function(index) {

						var theIndex = $("#content div").index(this);
						$("#content h2").eq(theIndex).show(speed);

					});
					$("*").removeHighlight();
					$("#content div.contains").highlight(theVal);
					$("input#search").after('<span class="close-search"></span>');
				} else {
					
					alert('You search "' + theVal + '" returned no results.');
					normal();
					$("input#search").val("").focus();
					
				}
					
				
			}
			

			
			
		}
		
	});
	
	$("input#submit").live('click', function(event) {
		event.preventDefault();
		var theVal = $("input#search").val();
		
		if(theVal == ""){
			alert("Please type in a search");
		} else {
			normal();
			$("#content h2, #content div").hide(speed);
			$("#content div:contains(" + theVal + ")").addClass("contains").show(speed);

			$("#content div.contains").each(function(index) {
				var theIndex = $("#content div").index(this);
				$("#content h2").eq(theIndex).show(speed);
			});

			$("*").removeHighlight();
			$("#content div.contains").highlight(theVal);
			$("input#search").after('<span class="close-search"></span>');
		}
		
	});
	
	$(".close-search").live('click', function(event) {
		$(this).remove();
		$("input#search").val('search');
		
		$("#content h2").show(speed);
		$("#content div:visible").hide(speed);
		$("#content div").removeClass("contains");
		$("*").removeHighlight();
	});
	
});
