/*
 * 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, callback) {
        // CUSTOMIZATION: Nick D'Autremont
        // Changed the parameters being passed to the proxy.
        $.ajaxSetup({ cache: false });
        $.getJSON(settings.url, { root: root, lang: settings.lang, cceid: settings.CCEID, toc: settings.toc }, function(response) {
            function createNode(parent, callback) {
                var current = ""
                // CUSTOMIZATION: Nick D'Autremont
                //  Added onclick event for leafnodes.
                var highlighting = "";
                if (this.id == settings.CCEID) {
                    highlighting = 'style="color: #FF9101; text-decoration: underline;" ';
                }

                var spanAttributes = "prevID='" + this.prevNodeID + "' prevTitle=\"" + this.prevNodeTitle + "\" nextID='" + this.nextNodeID + "' nextTitle=\"" + this.nextNodeTitle + "\"";
                if (this.hasChildren != null && !this.hasChildren) {
                    var strGetContent = this.id + ", " + this.prevNodeID + ", '" + escape(this.prevNodeTitle) + "', " + this.nextNodeID + ", '" + escape(this.nextNodeTitle) + "'";
                    current = $("<li/>").attr("id", this.id || "").html("<span " + spanAttributes + "><a href=\"#\"" + highlighting + "onclick=\"GetContent(" + strGetContent + ");return false;\" class=\"" + this.id + "\">" + this.text + "</a></span>").appendTo(parent);
                }
                else {
                    current = $("<li/>").attr("id", this.id || "").html("<span class='" + this.id + "' " + spanAttributes + highlighting + " >" + this.text + "</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({
                            text: "placeholder",
                            id: "placeholder",
                            children: []
                        }, branch);
                    }
                    if (this.children && this.children.length) {
                        $.each(this.children, createNode, [branch])
                    }
                }
            }

            // CUSTOMIZATION: C. Charlebois
            //  the response can be null/undefined
            if (null != response && undefined != response) {
                $.each(response, createNode, [child]);
            }
            $(container).treeview({ add: child });

            // CUSTOMIZATION: Nick D'Autremont
            //  Initiate our custom callback after the tree is built.
            if(typeof callback == 'function')
            {
                callback.call();
            }
        });
    }

    var proxied = $.fn.treeview;
    $.fn.treeview = function(settings, callback) {
        if (!settings.url) {
            return proxied.apply(this, arguments);
        }
        var container = this;
        load(settings, settings.rootID, this, container, callback);
        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);
