(function($){
	$.fn.menu_toggle = function(options){
		
		var defaults = {
			isExpansion:false,
			url: "/cate_list_ajax.php"
		};
		
		var opts = $.extend(defaults, options);
		
		//初始化父类
		if(defaults.isExpansion){
			$("div.cate_box").each(function(){
				$(this).css("display", "block");
			})
		}else{
			togglerInit();
		}
		
		//获取数据
		function _getData(obj,paras){
			$.ajax({
				url:defaults.url,
				type:"post",
				data:paras,
				dataType:"html",
				success:function(data){
					obj.after(data);
					box = _this.next("div.cate_box").eq(0);
					tree = box.find("div.cate_item");
					box.css("display", "block");
					togglerImgInit(tree);
				}
			});
		}
		
		function togglerImgInit(obj){
			obj.click(function(){
				_this = $(this);
				isfolder = _this.attr("isfolder");
				cateid 	 = _this.attr("cateid");
				
				if(_this.hasClass("cate_parent") && !defaults.isExpansion){
					show = $.cookie("Cate_" + cateid);
					$.cookie("Cate_" + cateid, show == 1 ? 0 : 1);
				}
				
				if(isfolder == 1){
					tree = _this.next("div.cate_box");
					if(tree.length == 0){
						_getData(_this, {cateid:cateid, index:_this.attr("index")});
					}else{
						tree.toggle();
					}
				}
			});
		}
		
		function togglerInit(){
			
			$("div.cate_parent").each(function(){
				_this 	= $(this);
				cateid 	= _this.attr("cateid");
				show = $.cookie("Cate_" + cateid);
				
				if(show == 1){
					tree = _this.next("div.cate_box");
					if(tree.length>0) tree.toggle();
					
				}
			})
		}
		
		togglerImgInit($("div.cate_item"));
	}
})(jQuery);