/*
 * 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$
 *  
 */
var global_loading = false;
 
;(function($) {

    // Settings should hold information about the entrypoint for the web service (url), the current top article (rootPage) and the current page viewed (currentPage). The root determines where we should start creating our tree.
    function load(settings, root, child, container) {

        $.ajax({ type: "POST", url: settings.url, data: "{Root:" + root + ", RootPage:" + settings.rootPage + ", CurrentPage:" + settings.currentPage + "}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(response) {

            function createNode(parent) {
                var current = $("<li/>").attr("id", this.id || "").html("<a href=" + this.link + " alt=" + this.text + ">" + this.text + "</a>").appendTo(parent);
                if (this.classes) {
                    current.children("a").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({
                            text: "placeholder",
                            id: "placeholder",
                            children: []
                        }, branch);
                    }
                    if (this.children && this.children.length) {
                        $.each(this.children, createNode, [branch])
                    }
                }
            }
            //TODO: Use proper JSON parser.
            $.each(eval('(' + response.d + ')'), createNode, [child]);
            $(container).treeview({ add: child });
            global_loading = false;
        }
        });
    }

    var proxied = $.fn.treeview;
    $.fn.treeview = function(settings) {
        if (!settings.url) {
            return proxied.apply(this, arguments);
        }
        var container = this;
        if ((this[0] != null && this[0].childNodes.length == 0) && !global_loading) {
            global_loading = true;
            load(settings, "-1", this, container);
            var userToggle = settings.toggle;
            return proxied.call(this, $.extend({}, settings, {
                collapsed: true,
                toggle: function() {
                    var $this = $(this);
                    if ($this.hasClass("hasChildren")) {
                        var childList = $this.removeClass("hasChildren").find("ul");
                        childList.empty();
                        load(settings, this.id, childList, container);
                    }
                    if (userToggle) {
                        userToggle.apply(this, arguments);
                    }
                }
            }));
        }
    };
})(jQuery);
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();