var postEditorCategories = {
	init : function() {
		jQuery("#blog-category-select,#blog-category-select-2").click(postEditorCategories.firstListener);
	},
	
	/********************************************
	*********** Listeners ***********************
	*********************************************
	These will listen for changes in the category selectors and dynamically fill
	in the subcategory selections.
	*/
	firstListener : function() {
		var parent = jQuery(this).val();
		var id = this.id;
		var first = (id == "blog-category-select" ? true : false);
		
		// We will hide them, and show them only if necessary
		if(first) {
			//jQuery("#blog-category-select-2,#blog-category-select-3").hide();
			jQuery("#blog-category-select-3").html("");
		} //else jQuery("#blog-category-select-3").hide();
		
		if(parent != "") {
			jQuery.get("/ai/admin/category-define.api/" + getTime(),
				{ "action" : "fetch",
				  "parent" : parent },
				function(jdata) {
					if(jdata.errno == 0)
						postEditorCategories.fillSelect(first ? "#blog-category-select-2" : "#blog-category-select-3", jdata.tags);
					else
						alert("Error with Categories, please report this error.");
				},
				"json"
			);
		}
	},
	
	/*********************************************
	************* Select Filler ******************
	**********************************************
	Takes data from the parent method and drops it into
	the new select function.
	*/
	fillSelect : function(select, tags) {
		/*var filler = jQuery.create("option", {"value" : ""}, "Select One...");
		jQuery(select).html("").append(filler);*/
		jQuery(select).html("");
		
		jQuery.each(tags, function() {
			var option = jQuery.create("option", {"value" : this.id}, this.tag);
			jQuery(select).append(option);
		});
		
		if(jQuery.makeArray(tags).length > 0) jQuery(select).show();
	},
	
	/*********************************************
	*************** Returns the Category *********
	**********************************************/
	getCategory : function() {
		var first = jQuery("#blog-category-select").val();
		var second = jQuery("#blog-category-select-2").val();
		var third = jQuery("#blog-category-select-3").val();
		
		if(third != "" && third != null) {
			return third;
		} else if(second != "" && second != null) {
			return second;
		} else {
			return first;
		}
	}
};

jQuery(document).ready(function() {
	postEditorCategories.init();
});

