﻿if ( typeof( travelport ) != "undefined" ) {

    function TableOfContents( ) {
        var _expanded = false;
                           
        _init = function() {
            var toc = $('#table-of-contents').click( _onClick );
            
            $('#table-of-contents-nav .expand').click( _onExpandAll );
            $('#table-of-contents-nav .collapse').click( _onCollapseAll );
            $('#table-of-contents > li').each( function() {             
                    $('ol',this).each( function() { 
                            $(this).hide();
                            _collapse( $(this).parent() );                            
                        } );                            
                } );
            if ( $('#content_main h1.current-page').length > 0 ) {
                _determineSelectedByCurrentPage( $('#content_main h1.current-page:eq(0)').text());
                _expandToCurrentSelected();
            }
        }

        _expandToCurrentSelected = function() {
            //Expand the Menu to the selected item, if one exists
            $('#table-of-contents li.selected').each( function() { 
                    $(this).parents('#table-of-contents > ol').show();
                    _expand( $(this).parents('#table-of-contents > ol') );
                });
             
        }

        _determineSelectedByCurrentPage = function( page ) {
            //Expand the Menu to the selected item, if one exists
                        
            $('#table-of-contents li a').each( function() { 
                    if ( $(this).html().toString().toLowerCase() == page.toString().toLowerCase() ) {
                        $(this).parent().addClass("selected");
                    }
                });
            
        }

        _onClick = function( e ) {
            this.blur();

            //alert( e.target.hash );
                        
            var node = $(e.target, this).next('ol');            
            if ( node.length > 0 ) {
                if ( _isExpanded( node.parent() ) ) {
                    node.slideUp(250);
                    _collapse( node.parent() );
                } else {
                    _expand( node.parent() );
                    node.slideDown(250);    
                }            
                //Collapase all children            
                $("ol",node).hide();
                _collapse( $("ol",node).parent() );
                            
                return false;        
            } else return true;
        }

        _onExpandAll = function( e ) {  
            this.blur();
            $('#table-of-contents > li').each( function() {
                        $('ol',this).each( function() { 
                                $(this).show();
                                _expand($(this).parent());
                        } ); 
            } );
            return false;                         
        }

        _onCollapseAll = function( e ) {  
            this.blur();

            $('#table-of-contents > li').each( function() {
                    $('ol',this).each( function() { 
                            $(this).hide();
                            _collapse($(this).parent());
                    } );                    
            } );
            return false; 
        }
       
        _isExpanded = function( target ) {        
            if ( target.hasClass("expanded") ) 
                return true;
            else return false;
        }

        _expand = function( target ) {
            target.removeClass("collapsed").addClass("expanded");
        }
        
        _collapse = function( target ) {
            
            target.addClass("collapsed").removeClass("expanded");        
        }

        _init();                
    }

    $(document).ready(  function() { travelport.web.tableOfContents = new TableOfContents(); } )

}