var navMeta =
{
    "guitars" :
    {
        "link" : "/Guitars",
        "list" :
        {
            "steel string" :
            {
                "link" : "/Guitars/Models",
                "list" :
                {
                    "om" :
                    {
                        "link" : "/Guitars/Models/OM"
                    },
                    "ga" :
                    {
                        "link" : "/Guitars/Models/GA"
                    }
                }
            },
            "classical" :
            {
                "link" : "/Guitars/Classical"
            },
            "prices" :
            {
                "link" : "/Guitars/Prices"
            },
            "photo gallery" :
            {
                "link" : "/Guitars/Photo Gallery",
                "list" :
                {
                    "general" :
                    {
                        "link" : "/Guitars/Photo Gallery/General"
                    },
                    "reserva" :
                    {
                        "link" : "/Guitars/Photo Gallery/Reserva"
                    },
                    "baritone" :
                    {
                        "link" : "/Guitars/Photo Gallery/Baritone",
                        "list" :
                        {
                            "baritone under construction" :
                            {
                                "link" : "/Guitars/Photo Gallery/Baritone/Baritone Under Construction"
                            }
                        }
                    },
                    "classical" :
                    {
                        "link" : "/Guitars/Photo Gallery/Classical"
                    },
                    "grand auditorium" :
                    {
                        "link" : "/Guitars/Photo Gallery/Grand Auditorium"
                    },
                    "12 string grand auditorium" :
                    {
                        "link" : "/Guitars/Photo Gallery/12 String Grand Auditorium"
                    }
                }
            },
            "kind words" :
            {
                "link" : "/Guitars/Kind Words"
            }
        }
    },
    "luthier" :
    {
        "link" : "/Luthier",
        "list" :
        {
            /*
            "contact" :
            {
                "link" : "/Luthier/Contact"
            }
            ,
            "performances" :
            {
                "link" : "/Luthier/Performances"
            }
            */
        }
    },
    "news" :
    {
        "link" : "/News",
        "list" :
        {
            /*
            "currently available" :
            {
                "link" : "/News/Currently Available"
             }
           */
        }
    },
    "links" :
    {
        "link" : "/Links",
        "list" :
        {
        /*
            "site map" :
            {
                "link" : "/Links/Site Map"
            },
            "dealers" :
            {
                "link" : "/Links/Dealers"
            },
        */
            "interesting links" :
            {
                "link" : "/Links/Interesting"
            }
        }
    },
    "contact" :
    {
        "link" : "/Contact"
    }
}

function insertAfter(node, nodeToAdd) {
    node.parentNode.insertBefore(nodeToAdd, node.nextSibling);
}

function createLink(text, href)
{
    var a = document.createElement('a');
    a.appendChild(document.createTextNode(text));
    a.setAttribute('href', href);
    return a;
}

function addLink(parent, text, href)
{
    parent.appendChild(createLink(text, href));
}

function addListItemLink(parent, text, href)
{
    var li = document.createElement('li');
    addLink(li, text, href);
    parent.appendChild(li);
    return li;
}

function fillList(label, list, meta)
{
    var selected = null;
    for (var key in meta)
    {
        var linkValue = meta[key].link;
        var li = addListItemLink(list, key, linkValue);
        if (key == label)
        {
            selected = li;
        }
    }
    return selected;
}

function loadon()
{
    var loc = "" + document.location;
    var path = unescape(loc).toLowerCase().split("/");
    if (path)
    {
        if(path.length<=4)
        {
            path = null;
        }
        else
        {
            path = path.slice(3, path.length - 1);
        }
    }

    var list = document.getElementById('n');
    list.className = 'nav';
    var meta = navMeta;
    var parent = list;
    var sel = fillList(path ? path[0] : "", list, meta);
    if (path)
    {
        for (var level = 0; level<path.length; level++)
        {
            if (meta[path[level]])
            {
                meta = meta[path[level]]['list'];
                var ul = document.createElement('ul');
                ul.className = 'nav'
                if (sel)
                {
                    ul.style.marginLeft = sel.offsetLeft + "px";
                    sel.className = "zz";
                //  sel.setAttribute('class', "zz");
                //  sel.style.backgroundColor = "#777";
                }
                var nextLevel = 1 + level;
                sel = fillList(path[nextLevel], ul, meta);
                insertAfter(parent, ul);
                parent = ul;
            }
        }
    }
}

