// Drilldown For Large Categories using jQuery 1.3
// This function will display a filter box when there are more than 10
//   items in a category page.

$(document).ready(function(){

	// Removes item filter
	function ClearFilter () {
		$(".contentsrow").each(function (i){
			$(this).show();
			$(this).css("background","");
			});
		$("#filtertext").val("");
		$("#clearfilter").css("visibility","hidden");
		$("#resulttitle").text("Available Items")
		$("#resultnumber").text("");
		
	}

	// Sets Item Filter
	function ApplyFilter (FilterText) {
		$(".contentsrow").hide();
		var nMatches = 0;
		var nTotalItems = 0;
		$(".contentsrow").each(function (i){
			
			if ( $(this).text().toUpperCase().indexOf(FilterText.toUpperCase()) != -1 ) {
				$(this).show();
				nMatches++ ;
				if (nMatches % 2 == 0) {
					$(this).css("background","#ffffdc");
				} else {
					$(this).css("background","#ffffff");	
				}
			}
			nTotalItems++;
			
		});
		$("#clearfilter").css("visibility","visible");
		$("#resulttitle").text("Filtered Results");
		$("#resultnumber").text("   Showing "+nMatches.toString() + " of "+ nTotalItems.toString()+" Items");
	}


// Filter on Keypress
$("#filtertext").keyup(
		function (keycode){
			if ($(this).val() != "") {
				ApplyFilter($(this).val());
			} else {
				ClearFilter ();
			}
		}
	);

	// Set Click Event For Clear List
	$("#clearfilter").click(
		function(){
			ClearFilter();
			}
	);

	// Show drilldown when JS is enabled and there are many results
	var nMinToShowFilter = 10;
	var nTotalItems = 0;
	$(".contentsrow").each(function (i){
		nTotalItems++;
	});
	if (nTotalItems > nMinToShowFilter) {
		$("#drilldown").show();
		$("#filtertext").focus();
	}



});