$(document).ready(function() {
	var ie = $.browser.msie;
	
	// Hide sidebar menu contents
	$("ul#menu li form").hide();

	// Sidebar menu functionality
	$("ul#menu li span").click(function () {
		form = $(this).siblings("form");
		if(!ie) {
			// Use a nice smooth sliding animation for Firefox & Safari
			form.slideToggle("fast");
		} else {
			// Issues with form field display in IE made the animation
			// look terribly chunky so we just show & hide the form for IE
			if ($(form).is(":hidden")) {
				form.show();
			}
			else {
				form.hide();
			}
		}
	});

	// Text input watermark
	$("#search_params_search_text").watermark("Keyword, Item Title, etc.");
	$("#wish_list_name").watermark("Enter first or last name");

    // Hide extra filters on page load
	function initializeFilter() {
	    $("ul#menu li#filter-wine dl.flavor dd label:gt(5)").hide();
	    $("ul#menu li#filter-wine dl.pairings dd label:gt(3)").hide();
	    $("ul#menu li#filter-wine dl.occasion dd label:gt(1)").hide();
	    $("ul#menu li#filter-wine dl.price dd label:gt(1)").hide();
	    $("ul#menu li#filter-wine dl dd.all,ul#menu li#filter-wine a.all").show();
    };

    initializeFilter();

    // Show/hide extra options on click
    $("ul#menu li#filter-wine dl dd.all a").click(function () {
		container = $(this).parent().prev();
		el = $(this);

		// Determine which node was clicked (to set a number for that node)
		if (container.parent().hasClass("flavor")) {
			number = 5;
		} else if (container.parent().hasClass("pairings")) {
			number = 3;
		} else if (container.parent().hasClass("occasion")) {
			number = 1;
		} else if (container.parent().hasClass("price")) {
			number = 1;
		};

		// Determine if we're showing or hiding and update update the text
		if ($(this).text().match(/Show/)) {
			container.find("label").show();
			el.html(el.html().replace(/Show all/, "Hide extra"));
		}
		else if ($(this).text().match(/Hide/)) {
			el.html(el.html().replace(/Hide extra/, "Show all"));
			container.find("label:gt(" + number + ")").hide();
		};
		return false;
	});

	// Show/hide ALL extra options on click
	$("li#filter-wine a.all").click(function () {
		if ($(this).text().match(/Show/)) {
			$(this).siblings().find("dd label").show();
			$(this).html($(this).html().replace(/Show all/, "Hide extra"));
		} else if ($(this).text().match(/Hide/)) {
		    initializeFilter();
			$(this).html($(this).html().replace(/Hide extra/, "Show all"));
		};
		return false;
	});
});