﻿$(window).addEvent('domready', function() {

    $$('.sub-nav-link').each(function(item, index) {
        $(item).setStyle('background', 'none');
    });

    $$('ul#left-nav > li.toggle > a').each(function(item, index) {

        $(item).set('toggleindex', index);
        var litoggle = $(item).getParent();
        if (litoggle != null) {

            $(item).addEvents({
                'mouseenter': function() {
                    $(this).addClass('hover');

                    if ($(this).hasClass('clean')) {
                        $(this).removeClass('clean');
                        $(this).addClass('clean');
                    }
                }
                , 'mouseleave': function() { $(this).removeClass('hover'); }
            });

            if ($(litoggle).getChildren('div.left-sub-nav').length > 0) {
                //console.log('adding subnav event: ' + $(item));
                $(item).addEvents({
                    'click': function() {
                        var anch = this;
                        var tindex = $(anch).get('toggleindex') * 1;
                        var nextanch = $$('ul#left-nav > li.toggle > a')[tindex + 1];

                        //if (this.get('togglestate') == 'on' && !this.hasClass('clean')) { navToLink(this); }
                        //GV: to enable top level link to actually navigate vs. simply open the sub nav
                        var url = $(anch).get('link');
                        if (url != null && url.length > 0) {
                            navToLink(this);
                        }
                        else {
                            $$('ul#left-nav > li.toggle > a').each(function(item, index) {
                                if (index == tindex) { $(item).addClass('on'); } else { $(item).removeClass('on'); }
                                $(item).removeClass('clean');
                                $(item).set('togglestate', 'off');
                            });

                            this.set('togglestate', 'on');

                            if (nextanch != null) {
                                nextanch.addClass('clean');
                            }

                            openSubNav(anch, tindex);
                        }
                    } // end item click


                }); // end item add events
            } else {
                //console.log('no sub nav: ' + $(litoggle).getChildren('div.left-sub-nav').length);
                $(item).addEvents({
                    'click': function() {
                        if ($(this).get('link') != null)
                            window.location = $(this).get('link');
                    }
                });
            }
        } // end litoggle.getChildren()
    }); // end elitobble != null

    function openSubNav(navlink, tindex) {
        // close all the subnavs
        $$('ul#left-nav > li.toggle').each(function(item, index) {
            $(item).getChildren('div').each(function(divitem, index) {
                $(divitem).hide();
                $(divitem).setStyle('visibility', 'hidden');
            });
        });

        var subnav = $($(navlink).getParent()).getChildren('div')[0];

        if (subnav != null) {
            $(subnav).show();
            $(subnav).setStyle('visibility', 'visible');
        } else { navToLink(navlink); }
    }

    function navToLink(navlink) {
        var url = $(navlink).get('link');
        if (url != null && url.length > 0) { window.location = url; }
    }

    // show open subnav on at load if that page has one
    if (currentpagenav.length > 0 && $(currentpagenav) != null) {
        var anch = $$('#' + currentpagenav + ' > a')[0];
        if (anch != null) {
            var tindex = $(anch).get('toggleindex') * 1;
            var nextanch = $$('ul#left-nav > li.toggle > a')[tindex + 1];

            $(anch).addClass('on');
            $(anch).set('togglestate', 'on');
            if (nextanch != null) {
                nextanch.addClass('clean');
                nextanch.set('togglestate', 'on');
            }

            openSubNav(anch, tindex);
        }
    }
});

