/*
 * Async Treeview 0.1 - Lazy-loading extension for Treeview
 *
 * http://bassistance.de/jquery-plugins/jquery-plugin-treeview/
 *
 * Copyright (c) 2007 Jörn Zaefferer
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: $Id$
 *
 */



;(function($) {
	
function load(settings, root, child, container) {
	var wait = true;
	//$.getJSON(settings.url, {prod: root}, function(response) {
	$.ajax({
			  async:false,
			  url: settings.url,
			  dataType: 'json',
			  data: {prod: root},
			  success: function(response) {
			

		function createNode(parent) {

			//var current = $("<li/>").attr("id", this.id || "").html("<span>" + this.text + "</span>").appendTo(parent);
			if (this.url && !this.hasChildren) {
				if (!this.childCount || this.childCount == 0) {
					var current = $("<li/>").attr("id", this.id || "").html("<a href=\"" + this.url + "\">" + this.label + "</a>").appendTo(parent);
				}
				else {
					var current = $("<li/>").attr("id", this.id || "").html("<a href=\"" + this.url + "\">" + this.label + " (" + this.childCount + ") </a>").appendTo(parent);
				}
			}
			else {
				if (!this.childCount || this.childCount == 0) {
					var current = $("<li/>").attr("id", this.id || "").html("<span>" + this.label + "</span>").appendTo(parent);
				}
				else {
					var current = $("<li/>").attr("id", this.id || "").html("<span>" + this.label + " (" + this.childCount + ") </span>").appendTo(parent);
				}
			}
			if (this.classes) {
				current.children("span").addClass(this.classes);
			}
			if (this.expanded) {
				current.addClass("open");
			}

			if (this.hasChildren || this.children && this.children.length) {
				var branch = $("<ul/>").appendTo(current);
				if (this.hasChildren) {
					current.addClass("hasChildren");
					createNode.call({
						label:"placeholder",
						id:"placeholder",
						children:[]
					}, branch);
				}
				if (this.children && this.children.length) {
					$.each(this.children, createNode, [branch]);
				}
				inRootLine = false;
				for(var i=0; i<activeProdIDs.length;i++)
				{
					if(this.id==activeProdIDs[i])
					{
						inRootLine = true;
						branch.empty();
						load(settings, activeProdIDs[i], branch, this);
						current.addClass("open");

						break;
					}
				}
				if(!inRootLine)
				{
					current.addClass("closed");
				}
			}
			else {
				for(var i=0; i<activeProdIDs.length;i++)
				{
					if(this.id==activeProdIDs[i])
					{
						current.addClass("act");
						break;
					}
				}
			}

		}
		$.each(response, createNode, [child]);
        $(container).treeview({add: child});
        wait = false;
	
    }});

}

var proxied = $.fn.treeview;
$.fn.treeview = function(settings) {
	if (!settings.url) {
		return proxied.apply(this, arguments);
	}
	var container = this;
	load(settings, "source", this, container);
	var userToggle = settings.toggle;
	return proxied.call(this, $.extend({}, settings, {
		collapsed: true,
		toggle: function() {
			var $this = $(this);
			if ($this.hasClass("hasChildren") && $this.hasClass("collapsable")) {
				var childList = $this.removeClass("hasChildren").find("ul");
				childList.empty();
				load(settings, this.id, childList, container);
			}
			if (userToggle) {
				userToggle.apply(this, arguments);
			}
		}
	}));
};

})(jQuery);