var GlobalNavigationManager = Class.create();
GlobalNavigationManager.prototype = {
    separationIndex : 0,

    initialize : function() {
        this.separationIndex = 0;
    },

    renderNavigationByRequest : function( globalNavCallback, node, p_separationIndex ) {
        this.separationIndex = p_separationIndex;
        new Ajax.Request( globalNavCallback, {
            method : 'get',
            parameters: {
                'TS' : new Date().getTime(),
                'node' : node
            },
            onSuccess : function( transport ) {
                if ( transport != null && transport.responseText != null ) {
                    this.renderNavigation(transport.responseText.evalJSON( true ));
                }
            }.bind(this)
        });
    },


    renderNavigation : function( current_path ) {
        /**
         * Temp. hack to remove an AJAX request for the menu
         */
        var json = {
            result: {
                selected: {
                    LinkName: "",
                    SubTertLinkName: "",
                    TertLinkName: "",
                    SubLinkName: ""
                },
                structure: [
                    {
                        LinkURL: "/index.html",
                        LinkName: "Home",
                        NavigationChildren: [
                            {
                                LinkURL: "/index/whitepapers.html",
                                LinkName: "White Papers"
                            },
                            {
                                LinkURL: "/index/contact-us.html",
                                LinkName: "Contact Us"
                            },
                            {
                                LinkURL: "/index/advertise.html",
                                LinkName: "Advertise"
                            },
                            {
                                LinkURL: "/index/resources.html",
                                LinkName: "Resources"
                            }
                        ]
                    },
                    {
                        LinkURL: "/index/SAN.html",
                        LinkName: "SAN",
                        NavigationChildren: [
                            {
                            LinkURL: "/index/SAN/Fibre-Channel.html",
                            LinkName: "Fibre Channel and FCoE"
                            },
                            {
                            LinkURL: "/index/SAN/iSCSI-IP-SANs.html",
                            LinkName: "iSCSI IP SANs"
                            }
                        ]
                    },
                    {
                        LinkURL: "/index/NAS.html",
                        LinkName: "NAS"
                    },
                    {
                        LinkURL: "/index/storage-management.html",
                        LinkName: "Storage Management",
                        NavigationChildren: [
                            {
                            LinkURL: "/index/storage-management/virtualization.html",
                            LinkName: "Virtualization"
                            },
                            {
                            LinkURL: "/index/storage-management/data-de-duplication.html",
                            LinkName: "Data Deduplication"
                            }
                        ]
                    },
                    {
                        LinkURL: "/index/backup-and_recovery.html",
                        LinkName: "Backup and Recovery",
                        NavigationChildren: [
                            {
                            LinkURL: "/index/backup-and_recovery/disaster-recovery.html",
                            LinkName: "Disaster Recovery and Business Continuity"
                            },
                            {
                            LinkURL: "/index/backup-and_recovery/cloud-storage.html",
                            LinkName: "Cloud Storage"
                            },
                            {
                            LinkURL: "/index/backup-and_recovery/disk-based-backup.html",
                            LinkName: "Disk-based Backup"
                            },
                            {
                            LinkURL: "/index/backup-and_recovery/archiving.html",
                            LinkName: "Tape and Archiving"
                            }
                        ]
                    },
                    {
                        LinkURL: "/index/disk-arrays.html",
                        LinkName: "Disk Arrays",
                        NavigationChildren: [
                            {
                            LinkURL: "/index/disk-arrays/RAID.html",
                            LinkName: "RAID and Controllers"
                            }, 
                            {
                            LinkURL: "/index/disk-arrays/Disk-Drives.html",
                            LinkName: "Disk Drives and SSD Drives"
                            }
                        ]
                    },
                    // {
                    //    LinkURL: "/index/past-issues.html",
                    //    LinkName: "Archived Issues",
                    //    NavigationChildren: [
                    //        {
                    //        LinkURL: "/infostor/archives.html",
                    //        LinkName: "Newsletter Archive"
                    //        }
                    //    ]
                    //},
                    {
                        LinkURL: "/index/webcasts.html",
                        LinkName: "Webcasts"
                    },
                    {
                        LinkURL: "/index/blogs.html",
                        LinkName: "Blogs"
                    }
                ]
            }
        }
        if ( json != null ) {
        var flag = 0; 
            for (var key in json['result']['structure']) {
                var curr_obj = json['result']['structure'][key];
                if (curr_obj['LinkURL'] == current_path) {
                    json['result']['selected']['LinkName'] = curr_obj['LinkName'];
                    break;
                } else if (curr_obj['NavigationChildren']) {
                    for (var key2 in curr_obj['NavigationChildren']) {
                        var child_obj = curr_obj['NavigationChildren'][key2];
                        if (child_obj['LinkURL'] == current_path) {
                            json['result']['selected']['LinkName'] = curr_obj['LinkName'];
                            flag = 1;
							break;
                        }
                    }
                } else {
                    if(flag == 0){
					json['result']['selected']['LinkName'] = "Home";
					}
                }
            }

            NavUtilities.json = json;
            var results = json.result;
            var structure = results.structure;
            var counter = 0;
		
            // Build navigation
            structure.each( function( item ) {
                var listItem = new Element( 'li', { 'id' : StringUtils.prepareForHTMLId( item.LinkName ) } );
                var anchor = new Element( 'a', { 'id' : StringUtils.prepareForHTMLId( item.LinkName ) + 'a', 'href' : item.LinkURL, 'target' : item.NewWindow == 'true' ? '_blank' : '_top'  }).update( item.LinkName );

                // Handle end items specifically (don't want an extra separator, etc...)
                if ( structure.length <= ++counter  ) {
                    listItem.addClassName( "endcap" );
                }
                listItem.appendChild( anchor );

                // Determine which side the navitem goes into
                if ( counter <= this.separationIndex ) {
                    $( 'globalNavList' ).appendChild( listItem );
                }
                else {
                    $( 'globalNavListRight' ).appendChild( listItem );
                }

                //Build the sub navigation structure
                if ( item.NavigationChildren != null && item.NavigationChildren.length > 0  ) {
                    var subUL = new Element( 'ul', { 'id' : StringUtils.prepareForHTMLId('sub' + item.LinkName) });
                    subUL.hide();

                    var dotCounter = 0;
                    item.NavigationChildren.each( function( subItem ) {
                        var subListItem = new Element( 'li', { 'id' : StringUtils.prepareForHTMLId( 'sub' + subItem.LinkName + item.LinkName ), 'class' : 'sub-nav-item' } );
                        var subAnchor = new Element( 'a', { 'href' : subItem.LinkURL, 'target' : subItem.NewWindow == 'true' ? '_blank' : '_top' } ).update( subItem.LinkName );
                        var subDot = new Element( 'span').update( '&#8226;' );

                        subListItem.appendChild( subAnchor );
                        subUL.appendChild( subListItem );
                        if ( ++dotCounter < item.NavigationChildren.length ) {
                            subUL.appendChild( subDot )
                        }
                    });
                    $( 'globalSubNav' ).appendChild( subUL );
                }

                Event.observe( listItem, 'mouseover', function( e ) {
                    NavUtilities.resetNavItems( $$( '#globalNav ul li' ) ); //Reset All
                    NavUtilities.highlightElement( $( StringUtils.prepareForHTMLId( item.LinkName )) );  //Highlight element

                    var subNavElement = $( 'sub' + StringUtils.prepareForHTMLId( item.LinkName ) );

                    if ( subNavElement && !subNavElement.visible() ) {
                        NavUtilities.hideDefaultSubnav();
                        subNavElement.show();

                        var position = Position.positionedOffset(e.element())[0] + (e.element().getWidth()/2);
                        var wMax = $('nav').getWidth();
                        var midpoint = subNavElement.getWidth() / 2;

                        if ( wMax - midpoint < position )
                            subNavElement.style.left = (wMax - subNavElement.getWidth()) + 'px';
                        else if ( position - midpoint < 0 )
                            subNavElement.style.left = 0 + 'px';
                        else
                            subNavElement.style.left = (position - midpoint) + 'px';


                    }

                }.bind( this ));

                Event.observe( 'nav', 'mouseout', function( e ) {
                    if (!$('nav').descendants().member(e.relatedTarget)) {
                        NavUtilities.resetNavItems( $$( '#globalNav ul li' ) ); //Reset All
                        if ( results.selected ) {
                            NavUtilities.handleSelectedNavItem( results );
                        }
                        else {
                            NavUtilities.showDefaultSubnav();
                        }
                    }
                }.bind( this ));
            }.bind( this ));

            if ( $( 'globalNavListRight' ).hasChildNodes() ) $( 'globalNavListRight' ).show();
            NavUtilities.handleSelectedNavItem( results );
        }
    }
};

