/**
 * @(#) DynamicMenu.js  1.0 1/5/2005
 * Copyright (c) 2005 Anime-Networks.com. All Rights Reserved.
 */

function DynamicMenu(id)
{
    var self = this;
    this.id = id;
    this.tree = new Node("tree", "", null, new Array(), false, true);
    this.allNodes = {}; // id => object
    this.imgObjects = [];
    this.count = 0;

    this.path = "images/";
    this.color = "orange";
    this.img = { "arrow_on": "arrow_on.gif", "arrow_off": "arrow_off.gif" };

    this.init = function()
    {
        var p, img;
        for (p in this.img)
        {
            this.img[p] = this.path + this.img[p];
        }
        for (p in this.img)
        {
            this.imgObjects.push(new Image());
            this.imgObjects.getLast().src = this.img[p];
            this.img[p] = this.imgObjects.getLast().src;
        }
        this.parse(document.getElementById(this.id).childNodes, this.tree, 1);
        this.updateHtml();
    };

    this.parse = function(nodes, tree)
    {
        for (var i = 0; i < nodes.length; i++)
        {
            if (nodes[i].nodeType == 1)
            {
                if (!nodes[i].className) { continue; }
                if (!nodes[i].id)
                {
                    nodes[i].id = this.id + "-" + (++this.count);
                }
                var node = new Node();
                node.id = nodes[i].id;
                if (nodes[i].firstChild)
                {
                    if (nodes[i].firstChild.tagName == "A")
                    {
                        var a = nodes[i].firstChild;
                        if (a.firstChild)
                        {
                            node.text = a.firstChild.nodeValue.trim();
                        }
                        if (a.href)
                        {
                            node.href = a.href;
                        }
                        if (a.title)
                        {
                            node.title = a.title;
                        }
                        if (a.target)
                        {
                            node.target = a.target;
                        }
                    }
                    else
                    {
                        node.text = nodes[i].firstChild.nodeValue.trim();
                    }
                }
                node.parentNode = tree;
                node.childNodes = (nodes[i].className == "folder" ? new Array() : null);
                node.isDoc      = (nodes[i].className == "doc");
                node.isFolder   = (nodes[i].className == "folder");
                tree.childNodes.push(node);
                this.allNodes[node.id] = node;
            }
            if (nodes[i].nodeType == 1 && nodes[i].childNodes)
            {
                this.parse(nodes[i].childNodes, tree.childNodes.getLast());
            }
        }
    };

    function Node(id, text, parentNode, childNodes, isDoc, isFolder)
    {
        this.id = id;
        this.text = text;
        this.parentNode = parentNode;
        this.childNodes = childNodes;
        this.isDoc = isDoc;
        this.isFolder = isFolder;
        this.href = "";
        this.title = "";
        this.target = "";
        this.isLast = function()
        {
            if (this.parentNode)
            {
                return this.parentNode.childNodes.getLast().id == this.id;
            }
            throw "DynamicMenu.Node.isLast() failed, this func cannot be called for the root element";
        };
        this.toHtml = function()
        {
            var s = '';
            if (this.isFolder)
            {
                s += '<img class="section" src="?schemes/?/menu_?.gif" /><br />'.format(self.path, self.color, this.text.toLowerCase().replace(/[^a-z0-9]/g, '_'));
                if (this.childNodes.length)
                {
                    for (var i = 0; i < this.childNodes.length; i++)
                    {
                        s += this.childNodes[i].toHtml();
                    }
                    s += '<br />';
                }
            }
            if (this.isDoc)
            {
                s += '<span><img class="arrow" hspace="3" src="?" />'.format(self.img.arrow_off);
                s += '<a class="link" onmouseover="return ?.mouseOver(this);" onmouseout="?.mouseOut(this)" href="?"???>?</a>'.format(self.id, self.id, (this.href.substring(0,11).toLowerCase() == 'javascript:' ? 'javascript:void(0)' : this.href), (this.href.substring(0,11).toLowerCase() == 'javascript:' ? ' onclick="?"'.format(this.href.substring(11)) : '') , (this.title ? ' title="?"'.format(this.title) : ""), (this.target ? ' target="?"'.format(this.target) : ""), this.text);
                s += '<br /><img class="divider" vspace="2" /><br /></span>';
            }
            return s;
        };
    }

    this.nodeArrow = function(a)
    {
        arrow = a.parentNode.childNodes[0];
        arrow.src = (arrow.src.indexOf(self.img.arrow_on) != -1) ? self.img.arrow_off : self.img.arrow_on;
    };
    
    this.mouseOver = function(a)
    {
        this.nodeArrow(a);
        window.status = a.firstChild.nodeValue.trim();
        return true;
    };
    
    this.mouseOut = function(a)
    {
        this.nodeArrow(a);
        window.status = window.defaultStatus;
        return true;
    }

    this.toHtml = function()
    {
        var s = "";
        var nodes = this.tree.childNodes;
        for (var i = 0; i < nodes.length; i++)
        {
            s += nodes[i].toHtml();
        }
        return s;
    };
    
    this.updateHtml = function()
    {
        document.getElementById(this.id).innerHTML = this.toHtml();
    };
}

/* Check whether array contains given string */
if(!Array.prototype.contains){Array.prototype.contains=function(s){for(var i=0;i<this.length;++i){if(this[i]===s){return true;}}return false;};}
/* Remove elements with such value (mutates) */
if(!Array.prototype.removeByValue){Array.prototype.removeByValue=function(value){var i,indexes=[];for(i=0;i<this.length;++i){if(this[i]===value){indexes.push(i);}}for(i=indexes.length-1;i>=0;--i){this.splice(indexes[i],1);}};}
/* Remove elements judged 'false' by the passed function (mutates) */
if(!Array.prototype.filter){Array.prototype.filter=function(func){var i,indexes=[];for(i=0;i<this.length;++i){if(!func(this[i])){indexes.push(i);}}for(i=indexes.length-1;i>=0;--i){this.splice(indexes[i],1);}};}
/* Get the last element from the array */
if(!Array.prototype.getLast){Array.prototype.getLast=function(){return this[this.length-1];};}
/* Strip whitespace from the beginning and end of a string */
if(!String.prototype.trim){String.prototype.trim=function(){return this.replace(/^\s*|\s*$/g,"");};}
/* Replace ? tokens with variables passed as arguments in a string */
String.prototype.format=function(){if(!arguments.length){throw"String.format() failed, no arguments passed, this = "+this;}var tokens=this.split("?");if(arguments.length!=(tokens.length-1)){throw"String.format() failed, tokens != arguments, this = "+this;}var s=tokens[0];for(var i=0;i<arguments.length;++i){s+=(arguments[i]+tokens[i+1]);}return s;};