// This script is used for the dropdown functionality on the N&O search bar. 
$(function() {
	// Populate search box on page load
	if ($("#search_keywords").val() == "") {
		$("#search_keywords").val("Search N&O");
	}

	// This fixes positioned pixel descrepancies between browsers
	jQuery.each(jQuery.browser, function(i) {
		if($.browser.safari){
			$("#y_search_buttons").css("left","675px");
		}else if ($.browser.msie) {
			$("#y_search_buttons").css("left","671px");
		} else {
			$("#y_search_buttons").css("left","670px");
		}
	});

	// When the icon image is clicked, show or hide the search engine choices
	$("#y_search_show_opts").click(function () { 
		if ($("#y_search_buttons").is(":hidden")) {
			$("#y_search_buttons").slideDown("fast");
		} else {
			$("#y_search_buttons").slideUp("fast");
		}
	});

	// This changes the icon image in the search bar when an engine is selected.
	// It also changes the form attributes (mainly for achives) to search the right place
	$("#y_search_buttons > li").click(function () {
		if ($(this).children("input").is("#searchArchive")) {
			$("#search_icon").attr("src", "http://media.newsobserver.com/static/images/masthead/search_archive_icon.jpg");
			$("#search_keywords").val("Search Archives");
			$("#y-srch-wdgt").attr("action", "http://www.newslibrary.com/nlsearch.asp");
			$("#y-srch-wdgt").attr("method", "post");
			$("#search_keywords").attr("name", "keywords");
		} else if ($(this).children("input").is("#searchNandO")) {
			$("#search_icon").attr("src", "http://media.newsobserver.com/static/images/masthead/search_magnifying_glass_icon.jpg");
			$("#search_keywords").val("Search N&O");
			$("#y-srch-wdgt").attr("action", "http://search2.newsobserver.com/search-bin/search.pl.cgi");
			$("#y-srch-wdgt").attr("method", "get");
			$("#search_keywords").attr("name", "sf_pubsys_story");
		} else if ($(this).children("input").is("#searchWeb")) {
			$("#search_icon").attr("src", "http://media.newsobserver.com/static/images/masthead/search_yahoo_icon.jpg");
			$("#search_keywords").val("Search Yahoo");
			$("#y-srch-wdgt").attr("action", "http://search2.newsobserver.com/search-bin/search.pl.cgi");
			$("#y-srch-wdgt").attr("method", "get");
			$("#search_keywords").attr("name", "sf_pubsys_story");
		}

		// Check the appropiate hidden radio
		$(this).children("input").attr("checked", "checked");
		
		// Hide the box
		$("#y_search_buttons").slideUp("fast");
	});

	// This hides the box and empty's the search bar when you click out of it
	$("#search_keywords").focus(function () {
		$("#y_search_buttons").slideUp("fast");
		$(this).val("");
	});
});
