;(function($) {

function load(settings, root, child, container) {

	$.ajax({

	   url:settings.url,

	   dataType: "json",

	   data:"root="+root,

	   error:function(){alert("请刷新！")},

	   success:function(response){

		$("#treeview").empty();

		function createNode(parent) {

					var current = $("<li/>").attr("id", this.id || "").html("<span>" + this.text + "</span><b></b>").appendTo(parent);

					if (this.hasChildren || this.children && this.children.length) {

						var branch = $("<ul/>").appendTo(current);

						if (this.hasChildren) {

							current.addClass("hasChildren");

			              	createNode.call({

								text:"placeholder",

								id:"placeholder",

								children:[]

							}, branch);

						}

					}

					current.parent().parent().find("div").removeClass("treeLoading");

				}



				$.each(response, createNode, [child]);

				$(container).treeview({add: child});

				dcampusUI._setContainerHeight();

		}

	});

	

}





function loadChildren(settings, root, child, container) {

	$.ajax({

	   url:settings.url,

	   dataType: "json",

	   data:"root="+root,

	   error:function(){alert("请刷新！")},

	   success:function(response){

		function createNode(parent) {	
		//房屋中介
    	if(this.link == "content.jsp?forumID=541") {
	 	  this.link = "automatch/index.jsp?forumID=541"; 
		}
	var current = $("<li/>").attr("id", this.id || "").html("<a rel='"+this.id+"' href='"+this.link+"' target='mainFrame'>" + this.text + "</a>").appendTo(parent);

					if (this.hasChildren || this.children && this.children.length) {

						var branch = $("<ul/>").appendTo(current);

						if (this.hasChildren) {

							current.addClass("hasChildren");

							current.find("a").eq(0).replaceWith("<a href='"+this.link+"' target='mainFrame' class='childNode' rel='"+this.id+"'>" + this.text + "</a><b></b>");

			              	createNode.call({

								text:"placeholder",

								id:"placeholder",

								children:[]

							}, branch);

						} 

					}

					current.parent().parent().find("div").removeClass("treeLoading");

				}

				

				$.each(response, createNode, [child]);

				$(container).treeview({add: child});

				dcampusUI._setContainerHeight();

            $(".treeview").find("a").bind("click",function(){

			 

			   $("#treeview").find("a").removeClass("currentColor");

			   $(this).addClass("currentColor");

	        });		

		}

	});
}


var proxied = $.fn.treeview;
$.fn.treeview = function(settings) {
	if (!settings.url) {
		return proxied.apply(this, arguments);
	}

	var container = this;

	load(settings, 0, this, container);

	var userToggle = settings.toggle;

	return proxied.call(this, $.extend({}, settings, {

		collapsed: true,

		toggle: function() {

			var $this = $(this);

		    if($(this).find("ul").eq(0).find("li").eq(0).attr("id")== "placeholder") {

				$(this).find("div").addClass("treeLoading");

			}
			if ($this.hasClass("hasChildren")) {

				var childList = $this.removeClass("hasChildren").find("ul");

				childList.empty();

				loadChildren(settings, this.id, childList, container);

			}

			if (userToggle) {

				userToggle.apply(this, arguments);

			}

		}

	}));
};
})(jQuery);