$(document).ready(function() {
     // Hover states for collapsible menu buttons
     $('#login_button').hover(
          function() {
               $(this).attr({ src: '/images/collapsible_menu/member_login_hover.png' });
          },
          function() {
               $(this).attr({ src: '/images/collapsible_menu/member_login.png' });
          }
     );
     $('#logout_button').hover(
          function() {
               $(this).attr({ src: '/images/collapsible_menu/logout_hover.png' });
          },
          function() {
               $(this).attr({ src: '/images/collapsible_menu/logout.png' });
          }
     );
     $('#account_button').hover(
          function() {
               $(this).attr({ src: '/images/collapsible_menu/my_account_hover.png' });
          },
          function() {
               $(this).attr({ src: '/images/collapsible_menu/my_account.png' });
          }
     );
     $('#sitemap_button_single').hover(
          function() {
               $(this).attr({ src: '/images/collapsible_menu/sitemap_single_hover.png' });
          },
          function() {
               $(this).attr({ src: '/images/collapsible_menu/sitemap_single.png' });
          }
     );
     
     $('#sitemap_button').hover(
          function() {
               $(this).attr({ src: '/images/collapsible_menu/sitemap_hover.png' });
          },
          function() {
               $(this).attr({ src: '/images/collapsible_menu/sitemap.png' });
          }
     );

     // Remove the border from the farthest right-most ul in the sitemap
     $('#sitemap ul#about_map').css({ paddingLeft: 0 });

     // Ensure the collapsible menu and all of its child elements are hidden initially
     $('#collapsible_menu').hide();
     $('#form#login').hide();
     $('#sitemap').hide();
     $('#my_account_tools').hide();

     // Show/hide the collapsible menu (sitemap & member login)
     $('#login_button').click(function() {
          if ( $('#collapsible_menu').is(':visible') ) {
               if( $('#sitemap').is(':visible') ) {
                    $('#sitemap').fadeOut( 1, function() {
                         $('#collapsible_menu').slideUp(300, function() {
                              $('#collapsible_menu').slideDown(300, function() {
                                   $('form#login').show();
                              });
                         });
                    });
               } else {
                    $('#collapsible_menu form#login').fadeOut(1, function() {
                         $('#collapsible_menu').slideUp(300);
                    });
               }
          } else {
               $('#sitemap').fadeOut( 1, function() {
                    $('#collapsible_menu').slideDown(300, function() {
                         $('#collapsible_menu form#login').show();
                    });
               });
          }
     });

     // Show/hide the collapsible menu (sitemap & member login)
     $('#account_button').click(function() {
          if ( $('#collapsible_menu').is(':visible') ) {
               if( $('#sitemap').is(':visible') ) {
                    $('#sitemap').fadeOut( 1, function() {
                         $('#collapsible_menu').slideUp(300, function() {
                              $('#collapsible_menu').slideDown(300, function() {
                                   $('#my_account_tools').show();
                              });
                         });
                    });
               } else {
                    $('#collapsible_menu #my_account_tools').fadeOut(1, function() {
                         $('#collapsible_menu').slideUp(300);
                    });
               }
          } else {
               $('#sitemap').fadeOut( 1, function() {
                    $('#my_account_tools').fadeOut( 1, function() {
                         $('#collapsible_menu').slideDown(300, function() {
                              $('#collapsible_menu #my_account_tools').show();
                         });
                    });
               });
          }
     });

     $('#sitemap_button, #sitemap_button_single').click(function() {
          if ( $('#collapsible_menu').is(':visible') ) {
               if ( $('form#login').is(':visible') ) {
                    $('form#login').fadeOut(1, function() {
                         $('#collapsible_menu').slideUp(300, function() {
                              $('#collapsible_menu').slideDown(300, function() {
                                   $('#sitemap').show( 1, function() {
                                        // Set the height of the sitemap DIV element explicitly
                                        var about_map_height = $('div#collapsible_menu div#sitemap div#about_map').height();
                                        var sitemap_heading_height = $('div#collapsible_menu div#sitemap h2').height();
                                        var sitemap_height = eval( about_map_height + sitemap_heading_height + 35 );
                                        $('div#collapsible_menu div#sitemap').height( sitemap_height );
                                   });
                              });
                         });
                    });
               } else if ( $('#my_account_tools').is(':visible') ) {
                    $('#my_account_tools').fadeOut(1, function() {
                         $('#collapsible_menu').slideUp(300, function() {
                              $('#collapsible_menu').slideDown(300, function() {
                                   $('#sitemap').show(1, function() {
                                        // Set the height of the sitemap DIV element explicitly
                                        var about_map_height = $('div#collapsible_menu div#sitemap div#about_map').height();
                                        var sitemap_heading_height = $('div#collapsible_menu div#sitemap h2').height();
                                        var sitemap_height = eval( about_map_height + sitemap_heading_height + 35 );
                                        $('div#collapsible_menu div#sitemap').height( sitemap_height );
                                   });
                              });
                         });
                    });
               } else {
                    $('#collapsible_menu div#sitemap').fadeOut(1, function() {
                         $('#collapsible_menu').slideUp(300);
                    });
               }
          } else {
               $('#collapsible_menu').slideDown(300, function() {
                    $('#collapsible_menu div#sitemap').show( 1, function() {
                         // Set the height of the sitemap DIV element explicitly
                         var about_map_height = $('div#collapsible_menu div#sitemap div#about_map').height();
                         var sitemap_heading_height = $('div#collapsible_menu div#sitemap h2').height();
                         var sitemap_height = eval( about_map_height + sitemap_heading_height + 35 );
                         $('div#collapsible_menu div#sitemap').height( sitemap_height );
                    });
               });
          }
     });
});
