$(document).ready(function(){
	$("select.section-catlist").change(sectionPicked)
	$("span.cat-catlist select").change(catPicked)

	function sectionPicked() {
		//find variables and objects we will need
		sType				= $(this).parents("li").attr("class");
		oCatName			= $("." + sType + " span.cat-catlist select").attr("name")
		oCatSpan			= $("." + sType + " span.cat-catlist");
		oSubCatSpan		= $("." + sType + " span.subcat-catlist");

		//since section has changed reset everything
		oCatSpan.children("select").val('')
		oSubCatSpan.children("select").val('')

		//show loading
		oCatSpan.parents("li").children("span.loading-category").show();

		//run loading suburbs
		loadCats(oCatSpan,oCatName,sType,'1')
	};

	function catPicked() {
		//find variables and objects we will need
		sType				= $(this).parents("li").attr("class");
		oSubCatName		= $("." + sType + " span.subcat-catlist select").attr("name")
		oSubCatSpan		= $("." + sType + " span.subcat-catlist");

		//show loading
		oSubCatSpan.parents("li").children("span.loading-category").show();

		//run loading suburbs
		loadCats(oSubCatSpan,oSubCatName,sType,'2')
	};

	function loadCats(oSpan,sSelectName,sType,iLevels) {
		var iSectionID		= $("." + sType + " .section-catlist").val()
		var iCategoryID	= $("." + sType + " span.cat-catlist select").val()
		var oCatSpan			= $("." + sType + " span.cat-catlist");
		var oSubCatSpan		= $("." + sType + " span.subcat-catlist");

		//disable any buttons that need to be
		$("input.disable-catlist, select.disable-catlist, span.disable-catlist input, span.disable-catlist select").attr("disabled", "disabled")

		$.get("ajaxCats.asp",{iSectionID: iSectionID, iCategoryID: iCategoryID, sSelectName: sSelectName, iLevels: iLevels}, function(sData){
			//return the options
			oSpan.html(sData);

			//hide loading
			oCatSpan.parents("li").children("span.loading-category").hide();
			oSubCatSpan.parents("li").children("span.loading-category").hide();

			$('span.cat-catlist select').bind('change', catPicked);

			//disable any buttons that need to be
			$("input.disable-catlist, select.disable-catlist, span.disable-catlist input, span.disable-catlist select").attr("disabled", "")
		})
	};
});
