$(function() {
  $('#heros').cycle({
    fx:                  'uncover',
    direction:           'down',
    pager:               '#hero_buttons',
    pagerAnchorBuilder:  function(idx, slide) {
      return '<a href="#" id="hero_button_'+idx+'" class="hero_button" year="2011"></a>'
    },
    pauseOnPagerHover:    1
  });
});


$(function() {
  // Add lightbox to programme cover images
  $('.fancybox').fancybox({
    'type': 'image',
    'transitionIn': 'fade'
  });
  $('.fancybox-inline').fancybox({
    'type': 'inline',
    'transitionIn': 'fade'
  });
});

$(function() {
  $( "#tabs" ).tabs();
});

$(function() {
  // Add lightbox to programme cover images
  $('.fancybox').fancybox({
    'type': 'image',
    'transitionIn': 'fade'
  });
});

$(function() {
  // Add lightbox to programme cover images
  $('.fancybox_ajax').fancybox({
    'type': 'ajax',
    'transitionIn': 'fade'
  });
});

// Flash message fade
$(function() {
  $('#flashMessage').delay(3000).fadeTo(500, 0).slideUp();
});
$(function() {
  $('.message.warning').delay(3000).fadeTo(500, 0).slideUp();
});

// Hightlight Flash error messages
function highlight_error() {
  $('.message.error').delay(5000).show('highlight', null, null, highlight_error);
}
$(function() {
  $('.message.error').delay(500).show('highlight', null, null, highlight_error);
});

// Remove error class from form field as soon as start editing
$(function() {
  $(':input.error').keypress(function(event) {
    $(event.target).removeClass('error');
  });
});

// Hide past newsletter list & add 'Show archive' revealer
$(function() {
  $( '#past_newsletters' ).hide();
  var revealer = $( '<div id="newsletter_archive_reveal"><img src="/img/reveal_down.jpg" /><img src="/img/reveal_up.jpg" style="display: none;" />Newsletter archive</div>' );
  $( '#current_newsletter .page_body' ).css( 'padding-top', '30px' );
  $( revealer ).appendTo( '#current_newsletter' ).click( function( event ) {
    $( '#newsletter_archive_reveal img' ).toggle();
    if ( $( '#past_newsletters' ).is( ':visible' ) ) {
      $( '#past_newsletters' ).slideUp();
    } else {
      $( '#past_newsletters' ).slideDown();
    }
  } );
});

// Hide the pw reset form if on the login page & add toggle functionality
$(function() {
  if ( $('#UserLoginForm').length > 0 ) {
     $('#UserForgottenPasswordForm').hide();
     $('#login_link').hide();
     $('#forgotten_password_link').click( function() {
       $('#login_link, #UserForgottenPasswordForm, #UserLoginForm, #forgotten_password_link').stop();
       $('#forgotten_password_link').animate( { opacity: '0' }, 500, function() {
         $('#UserLoginForm').slideUp( 500, function() {
           $('#forgotten_password_link').hide();
           $('#login_link').css( 'opacity', '0' ).show();
           $('#UserForgottenPasswordForm').slideDown( 500, function() {
             $('#login_link').animate( { opacity: '1' }, 500 );
           } );
         } );
       } );
       return false;
     } );
     $('#login_link').click( function() {
       $('#login_link, #UserForgottenPasswordForm, #UserLoginForm, #forgotten_password_link').stop();
       $('#login_link').animate( { opacity: '0' }, 500, function() {
         $('#UserForgottenPasswordForm').slideUp( 500, function() {
           $('#login_link').hide();
           $('#forgotten_password_link').css( 'opacity', '0' ).show();
           $('#UserLoginForm').slideDown( 500, function() {
             $('#forgotten_password_link').animate( { opacity: '1' }, 500 );
           } );
         } );
       } );
       return false;
     } );
  }
});
