﻿/**
 * @author Mark
 * @usage:
 *         new TreeUI( 
 *                     { currentNode: "", 
 *                       rootNode: "",
 *                       ui: { navigator: $("#regionPanel > .title").get(0), 
 *                             content: $("#regionPanel > .content").get(0) 
 *                           } 
 *                     });
 */

Tree = function() { this.initialize.apply(this, arguments); };

(function($) {
    Tree.prototype = {
        initialize: function(args) {
            this.currentNode = args.currentNode;
            this.currentName = args.currentName;
            this.rootNode = args.rootNode;
            this.onLoad = args.onLoad || function() { };
            this.onChanging = args.onChanging || function() { };
            this.onChanged = args.onChanged || function() { };
            this.onAddNavigator = args.onAddNavigator || function() { };
            this.onAddedNavigator = args.onAddedNavigator || function() { };
            this.onAddNode = args.onAddNode || function() { };
            this.data = null;
            this.bindToUi(args.ui);
        },
        bindToUi: function(ui) {
            this.ui = ui || this.createUi();
            if (this.currentNode == null) {
                var thisObj = this;
                this._asyncGetNodeId(this.currentName, this.rootNode, function(currentNode) {
                    if (currentNode == null) {
                        currentNode = "4bc9bfe3-8d1e-de11-9504-000000004188";
                        currentName = I18N.Industry;
                    }
                    thisObj.currentNode = currentNode;
                    thisObj.onLoad(thisObj.currentNode);
                    thisObj.navigator(thisObj.currentNode);
                });
            } else {
                this.onLoad(this.currentNode);
                this.navigator(this.currentNode);
            }
        },
        navigator: function(nodeId) {
            var thisObj = this;
            this._asyncGetTreeData(nodeId, this.rootNode, function(node) {
                if (node) {
                    with ($(thisObj.ui.navigator)) {
                        // clear old
                        find(".node,.split").remove();
                        // append new
                        var current = node;
                        while (current != null && current.GenericCategoryId != null) {
                            if (thisObj.onAddNavigator(current, find("a.node:first").get(0)) != false) {
                                var parentName = current.Parent && current.Parent.Parent && current.Parent.Parent.Parent ? current.Parent.Name : "";
                                prepend("<a class='node" + (current == node ? " current" : "") +
                                        "' href='javascript:void(0)' nodeid='" + current.GenericCategoryId + "' parent='" + parentName + "'>" +
                                            current.Name + "</a>" + (current != node ? "<span class='split'> » </span>" : ""));
                                thisObj.onAddedNavigator(current, find("a.node:first").get(0));
                            }
                            current = current.Parent;
                        }
                        prepend(find(":not(.node,.split)"));
                    }
                    with ($(thisObj.ui.content)) {
                        // clear old
                        find(".node").remove();
                        // append new
                        for (var i = 0; i < node.Children.length; i++) {
                            if (thisObj.onAddNode(node.Children[i], find("a.node:last").get(0)) != false) {
                                append("<a class='node' href='javascript:void(0)' nodeid='" + node.Children[i].GenericCategoryId + "'>" +
                                        node.Children[i].Name + "</a>");
                            }
                        }
                        if (find(".node").length == 0 && is(":visible"))
                            $(".btn", thisObj.ui.navigator).click();
                    }
                    $([thisObj.ui.navigator, thisObj.ui.content]).find(".node").click(function(e) {
                        thisObj.navigator($(this).attr("nodeid"));
                        thisObj.onChanging($(this).text(), $(this).attr("parent") + " " + $(this).text());
                        //thisObj = null;
                    });
                    $(thisObj.ui.navigator).find(".node").click(function() {
                        if ($(thisObj.ui.content).is(":hidden"))
                            $(".btn", thisObj.ui.navigator).click();
                    });

                    thisObj.node = node;
                    thisObj.onChanged(node.Name, (node.Parent && node.Parent.Parent && node.Parent.Parent.Parent ? node.Parent.Name : "") + " " + node.Name);
                }
            });
        },
        createUi: function(navigatorElt, contentElt) {
            if (navigatorElt == null)
                navigatorElt = $("<div class='title'></div>").get(0);
            if (contentElt == null)
                contentElt = $("<div class='content'></div>").get(0);

            $("span.btn", navigatorElt).click(function() {
                if ($(this).parent().next().css("display") == "none") {
                    $(this).removeClass("hide");
                    $(this).parent().next().slideDown(200);
                } else {
                    $(this).addClass("hide");
                    $(this).parent().next().slideUp(200);
                }
            });

            return {
                navigator: navigatorElt,
                content: contentElt
            };
        },
        _asyncGetTreeData: function(nodeId, rootNodeId, fn) {
            // GLT Location
            // GIT Category
            $.get("traveltongcommondata.axd", {
                action: "GGC", GCId: nodeId, RGCId: rootNodeId, langId: I18N.langCode, level: "1", format: "json"
            }, function(data) {
                eval("data = " + data + ";");
                fn(data);
            });
        },
        _asyncGetNodeId: function(nodeName, rootNodeId, fn) {
            $.get("traveltongcommondata.axd", {
                action: "GGCBKW", KW: nodeName, RGCId: rootNodeId, LangId: I18N.langCode, level: "1", format: "json"
            }, function(data) {
                eval("data = " + data + ";");
                fn(data.GenericCategoryId);
            });
        }
    }
})(jQuery);