function fixMenuHeight()
{
  $('div#menu').height(
    $('div#middle-inner-2').height() - 10
  );
}

$(document).ready(function() {

  // Handle external links.

  $('a.external').click(function() {
    window.open($(this).attr('href'));
    return false;
  });

  // Handle styled selector.

  $('div.styled-selector select').sb({
    fixedWidth: true
  });

  // Handle the login fields.

  var usernameFieldText = 'Username';
  var passwordFieldText = 'Password';

  $('div#account-login input#login-username-field').val(usernameFieldText);
  $('div#account-login input#login-password-field').val(passwordFieldText);

  $('div#account-login input#login-username-field').focus(function() {
    if ($(this).val() == usernameFieldText) {
      $(this).val('');
    }
  });

  $('div#account-login input#login-username-field').blur(function() {
    if ($(this).val() == '') {
      $(this).val(usernameFieldText);
    }
  });

  $('div#account-login input#login-password-field').focus(function() {
    if ($(this).val() == passwordFieldText) {
      $(this).val('');
    }
  });

  $('div#account-login input#login-password-field').blur(function() {
    if ($(this).val() == '') {
      $(this).val(passwordFieldText);
    }
  });

  // Login form validation.

  $('div#account-login form').submit(function() {

    var validForm = $('div#account-login input#login-username-field').val() != ''
                    && $('div#account-login input#login-username-field').val() != usernameFieldText
                    && $('div#account-login input#login-password-field').val() != ''
                    && $('div#account-login input#login-password-field').val() != passwordFieldText;

    if (!validForm) {
      alert('Please specify your username and passsword to log in.');
      return false;
    }

  });

  // Set menu height and rollovers (if appropriate).

  if ($('div#menu').is(':visible')) {

    fixMenuHeight();

    $('div#menu li:not(.selected) a').hover(function() {
      $(this).css('background', 'url(/img/menu-hovered-item-background.png)');
    }, function() {
      $(this).css('background', 'url(/img/menu-item-background.png)');
    });

  }

  // Search form validation.

  $('div#site-search form').submit(function() {
    if ($('div#site-search input#search-field').val() == '') {
      alert('Please specify a search criteria.');
      return false;
    }
  });

  // Toggle Hover for login button
  $('#login-button').mouseenter(function() {
    $(this).attr('src', '/img/header-login-button-hover.gif');
  })
  $('#login-button').mouseleave(function() {
    $(this).attr('src', '/img/header-login-button.png');
  })

  // Toggle Hover for search button
  $('#site-search-button').mouseenter(function() {
    $(this).attr('src', '/img/search-button-hover.gif');
  })
  $('#site-search-button').mouseleave(function() {
    $(this).attr('src', '/img/search-button.png');
  })

  // Toggle Hover for Gallery Buttons
  $('#move-left').mouseenter(function() {
    $(this).attr('src', '/img/gallery-move-left-button-hover.gif');
  })
  $('#move-left').mouseleave(function() {
    $(this).attr('src', '/img/gallery-move-left-button.png');
  })
  
  // Toggle Hover for Gallery Buttons
  $('#continue-button').mouseenter(function() {
    $(this).attr('src', '/img/continue-button-hover.gif');
  })
  $('#continue-button').mouseleave(function() {
    $(this).attr('src', '/img/continue-button.gif');
  })
  
  // Toggle Hover for Gallery Buttons
  $('form#forgotten-password #submit-button').mouseenter(function() {
    $(this).attr('src', '/img/send-details-button-hover.gif');
  })
  $('form#forgotten-password #submit-button').mouseleave(function() {
    $(this).attr('src', '/img/send-details-button.gif');
  })


  $('#move-right').mouseenter(function() {
    $(this).attr('src', '/img/gallery-move-right-button-hover.gif');
  })
  $('#move-right').mouseleave(function() {
    $(this).attr('src', '/img/gallery-move-right-button.png');
  })

  // Toggle Hover for arrow button on select lists
  $('.selectbox .display .arrow_btn').mouseenter(function() {
    $(this).css('background-image', 'url(/img/styled-selector-button-hover.gif)');
    $(this).css('cursor', 'pointer');
  })
  $('.selectbox .display .arrow_btn').mouseleave(function() {
    $(this).css('background-image', 'url(/img/styled-selector-button.png)');
  })

});
