/*
    This class handles the display of the tertiary (or _local_)
    navigation based upon the tree structure in the Navigation
    Utilities
*/
var TertiaryNavigationManager = Class.create();
TertiaryNavigationManager.prototype = {
    initialize : function() { },

    /*
        This is the actual display builder.  It was initially
        designed for the tertiary nav, but DM has stated that
        this componet should have been called "local nav".

        It's designed to be versatile, in that it is tier
        agnostic.  Its sole function is to display the data
        which is provided in array format (and it must be the
        same pattern used in the global nav tree)
     */
    buildDisplay : function( data, style, destination, linkName, subItemNameToMatch ) {
        var navItemCounter = 0;
        if ( style == 'vertical' ) {
            var navListA = new Element( 'UL', { 'id' : 'ta' + linkName, 'class' : 'tertiary split' } );
            var navListB = new Element( 'UL', { 'id' : 'tb' + linkName, 'class' : 'tertiary split right' } );
            data.each( function( navItem ) {
                var tertiaryHtmlItem = new Element( 'LI', { 'class' : 'tert-nav-item' } );
                var tertiaryAnchorItem = new Element( 'A', { 'href' : navItem.LinkURL, 'target' : navItem.NewWindow == 'true' ? '_blank' : '_top' } ).update( navItem.LinkName );

                if ( navItem.LinkName == subItemNameToMatch ) {
                    tertiaryHtmlItem.addClassName( 'selected' );
                }

                tertiaryHtmlItem.appendChild( tertiaryAnchorItem );
                (( ++navItemCounter < (data.length/2)+1 ) ? navListA : navListB).appendChild( tertiaryHtmlItem );

            }.bind( this ));
            destination.appendChild( navListA );
            destination.appendChild( navListB );
        }
        else {
            var navList = new Element( 'UL', { 'id' : 't' + linkName, 'class' : 'tertiary full' } );
            data.each( function( navItem ) {
                var tertiaryHtmlItem = new Element( 'LI', { 'class' : 'tert-nav-item' } );
                var tertiaryAnchorItem = new Element( 'A', { 'href' : navItem.LinkURL, 'target' : navItem.NewWindow == 'true' ? '_blank' : '_top' } ).update( navItem.LinkName );

                if ( navItem.LinkName == subItemNameToMatch ) {
                    tertiaryHtmlItem.addClassName( 'selected' );
                }

                tertiaryHtmlItem.appendChild( tertiaryAnchorItem );
                navList.appendChild( tertiaryHtmlItem );

                if ( ++navItemCounter < data.length ) {  // Append Separator
                    navList.appendChild( new Element( 'li' ).update( '|') );
                }
            });
            destination.appendChild( navList );
        }
        destination.appendChild( new Element( 'div', { 'style' : 'clear:both;' } )).update( ' ' );
    },

    /*
        Wrapper function to build the display. Digital Media asked
        to that the option be provided for them to choose whether
        to display the siblings of the selected node or the children.
        This function simply gathers the correct set of data based
        upon the users choice and passes it into the grunt display
        method above.
     */
    buildNavigation : function( data, style, destination, uniqueIdentifier, useChildren ) {
        if ( data.selected ) {
            var linkName = data.selected.LinkName;
            var subLinkName = data.selected.SubLinkName;
            var tertLinkName = data.selected.TertLinkName;
            var subTertLinkName = data.selected.SubTertLinkName;
            if ( StringUtils.isNotBlank(linkName) ) {
                var siblingTree = this.findPath( data, linkName );
                var childTree = null;
                var baseItemForDisplay = linkName;
                var subItemToSearchFor = linkName;

                if ( StringUtils.isNotBlank(subLinkName) && StringUtils.isBlank(tertLinkName)) {
                    childTree = this.findPath( siblingTree, subLinkName );
                    baseItemForDisplay = subLinkName;
                    subItemToSearchFor = tertLinkName;
                }
                else if ( StringUtils.isNotBlank(subLinkName) && StringUtils.isNotBlank(tertLinkName)) {
                    siblingTree = this.findPath( siblingTree, subLinkName );
                    childTree = this.findPath( siblingTree, tertLinkName );
                    baseItemForDisplay = tertLinkName;
                    subItemToSearchFor = subTertLinkName;
                }
                else {
                    childTree = siblingTree;
                    siblingTree = {
                        "NavigationChildren" : data.structure,
                        "LinkName" : "ROOT",
                        "LinkURL" : "/"
                    };
                }

                if ( useChildren ) {
                    this.buildDisplay( childTree.NavigationChildren, style, destination, baseItemForDisplay + uniqueIdentifier, subItemToSearchFor );
                }
                else { //if dataLocation is sibling or was not defined
                    this.buildDisplay( siblingTree.NavigationChildren, style, destination, baseItemForDisplay + uniqueIdentifier, baseItemForDisplay );
                }
            }
        }

    },

    findPath : function( data, item ) {
        var result;
        if ( data.LinkName == item ) {
            result = data;
        }
        else {
            var subData = (data.NavigationChildren) ? data.NavigationChildren : (data.structure)? data.structure : null;
            if ( data.NavigationChildren || data.structure ) {
                subData.each( function( subItem ) {
                    if ( subItem.LinkName == item ) {
                        result = subItem;
                        throw $break;
                    }
                }.bind(this));
            }
        }
        return result;
    }
}