/*
* Async Treeview 0.1 - Lazy-loading extension for Treeview
* 
* http://bassistance.de/jquery-plugins/jquery-plugin-treeview/
*
* Copyright (c) 2007 John 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) {
        $.getJSON(settings.url, { root: root }, function(response) {
            function createNode(parent) {
                var contextMenuUrl = this.ContextMenuUrl;
                var urlStr = "";
                var href = "javascript:void(0);";
                $.each(contextMenuUrl, function(i, url) {
                    if (i == 0) {
                        urlStr = url;
                    } else {
                        urlStr = urlStr + "|" + url;
                    }
                });
                if (this.HasChildren) {
                    href = contextMenuUrl[2];
                } else {
                    href = contextMenuUrl[0];
                }
                var current = $("<li/>").attr("id", this.ID || "").html("<a href='" + href + "' id='" + urlStr + "' target='main'>" + this.Text + "</a>").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: "Loading...",
                            ID: "loading",
                            Children: [],
                            ContextMenuUrl: []
                        }, branch);
                        current.find("#loading a").addClass("placeholder");
                    }
                    if (this.Children && this.Children.length) {
                        $.each(this.Children, createNode, [branch])
                    }
                }
            }
            var temp = $(child).html();
            child.empty();
            $.each(response, createNode, [child]);
            $(container).append(temp);
            $(container).treeview({ add: child });
            bindContextMenu();
        });
    }

    var proxied = $.fn.treeview;
    $.fn.treeview = function(settings) {
        if (!settings.url) {
            return proxied.apply(this, arguments);
        }
        var container = this;
        load(settings, settings.root, 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);

function bindContextMenu() {
    $('#tree li a').contextMenu('treeMenu', {
        onShowMenu: function(e, menu) {
            var contextMenuUrl = $(e.target).attr("id").split("|");
            menu.find("li a").each(function(i) {
                $(this).attr("href", contextMenuUrl[i]);
                if ($(this).attr("href") == "javascript:void(0);") {
                    $(this).parent().remove();
                }
            });
            return menu;
        },
        onContextMenu: function(e) {
            if ($(e.target).hasClass('dontShow')) return false;
            if ($(e.target).attr("href") == "javascript:void(0);" || $(e.target).attr("href") == "undefined") return false;
            return true;
        }
    });
}


