

/**
 * TFolderMenu
 */
var TFolderMenu = TClass.extend(TMenu, {

    /**
     * Constructor
     */
    initialize: function(name) {
        $C(TMenu).initialize.call(this, name);

        this.OnInitExpand = false;

        this.Icon = {
	              Directory   : '', // to be set when class is initialized
    		        root        : 'root.gif',
      		      folder		  : 'folder.gif',
    	  	      folderOpen	: 'folderopen.gif',
    		        item		    : 'item.gif',
    		        empty		    : 'empty.gif',
      		      line		    : 'line.gif',
    	  	      join		    : 'join.gif',
    		        joinBottom	: 'joinbottom.gif',
    		        plus		    : 'plus.gif',
      		      plusBottom	: 'plusbottom.gif',
    	  	      minus		    : 'minus.gif',
    		        minusBottom	: 'minusbottom.gif'
	      };

	      var str = "";
	      for (i in this.Icon) {
	           if (i != "Directory") {
	               str += this.Icon[i] + ";";
	           }
	      }

	      this.fCachedIcons = str;
	      this.fLastSelectedItem = null;
    },


    /**
     * Set icon directory
     */
    setIconDirectory: function(aDirectory) {
        this.Icon.Directory = aDirectory;
    },


    /**
     * Load assets and finish
     */
    loadAssetsAndFinish: function() {
        var tempA = this.fCachedIcons.split(";");
        var temp = new Array();
        for (i in tempA) if (tempA[i]) temp[temp.length] = tempA[i];
        this.fCachedIconsTotalCount = temp.length;
        this.fCachedIconsLoaded = 0;
        for (var i=0; i<temp.length; i++) {
            var o = this;
            var img = new Image();

            if (img.addEventListener) {
                img.addEventListener("load", function() {o.cachedImage_OnLoad()}, false);
            }
            else {
                img.attachEvent("onload", function() {o.cachedImage_OnLoad()});
            }

            img.src = this.Icon.Directory + temp[i];
        }
    },


    /**
     * cachedImage_OnLoad
     */
    cachedImage_OnLoad: function() {
        this.fCachedIconsLoaded++;
        var temp = this.i18n('Loading') + this.fCachedIconsLoaded + "/" + this.fCachedIconsTotalCount;
        var html = '<a href="#" onclick="' + this.getName() + '.apply();" style="font-family:Verdana;font-size:14px;text-decoration:none;">' + temp + '</a>';
        setHtml(this.getName(), html);

        if (this.fCachedIconsLoaded >= this.fCachedIconsTotalCount) {
            this.apply();
            if (this.OnInitExpand) {
                this.toggleAll();
            }
        }
    },


    /**
     * Apply
     */
    apply: function() {
        var html = "<table cellspacing='0' cellpadding='0' border='0' class='" + this.getClassName() + "'><tr><td class='box' style='text-align:left;vertical-align:top'>";

        html += this.root2html();

        for (i in this.tree.subNodes) {
             html += this.node2html(this.tree.subNodes[i], true);
        }

        html += "</td></tr></table>"

        setHtml(this.getName(), html);
    },


    /**
     * Finish
     */
    finish: function() {
        this.apply();
    },


    /**
     * Root to html
     */
    root2html: function() {
        var html = "";
        html += "<table cellspacing='0' cellpadding='0' border='0'>";
        html += "<tr><td style='vertical-align:bottom'>";
        html += "<a href='#' onclick='" + this.getName() + ".toggleAll();return false;' style='outline-style:none;'><img src='" + this.Icon.Directory + this.Icon.root + "' style='display:block' border='0' /></a>";
        html += "</td><td style=''>";
        html += "<img src='/system/assets/images/blank.gif' style='width:4px;' /><span class='root'>" + (this.tree.attributes["text"] ? this.tree.attributes["text"] : "") + "</span>";
        html += "</td></tr></table>";

        return html;
    },


    /**
     * Node to html
     */
    node2html: function(node, includingDivLayer) {
        if (node.attributes["tagname"] == "separator") {
            return "";
        }

        var pNode = this.tree.findNode(node.parentId);

        var html = "";

        if (includingDivLayer)
            html += "<div id='" + node.id + "'>";

        html += "<table cellspacing='0' cellpadding='0' border='0'><tr>";

        var tempNode = pNode;
        var tempA = new Array();
        while (tempNode) {
               if (tempNode.isLastLeaf || (tempNode.id == this.tree.id))
                   tempA[tempA.length] = this.Icon.empty;

               else
                   tempA[tempA.length] = this.Icon.line;

               tempNode = this.tree.findNode(tempNode.parentId);
        }
        tempA.reverse();

        for (var i=1; i<tempA.length; i++)
             html += "<td style='vertical-align:middle'><img src='" + this.Icon.Directory + tempA[i] + "' border='0' style='display:block' /></td>";

        html += "<td style='vertical-align:middle;padding:0px;'>";

        if (node.subNodesCount == 0)
            html += "<img src='" + this.Icon.Directory + (pNode.subNodesCount!=node.zindex ? this.Icon.join : this.Icon.joinBottom) + "' border='0' style='display:block' /></td><td style='vertical-align:middle;padding:0px;'><img src='" + this.Icon.Directory + (node.attributes["icon"]?node.attributes["icon"]:this.Icon.item) + "' border='0' style='display:block' />";
        else if (node.isOpen == true)
            html += "<a href='#' onclick='" + this.getName() + ".toggle(\"" + node.id + "\");return false;' style='outline-style:none;'><img src='" + this.Icon.Directory + (pNode.subNodesCount!=node.zindex ? this.Icon.minus : this.Icon.minusBottom) + "' border='0' style='display:block' /></a></td><td style='vertical-align:middle;padding:0px;'><img src='" + this.Icon.Directory + this.Icon.folderOpen + "' border='0' style='display:block' />";
        else
            html += "<a href='#' onclick='" + this.getName() + ".toggle(\"" + node.id + "\");return false;' style='outline-style:none;'><img src='" + this.Icon.Directory + (pNode.subNodesCount!=node.zindex ? this.Icon.plus : this.Icon.plusBottom) + "' border='0' style='display:block' /></a></td><td style='vertical-align:middle;padding:0px;'><img src='" + this.Icon.Directory + this.Icon.folder + "' border='0' style='display:block' />";

        html += "</td><td style='vertical-align:middle' nowrap='nowrap'>";

        var itemClass = (node.attributes["class"]?node.attributes["class"]:'item');
        if (node.subNodesCount == 0) {
            if (this.fTargetFunction)
                html += "<img src='/system/assets/images/blank.gif' width='4' /><a href='#' onclick='" + this.fTargetFunction + "(" + node.asString() + ");" + this.getName() + ".selectItem(\"" + node.id + "\")" + ";return false;' class='" + itemClass + "' style='outline-style:none;'>" + node.attributes["text"] + "</a>";
            else if (node.attributes["link"])
                html += "<img src='/system/assets/images/blank.gif' width='4' /><a href='" + node.attributes["link"] + "'" + (this.fTargetFrame ? " target='" + this.fTargetFrame + "'" : "") + " onclick='" + this.getName() + ".selectItem(\"" + node.id + "\")" + ";' class='" + itemClass + "' style='outline-style:none;'>" + node.attributes["text"] + "</a>";
            else
                html += "<img src='/system/assets/images/blank.gif' width='4' /><a href='#' class='" + itemClass + "' style='outline-style:none;'>" + node.attributes["text"] + "</a>";
        }
        else
            html += "<img src='/system/assets/images/blank.gif' width='4' /><a href='#' onclick='" + this.getName() + ".toggle(\"" + node.id + "\");return false;' class='" + itemClass + "'>" + node.attributes["text"] + "</a>";

        html += "</td></tr></table>";

        if (node.isOpen == true)
            for (i in node.subNodes)
                 html += this.node2html(node.subNodes[i], true);

        if (includingDivLayer)
            html += "</div>";

        return html;
    },


    /**
     * Select item
     */
    selectItem: function(nodeId) {
       if (this.fLastSelectedItem && elem(this.fLastSelectedItem)) {
           elem(this.fLastSelectedItem).className = "";
       }

       if (elem(nodeId)) {
           elem(nodeId).className = "selectedItem";
           this.fLastSelectedItem = nodeId;
       }
    },


    /**
     * Toggle node
     */
    toggle: function(nodeId) {
       var node = this.tree.findNode(nodeId);
       node.isOpen = (arguments.length==2 ? arguments[1] : !node.isOpen);
       if (document.getElementById(nodeId)) {
           setHtml(nodeId, this.node2html(node, false));
       }
    },


    /**
     * Open node
     */
    openNode: function(key, value) {
       var node = this.tree.findNodeByAttrKey(key, value);
       if (!node) return;

       if (elem(node.id)) {
           node.isOpen = true;
           setHtml(node.id, this.node2html(node, false));
       }
    },


    /**
     * Click node
     */
    clickNode: function(key, value, clazz) {
       var node = this.tree.findNodeByAttrKey(key, value);
       if (!node) return;

       if (this.fTargetFunction) {
           eval(this.fTargetFunction + "(" + node.asString() + ")");
           this.selectItem(node.id);
       }
    },


    /**
     * Toggle all
     */
    toggleAll: function() {
        this.tree.isOpen = !this.tree.isOpen;
        for (i in this.tree.subNodes) {
            this.toggleAllChilds(this.tree.subNodes[i]);
        }

        this.apply();
    },


    /**
     * Toggle all nodes
     */
    toggleAllChilds: function(node) {
        if (node && node.subNodesCount > 0) {
            node.isOpen = this.tree.isOpen;
            for (i in node.subNodes) {
                this.toggleAllChilds(node.subNodes[i]);
            }
        }
    }

});