//used to make dropdowns that filter content on html only pages.

//called by items created with populateDropdown()
//function filters lists which have attrname="filtername" and have id="filter"
function showItems(all, dd, attrName, filterName){

	resetDropdownSelections();
	$(".filter").each(function(i){
		$(this).hide();
	});
	if(all){ //show all items of type attrName
		$(".filter").each(function(i){
			name = $(this).attr(attrName);
			if(typeof name != 'undefined') $(this).show();
		});
	} else {
		$(".filter").each(function(i){
			name = $(this).attr(attrName);
			if(typeof name != 'undefined'){
				if(name == filterName){
					$(this).show();
				}
			}
		});

	}
	//find the dropdown and set its topic to topic selected
	$(".menuBox a").each(function(i){
		name = $(this).attr("name");
		if(typeof name != 'undefined' && name == dd){
			$(this).html(filterName);
			$("#"+dd).hide();//hide the popup
		}
	});

}
//given a hidden dropdown div dropdown
//and an attribute name which items with id "filter" will have, 
//we get a Set of items of attribute type attrName and set them 
//as a list of links in the dropdown's content area.
//also add calls to showItems() to each onClick which will take care of
//filtering
function populateDropdown(dropdown, attrName){
	cont = $("#"+dropdown).find('#content');
	names = [];
	$('.filter').each(function(i){
		name = $.trim($(this).attr(attrName));

		if(typeof name != 'undefined' || name.length > 0){
			if (!new RegExp('^(' + names.join('|') + ')$').test(name)){ //!contains
				names.push(name);
			}
		}
	});
	names.sort( function( a, b ) { 
		if(new RegExp('^(\d+)$').test(a+b)){ //is digit
			return b-a;//reverse sort
		} else {
			return 0;
		}
	});

	$.each( names, function(num, name){
		$("<a href='javascript:;' onclick=\"javascript:showItems(false,'"+dropdown+"', '"+attrName+"', '"+name+"');\">"+name+"</a><br/>").appendTo(cont);
	});
	$("<a href='javascript:;' onclick=\"javascript:showItems(true,'"+dropdown+"', '"+attrName+"', 'All "+attrName+"s');\">Show all "+attrName+"s</a><br/>").appendTo(cont);
}

function resetDropdownSelections(){
	$(".menuBox a").each(function(i){
		name = $(this).attr("name");
		if(typeof name != 'undefined'){
			$(this).html(getDefualtSelection(name));
		}
	});
}

function getDefualtSelection(dropdownName){
	alert("you need to add a getDefualtSelection(dropdownName) function to the page and let it return the"+
				"defualt selection text for the dropdown");
	return "not-defined";
}