$(function() {
	
	$("#catselect").change(function() {
		$("#moditemdiv").hide("fast");
		$("#itemselect").removeOption(/./);
		$("#itemdiv").hide("fast");
		$("#moditemdiv").hide("fast");
		if (this.id > '0') {
			$("#catdiv").hide();
			$("#catdiv").show("fast");
			$("#catname").val(this.options[this.selectedIndex].text);
		} else {
			$("#catdiv").hide("slow");
		}
	});

	
	$("#catsavebtn").click(function() {
		var newName = $("#catname").val();
		$("#catselect option:selected").each(function () {
			$(this).text(newName);
		});
		
		$("#msgdiv").load("adminupdate.php?a=savecat&id=" + String($("#catselect").selectedValues()), {name:newName});
		
		$("#catselect").attr({selectedIndex:-1});	
		$("#catdiv").hide();
	});

	
	$("#catcancelbtn").click(function() {
		$("#catdiv").hide("fast");
		$("#catselect").attr({selectedIndex:-1});	
		$("#msgdiv").text("Category edit canceled.");
	});
	

	$("#catdeletebtn").click(function() {
		var selVal = String($("#catselect").selectedValues());
		$("#msgdiv").load("adminupdate.php?a=deletecat&id=" + selVal, {}, function() {
			if ($("#msgdiv").text() == "Category deleted.") {
				$("#catselect").removeOption(selVal);
				$("#catselect").attr({selectedIndex:-1});	
				$("#catdiv").hide("fast");
			}
		});
	});
	
	
	$("#cataddbtn").click(function() {
		var maxID = 0;
		$("#catselect option").each(function(i) {
			if (Number(this.id) > maxID) {
				maxID = Number(this.id);
			}
		});
		maxID++;
		$("#catselect").addOption(String(maxID), "new category");
		$("#msgdiv").load("adminupdate.php?a=addcat&id=" + maxID, {}, function() {
			$("#catname").val("new category");
			$("#catdiv").show("fast");
			$("#itemdiv").hide("fast");
			$("#moditemdiv").hide("fast");
		});
	});
	
	
	$("#catmoditemsbtn").click(function() {
		var catName = "";
		var catID = "";
		$("#catselect option:selected").each(function () {
			catName = this.text;
			catID = this.id;
		});
		$("#catdiv").hide();
		$("#moditemstitle").text("Modify Items for category '" + catName + "'");
		
		//load items listbox
		$("#itemselect").ajaxAddOption("adminupdate.php?a=loaditems&cid=" + catID, {}, false, function() {
			$("#itemdiv").show("fast");		
			$("#itemselect").attr({selectedIndex:-1});	
		});
	});


	$("#itemselect").change(function() {
		if (this.id > '0') {
			$("#moditemdiv").hide();
			$("#moditemdiv").show("fast");
			$("#itemname").val(this.options[this.selectedIndex].text);
		} else {
			$("#moditemdiv").hide("slow");
		}
	});

	
	$("#itemaddbtn").click(function() {
		var maxItemID = 0;
		$("#itemselect option").each(function(i) {
			if (Number(this.id) > maxItemID) {
				maxItemID = Number(this.id);
			}
		});
		maxItemID++;
		$("#itemselect").addOption(String(maxItemID), "new item");
		$("#catselect option:selected").each(function () {
			catID = this.id;
		});
		$("#msgdiv").load("adminupdate.php?a=additem&cid=" + catID + "&iid=" + maxItemID, {}, function() {
			$("#itemname").val("new item");
			$("#moditemdiv").show("fast");
		});
	});		

		
	$("#itemsavebtn").click(function() {
		var newName = $("#itemname").val();
		
		$("#itemselect option:selected").each(function () {
			$(this).text(newName);
		});
		
		$("#msgdiv").load("adminupdate.php?a=saveitem&cid=" + String($("#catselect").selectedValues()) + "&iid=" + String($("#itemselect").selectedValues()), {name:newName}, function() {
			$("#moditemdiv").hide();
			$("#itemselect").attr({selectedIndex:-1});	
		});
		
	});

	
	$("#itemmodcancelbtn").click(function() {
		$("#moditemdiv").hide("fast");
		$("#itemselect").attr({selectedIndex:-1});	
		$("#msgdiv").text("Item edit canceled.");
	});

		
	$("#itemcancelbtn").click(function() {
		$("#itemdiv").hide("fast");
		$("#moditemdiv").hide("fast");
		$("#itemselect").attr({selectedIndex:-1});	
		$("#msgdiv").text("Item edit canceled.");
		$("#catdiv").show("fast");
	});
	
	
	$("#itemdeletebtn").click(function() {
		var selVal = String($("#itemselect").selectedValues());
		$("#msgdiv").load("adminupdate.php?a=deleteitem&cid=" + String($("#catselect").selectedValues()) + "&iid=" + selVal, {}, function() {
			if ($("#msgdiv").text() == "Item deleted.") {
				$("#itemselect").removeOption(selVal);
				$("#itemselect").attr({selectedIndex:-1});	
				$("#moditemdiv").hide("fast");
			}
		});
	});
	
	
	$("#catselect").attr({selectedIndex:-1});	
});
