// Look for anchor and automatically open Advanced Search form if found
function popOpenAdvancedSearch() {
    Drupal.toggleFieldset(".search-advanced");
}

if (window.location.hash == "#advanced") {
    $(document).ready(popOpenAdvancedSearch);
}

/*
 * Handles selection of header search tabs.
 */
$(document).ready(function(){
	var a = "Enter keyword";
	var search_all_label = "Enter keyword (category is Advanced) ";
	var search_people_label = "Enter keyword (category is People)";
	var search_groups_label = "Enter keyword (category is Groups)";
	var search_resources_label = "Enter keyword (category is Content)";
	$("#search-tabs a").each(function(){
			$(this).click(function(){
				// Set active tab to inactive
				$("#search-tabs a.active").toggleClass("active");
				// Set this tab as active
				$(this).toggleClass("active");
				// Set search field text based on tab id
				// *** Also need to set hidden field values for types
				if ($("#search_form_label").val()==""||$("#search_form_label").val()==a) {
					switch ($(this).attr("id"))
					{
						case "search-all":
							$("#search_form_label").val(search_all_label);
							break
						case "search-people":
							$("#search_form_label").val(search_people_label);
							break
						case "search-groups":
							$("#search_form_label").val(search_groups_label);
							break
						case "search-resources":
							$("#search_form_label").val(search_resources_label);
							break
						default:							
					}	
					a = $("#search_form_label").val();
				}							
				switch ($(this).attr("id"))
				{
					case "search-all":
						$("#search-theme-form").attr("action", "/search/node#advanced");
						$("#search-theme-form input.search-type").remove();
						break
					case "search-people":
						$("#search-theme-form").attr("action", "/search/user");
						$("#search-theme-form input.search-type").remove();
						break
					case "search-groups":
						$("#search-theme-form").attr("action", "/search/node");
						$("#search-theme-form input.search-type").remove();
						$("#search-theme-form").prepend("<input class=\"search-type\" type=\"hidden\" name=\"type[og]\" value=\"og\" />");
						break
					case "search-resources":
						$("#search-theme-form").attr("action", "/search/node");
						$("#search-theme-form input.search-type").remove();
						$("#search-theme-form").prepend("<input class=\"search-type\" type=\"hidden\" name=\"type[blog]\" value=\"blog\" />")
																	 .prepend("<input class=\"search-type\" type=\"hidden\" name=\"type[book]\" value=\"book\" />")
																	 .prepend("<input class=\"search-type\" type=\"hidden\" name=\"type[bookmark]\" value=\"bookmark\" />")
																	 .prepend("<input class=\"search-type\" type=\"hidden\" name=\"type[contentharvester]\" value=\"contentharvester\" />")
																	 .prepend("<input class=\"search-type\" type=\"hidden\" name=\"type[event]\" value=\"event\" />")
																	 .prepend("<input class=\"search-type\" type=\"hidden\" name=\"type[weblink]\" value=\"weblink\" />")
																	 .prepend("<input class=\"search-type\" type=\"hidden\" name=\"type[faq]\" value=\"faq\" />") 
																	 .prepend("<input class=\"search-type\" type=\"hidden\" name=\"type[files]\" value=\"files\" />")
																	 .prepend("<input class=\"search-type\" type=\"hidden\" name=\"type[forum]\" value=\"forum\" />")
																	 .prepend("<input class=\"search-type\" type=\"hidden\" name=\"type[helptip]\" value=\"helptip\" />")
																	 .prepend("<input class=\"search-type\" type=\"hidden\" name=\"type[page]\" value=\"page\" />")
																	 .prepend("<input class=\"search-type\" type=\"hidden\" name=\"type[poll]\" value=\"poll\" />")
																	 .prepend("<input class=\"search-type\" type=\"hidden\" name=\"type[sitenotes]\" value=\"sitenotes\" />")
																	 .prepend("<input class=\"search-type\" type=\"hidden\" name=\"type[story]\" value=\"story\" />")
																	 .prepend("<input class=\"search-type\" type=\"hidden\" name=\"type[survey]\" value=\"survey\" />");
						break
					default:
				}
				return false;
			});
	 	});
	$("#search_form_label")
		.focus(function(){
			if ($(this).val()==a) {
				$(this).val("");
			}
	 	})
	$("#search_form_label")
		.blur(function(){
			if ($(this).val()=="") {
				$(this).val(a);
			}
	 	})
});