var catLearnShowing;
$(document).ready(function(){
	handleLearnForm();
	handleCatLearn();
});
function handleLearnForm() {
	var defaultInputLearn = "Learn about Health Topics...";
	$("input#inputLearn")
		.val(defaultInputLearn)
		.blur(function() {
			if ($.trim($(this).val())=="") {
				$(this).removeClass("inputText");
				$(this).val(defaultInputLearn);
			}
		})
		.click(function() {
			if ($.trim($(this).val())==defaultInputLearn) {
				$(this).addClass("inputText");
				$(this).val("");
			}
		})
		.focus(function() {
			if ($.trim($(this).val())==defaultInputLearn) {
				$(this).addClass("inputText");
				$(this).val("");
			}
		});
	$("#learn-form").submit(function() {
		var searchterm = $.trim($("input#inputLearn").val());
		if (searchterm=="" || searchterm==defaultInputLearn) {
			return(false);
		} else {
			return(true);
		}
	});
}
function handleCatLearn() {
	transformCatLearn();
	catLearnShowing = false;
	$("#catLearnText").click(function() {
		toggleCatLearnChoices();
		return(false);
	});
	$("ul#catLearnChoices li").click(function() {
		var catValue = $(this).attr("val");
		var catLabel = $(this).text();
		$("#catLearnText").find("a").text(catLabel);
		$("#catLearn").val(catValue);
		toggleCatLearnChoices();
	});
	$("ul#catLearnChoices").blur(function() {
		hideCatLearnChoices();
	});
	$("ul#catLearnChoices").bind("clickoutside",function(event){
		if (catLearnShowing) {
			hideCatLearnChoices();
		}

	});
}
function toggleCatLearnChoices() {
	if ($("ul#catLearnChoices").is(":visible")) {
		hideCatLearnChoices();
	} else {
		showCatLearnChoices();
	}
}
function hideCatLearnChoices() {
	$("ul#catLearnChoices").hide();
	catLearnShowing = false;
}
function showCatLearnChoices() {
	$("ul#catLearnChoices").slideDown(300,function() {
		catLearnShowing = true;
	});
}
function transformCatLearn() {
	var selectedCatLearnValue = $("select#catLearnSelect :selected").val();
	var selectedCatLearnText = $("select#catLearnSelect :selected").text();
	$("#catLearnText").after("<input type='hidden' id='catLearn' name='filter' value='" + selectedCatLearnValue + "' />");
	placeCatLearnChoices();
	$("select#catLearnSelect > option").each(function() {
		$("ul#catLearnChoices").append("<li val='" + $(this).val() + "'>" + $(this).text() + "</li>");
	});
	$("select#catLearnSelect").remove();
	// We get rid of all the content and then put it back in so that IE doesn't add extra space between the text and link
	var remainingCatText = $("#catLearnText").text();
	$("#catLearnText").text("").append(remainingCatText + " <a href='#'>" + selectedCatLearnText + "</a>");
}
function placeCatLearnChoices() {
	if($.browser.msie) {
		// When inside KB, tmpl-main won't exist
		if ($("#tmpl-main").is("*")) {
			$("#tmpl-main").prepend("<ul id='catLearnChoices'></ul>");
			$("#catLearnChoices").css("top","314px").css("right","34px");
			$("body.tmpl-splash #catLearnChoices").css("top","366px").css("right","34px");
		}
		if ($("#HwContainer").is("*")) {
			$("#HwContainer").prepend("<ul id='catLearnChoices'></ul>");
			$("#catLearnChoices").css("top","314px").css("right","34px");
		}
	} else {
		$("#catLearnText").after("<ul id='catLearnChoices'></ul>");
	}
}

