var nav = {
    menu: [
        { id: 'nav-home', href: '/', text: 'Home' },
        { id: 'nav-news', text: 'News' , menu: [
            { href: '/news/news.html', text: 'Latest News' },
            { text: 'Game Updates', menu: [
                { href: '/news/graciaepilogue_01.html', text: 'Gracia Epilogue' },
                { href: '/news/graciafinal_01.html', text: 'Gracia Final' },
                { href: '/news/graciapt2_01.html', text: 'Gracia Part 2' },
                { href: '/news/gracia_01.html', text: 'Gracia Part 1' },
                { href: '/news/hellbound_01.html', text: 'Hellbound' },
                { href: '/news/kamael_01.html', text: 'The Kamael' },
                { href: '/news/interlude_01.html', text: 'Interlude' },
                { href: '/news/chronicle5_01.html', text: 'Chronicle 5' },
                { href: '/news/chronicle4_01.html', text: 'Chronicle 4' },
                { href: '/news/chronicle3_01.html', text: 'Chronicle 3' },
                { href: '/news/chronicle2_01.html', text: 'Chronicle 2' },
                { href: '/news/chronicle1_01.html', text: 'Chronicle 1' },
                { href: '/news/career.html', text: 'Prelude' }
            ] },
            { href: '/news/development.html', text: 'Server Maintenance' },
            { href: '/news/press.html', text: 'In the Press' },
            { href: '/news/event.html', text: 'Events' },
            { href: '/news/newsletter_archive.html', text: 'Newsletter Archive' }
        ] },
        { id: 'nav-gamebackground', text: 'Game Background' , menu: [
            { href: '/background/about.html', text: 'Overview' },
            { text: 'Myths & History', menu: [
                { href: '/background/interlude/list.html', text: 'Interlude' },
                { href: '/background/chronicle5/list.html', text: 'Chronicle 5' },
                { href: '/background/chronicle4/list.html', text: 'Chronicle 4' },
                { href: '/background/chronicle3/list.html', text: 'Chronicle 3' },
                { href: '/background/chronicle2/list.html', text: 'Chronicle 2' },
                { href: '/background/chronicle/list.html', text: 'Chronicle 1' },
                { href: '/background/legends.html', text: 'Prelude' },
                { href: '/background/legends_list.html', text: 'The Legend' }
            ] },
            { href: '/background/dev_qa.html', text: 'Making Lineage II' },
            { href: '/background/credits.html', text: 'Game Credits' }
        ] },
        { id: 'nav-newplayerguide', text: 'New Player Guide' , menu: [
            { href: '/guide/new_l2.html', text: 'New to Lineage II' },
            { href: 'http://www.discoverlineage2.com', text: 'Discover Lineage II' },
            { href: '/guide/systemneeds.html', text: 'System Requirements' },
            { text: 'Getting Started', menu: [
                { href: '/guide/install.html', text: 'Installation' },
                { href: '/guide/create.html', text: 'Creating Your Character' },
                { href: '/guide/interface.html', text: 'Game Interface' },
                { href: '/guide/tips.html', text: 'Shortcuts & Tips' }
            ] },
            { text: 'Character Appearance', menu: [
                { href: '/guide/appearance_hairstyles.html', text: 'Hairstyles' },
                { href: '/guide/appearance_hair_colors.html', text: 'Hair Colors' },
                { href: '/guide/appearance_face_options.html', text: 'Face Options' }
            ] },
            { text: 'Player Commands', menu: [
                { href: '/guide/commands.html', text: 'Player Commands' },
                { href: '/guide/macros.html', text: 'Macros' },
                { href: '/guide/alt_commands.html', text: 'Alt Commands' }
            ] },
            { text: 'Adventure Bonuses', menu: [
                { href: '/guide/support_magic.html', text: 'Adventurer\'s Support Magic' },
                { href: '/guide/vitality_system.html', text: 'Vitality System' },
                { href: '/guide/newbie_quest_guide.html', text: 'Newbie Quest Guide' },
                { href: '/guide/miscellaneous.html', text: 'Miscellaneous' }
            ] },
            { href: '/guide/game_guides.html', text: 'Game Guides' }
        ] },
        { id: 'nav-thelibrary', text: 'The Library' , menu: [
            { text: 'Quests', menu: [
                { href: '/Knowledge/quest.html', text: 'Starting Villages' },
                { href: '/Knowledge/quest2.html', text: '1st Class Transfer' },
                { href: '/Knowledge/quest3.html', text: 'Level 15-40' },
                { href: '/Knowledge/quest4.html', text: '2nd Class Transfer' },
                { href: '/Knowledge/quest5.html', text: 'Level 41+' },
                { href: '/Knowledge/quest6.html', text: '3rd Class Transfer' },
                { href: '/Knowledge/quest7.html', text: 'Clan-Related Quests' }
            ] },
            { text: 'Monsters', menu: [
                { href: '/Knowledge/monsters_6.html', text: 'Interlude' },
                { href: '/Knowledge/monsters_5.html', text: 'Chronicle 5' },
                { href: '/Knowledge/monsters_4.html', text: 'Chronicle 4' },
                { href: '/Knowledge/monsters_3.html', text: 'Chronicle 3' },
                { href: '/Knowledge/monsters_2.html', text: 'Chronicle 2' },
                { href: '/Knowledge/monsters_1.html', text: 'Chronicle 1' }
            ] },
            { text: 'Items & Equipment', menu: [
                { href: '/Knowledge/items_overview.html', text: 'Weapon & Armor Overview' },
                { href: '/Knowledge/item.html', text: 'Weapons' },
                { href: '/Knowledge/item.html?category=armor', text: 'Armor' },
                { href: '/Knowledge/item.html?category=accessories', text: 'Jewelry' },
                { href: '/Knowledge/item.html?category=etc', text: 'Misc.' },
                { href: '/Knowledge/item_sets.html', text: 'Armor Sets' }
            ] },
            { href: '/Knowledge/recipes.html', text: 'Crafting Recipes' },
            { text: 'Enhancements', menu: [
                { href: '/Knowledge/enhancements.html', text: 'Enchantment' },
                { href: '/Knowledge/enhancements_2.html', text: 'Dyes and Symbols' },
                { href: '/Knowledge/enhancements_3.html', text: 'Special Abilities' },
                { href: '/Knowledge/enhancements_4.html', text: 'Weapon Augmentation' },
                { href: '/Knowledge/elemental_attribute_system.html', text: 'Elemental Attribute System' }
            ] },
            { text: 'Transformations', menu: [
                { href: '/Knowledge/transformation_overview.html', text: 'Overview' },
                { href: '/Knowledge/combat_transformations.html', text: 'Combat' },
                { href: '/Knowledge/noncombat_transformations.html', text: 'Non-Combat' },
                { href: '/Knowledge/common_transformations.html', text: 'Common' },
                { href: '/Knowledge/rare_transformations.html', text: 'Rare' },
                { href: '/Knowledge/divine_transformations.html', text: 'Divine' },
                { href: '/Knowledge/other_transformations.html', text: 'Other' }
            ] },
            { text: 'Clans, Allies, & Manors', menu: [
                { href: '/Knowledge/clans.html', text: 'Clans' },
                { href: '/Knowledge/Alliance.html', text: 'Alliances' },
                { href: '/Knowledge/manor.html', text: 'Manor System' }
            ] },
            { text: 'Castle Siege', menu: [
                { href: '/Knowledge/castle.html', text: 'Introduction' },
                { href: '/Knowledge/castle_2.html', text: 'Preparation' },
                { href: '/Knowledge/castle_3.html', text: 'A Siege Begins' },
                { href: '/Knowledge/castle_4.html', text: 'Siege Rules' },
                { href: '/Knowledge/castle_5.html', text: 'Castle Structures & Functions' }
            ] },
            { text: 'Fortresses', menu: [
                { href: '/Knowledge/fortress_introduction.html', text: 'Fortress Overview' },
                { href: '/Knowledge/fortress_rules.html', text: 'Basic Rules' },
                { href: '/Knowledge/fortress_siege_preparation.html', text: 'Preparation' },
                { href: '/Knowledge/siege_process.html', text: 'Siege Process' },
                { href: '/Knowledge/fortress_political_activities.html', text: 'Political Activities' },
                { href: '/Knowledge/fortress_dungeon.html', text: 'Fortress Benefits' }
            ] },
            { text: 'Seven Signs', menu: [
                { href: '/Knowledge/sevensigns.html', text: 'Overview' },
                { href: '/Knowledge/sevensigns_1.html', text: 'Details' },
                { href: '/Knowledge/sevensigns_2.html', text: 'Progress' }
            ] },
            { text: 'Subclass System', menu: [
                { href: '/Knowledge/subclasses.html', text: 'Overview' },
                { href: '/Knowledge/subclasses_1.html', text: 'Details' },
                { href: '/Knowledge/subclasses_2.html', text: 'Available Classes' }
            ] },
            { text: 'Player vs. Player Combat', menu: [
                { href: '/Knowledge/cp.html', text: 'PvP' },
                { href: '/Knowledge/cursed_swords.html', text: 'Cursed Swords' },
                { href: '/Knowledge/dueling.html', text: 'Dueling' }
            ] },
            { text: 'Summon System & Pets', menu: [
                { href: '/Knowledge/summon_0.html', text: 'Summon System' },
                { href: '/Knowledge/pet_1.html', text: 'Pet System' }
            ] },
            { href: '/Knowledge/underground_coliseum.html', text: 'Underground Coliseum' },
            { href: '/Knowledge/noblesse.html', text: 'Noblesse' },
            { href: '/Knowledge/fishing.html', text: 'Fishing' },
            { href: '/Knowledge/hero.html', text: 'Heroes & Olympiad' },
            { text: 'Races, Classes & Skills', menu: [
                { href: '/Knowledge/race_human.html', text: 'Human' },
                { href: '/Knowledge/race_elf.html', text: 'Elf' },
                { href: '/Knowledge/race_darkelf.html', text: 'Dark Elf' },
                { href: '/Knowledge/race_orc.html', text: 'Orc' },
                { href: '/Knowledge/race_dwarf.html', text: 'Dwarf' },
                { href: '/Knowledge/race_kamael.html', text: 'Kamael' }
            ] },
            { href: '/Knowledge/instanced_dungeon_main.html', text: 'Instanced Dungeons' }
        ] },
        { id: 'nav-community', text: 'Community' , menu: [
            { href: 'http://boards.lineage2.com', text: 'Message Boards' },
            { href: '/community/fanart.html', text: 'Fan Art' },
            { href: '/community/fanfiction.html', text: 'Fan Fiction' },
            { href: '/community/fansite_1.html', text: 'Fan Site Listing' },
            { href: '/community/fansitekit.html', text: 'Fan Site Kits' },
            { href: '/community/castlecontrol.html', text: 'Castle Control' },
            { href: '/minigames/index.html', text: 'L2 Fun' },
            { href: '/community/teambios.html', text: 'Team Bios' },
            { href: '/community/spotlight.html', text: 'Spotlight' }
        ] },
        { id: 'nav-downloads', text: 'Downloads' , menu: [
            { href: '/pds/game_dw.html', text: 'Game Download' },
            { href: '/pds/official/screenshots.html', text: 'Official Screenshots' },
            { href: '/pds/players.html', text: 'Players\' Screenshots' },
            { href: '/pds/gracia_movie.html', text: 'Movies' },
            { href: '/pds/pds_music.html', text: 'Music' },
            { href: '/pds/concept_art.html', text: 'Concept Art' },
            { href: '/pds/wp_gracia.html', text: 'Wallpaper' },
            { href: '/pds/map.html', text: 'Maps' }
        ] },
        { id: 'nav-support', text: 'Support' , menu: [
            { href: 'http://help.ncsoft.com/cgi-bin/ncsoft.cfg/php/enduser/std_alp.php?p_sid=WjDPwLCj&p_accessibility=0&p_page=1&p_pv=1.10&p_prods=10&prod_lvl1=10', text: 'Support Center' },
            { href: '/support/security_faq.html', text: 'Security FAQ' },
            { href: 'http://help.ncsoft.com/cgi-bin/ncsoft.cfg/php/enduser/std_adp.php?p_faqid=4654', text: 'Contact Support' },
            { href: 'http://us.ncsoft.com/en/legal/user-agreements/lineage-2-user-agreement.html', text: 'User Agreement' },
            { href: '/legal/rules.html', text: 'Rules of Conduct' }
        ] },
        { id: 'nav-myaccount', text: 'My Account' , menu: [
            { href: 'https://secure.ncsoft.com/cgi-bin/accountManagement.pl', text: 'Account Login' },
            { href: '/my_account/account_services1.html', text: 'Account Services' },
            { href: '/my_account/refer_a_friend.html', text: 'Referral Programs' }
        ] }
    ],
    attachToPage: function (div_name) {
        
        if (div_name) {
            var nav_div = document.getElementById(div_name);
            if (nav_div) {
                nav_div.innerHTML = this.getHTML();
                nav.init();
            }
        }
        else {
            document.write(nav.getHTML());
            nav.init();
            /*
            jQuery(document).bind('ready', function () {
                var old_nav = document.getElementById('navContainer');
                old_nav.setAttribute('id', 'nav');
                old_nav.innerHTML = '<div id="nav-top">&nbsp;</div>\n<div id="nav-main">' + nav.getHTML() + '</div>\n<div id="nav-bottom">&nbsp;</div>\n';
            });
            */
        }

        return(true);
    },
    getHTML: function () {
        var nav_primary = this.menu;
        var html = '\n<ul id="nav-primary">\n';
        for (var i = 0; i < nav_primary.length; i++) {
            html += '\t<li><a id="' + nav_primary[i].id + '" class="nav-primary-button" href="' + ((nav_primary[i].href != undefined) ? nav_primary[i].href : '#') + '">' + nav_primary[i].text + '</a>';
            if (nav_primary[i].menu) {
                var nav_secondary = nav_primary[i].menu;
                html += '\n\t\t<ul class="nav-secondary">\n';
                for (var j = 0; j < nav_secondary.length; j++) {
                    html += '\t\t\t<li>';

                    var html_tertiary = '';
                    if (nav_secondary[j].menu) {
                        var nav_tertiary = nav_secondary[j].menu;
                        
                        html_tertiary += '\n\t\t\t\t<ul class="nav-trinary">\n';
                        for (var k = 0; k < nav_tertiary.length; k++) {
                            html_tertiary += '\t\t\t\t\t<li>';
                            html_tertiary += '<a id="nav-tertiary-' + i + '-' + j + '-' + k + '"';
                            if (nav_tertiary[k].href && (location.href.indexOf(nav_tertiary[k].href) >= 0) && (location.href.indexOf(nav_tertiary[k].href) + nav_tertiary[k].href.length == location.href.length)) { html_tertiary += ' class="active"'; }
                            html_tertiary += ' href="' + ((nav_tertiary[k].href != undefined) ? nav_tertiary[k].href : '#') + '">';
                            html_tertiary += (nav_tertiary[k].text ? nav_tertiary[k].text : '<img src="' + nav_tertiary[k].image + '">') + '</a></li>\n';
                        }
                        html_tertiary += '\t\t\t\t</ul>\n\t\t\t';
                    }

                    html += '<a id="nav-secondary-' + i + '-' + j + '"';
                    if (html_tertiary) { html += ' class="nav-expand"'; }
                    if (nav_secondary[j].href && (location.href.indexOf(nav_secondary[j].href) >= 0) && (location.href.indexOf(nav_secondary[j].href) + nav_secondary[j].href.length == location.href.length)) { html += ' class="active"'; }
                    html += ' href="' + ((nav_secondary[j].href != undefined) ? nav_secondary[j].href : '#') + '">' + nav_secondary[j].text + '</a>';
                    html += html_tertiary + '</li>\n';
                } html += '\t\t</ul>\n\t';
            } html += '</li>\n';
        } html += '</ul>\n';
    
        return html;
    },
    init: function () {
        // Collapse subnavs (until js runs, the subnavs remain open to allow non-js folks to access the menus)
        jQuery("#nav-primary li:has('ul') > a").bind('click keydown', nav.select);
        // TODO: dont hide previously opened nav items from nav.selected
        jQuery("#nav-primary ul:not(':has('a.active')')").hide();
    },
    selected: [],
    select: function (e) {
        if (e.keyCode == 32 || e.keyCode == 13 || e.type == 'click') {
            if (jQuery(this).parent().children('.nav-secondary').size() > 0) {
                jQuery(this).addClass('nav-collapse').removeClass('nav-expand');
                jQuery(this).parent().children('.nav-secondary').slideDown();
            }
            else if (jQuery(this).parent().children('.nav-trinary').size() > 0) {
                jQuery(this).addClass('nav-collapse').removeClass('nav-expand');
                jQuery(this).parent().children('.nav-trinary').slideDown();
            }
            jQuery(this).unbind().bind('click keydown', nav.deselect);

            // track opened navs in nav.selected array (TODO: persist nav.selected across pages using cookies)
            nav.selected.push(jQuery(this).attr('id'));

            return(false);
        }
        return(this);
    },
    deselect: function (e) {
        if (e.keyCode == 32 || e.keyCode == 13 || e.type == 'click') {
            if (jQuery(this).parent().children('.nav-secondary').size() > 0) {
                jQuery(this).removeClass('nav-collapse').addClass('nav-expand');
                jQuery(this).parent().children('.nav-secondary').slideUp();
            }
            else if (jQuery(this).parent().children('.nav-trinary').size() > 0) {
                jQuery(this).removeClass('nav-collapse').addClass('nav-expand');
                jQuery(this).parent().children('.nav-trinary').slideUp();
            }
            jQuery(this).unbind().bind('click keydown', nav.select);

            // track opened navs in nav.selected array (TODO: persist nav.selected across pages using cookies)
            // remove deselected nav from nav.selcted.
            var id = jQuery(this).attr('id');
            for (var i = 0; i < nav.selected.length; i++) {
                if (nav.selected[i] == id) {
                    var rest = nav.selected.slice(i + 1);
                    nav.selected.length = i;
                    nav.selected.push.apply(nav.selected, rest);
                    break;
                }
            }

            return(false);
        }
        return(this);
    },
    setCookie: function (cookieName, cookieValue) {
        var today = new Date();
        var expire = new Date();
        expire.setTime(today.getTime() + 3600000 * 24 * 1);
        document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
    },
    getCookie: function (cookieName) {
        var theCookie = "" + document.cookie;
        var ind = theCookie.indexOf(cookieName);
        if (ind == -1 || cookieName == "") return ""; 
        var ind1 = theCookie.indexOf(';', ind);
        if (ind1 == -1) ind1 = theCookie.length; 
        return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
    }    
};

// Compatibility with legacy nav draw call
var n_e = { output: nav.attachToPage };

// Make sure jQuery is loaded.
var jQueryScriptOutputted = false;
function initJQuery() {
    //if the jQuery object isn't available
    if (typeof(jQuery) == 'undefined') {
        if (! jQueryScriptOutputted) {
            //only output the script once..
            jQueryScriptOutputted = true;
            //output the script (load it from google api)
            document.write("<scr" + "ipt type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js\"></scr" + "ipt>");
        }
        setTimeout("initJQuery()", 50);
    }
    else {
        jQuery(document).bind('ready', function () {
            nav.attachToPage('nav-main');
        });
    }
}
initJQuery();