jQuery(document).ready(function($) {
  // Primary instant switch active class
  $('header nav li a').click(function() {
    $('header nav li').removeClass('active');
    $(this).parent().addClass('active');
  });

  // Secondary instant switch active class
  $('.secondary nav li a').click(function() {
    $('.secondary nav li').removeClass('active');
    $(this).parent().addClass('active');
  });

  // Add a print this page link
  $('.secondary nav:first ul, footer ul').append('<li class="print"><a href="#print">Print this page</a></li>');
  $('.secondary nav ul li.print a, footer ul li.print a').click(function() {
    window.print();
    return false;
  });

  // Elastic
  $('textarea').elastic();

  // Rotate latest news on the homepage
  $('section#blog article').hide();
  $('section#blog article:first').show();

  toggleBlogPost();
  function toggleBlogPost() {
    $('section#blog article:visible').delay(4000).fadeOut(function() {
      var next = $(this).next();

      if ( next.length ) {
        next.fadeIn(function() {
          toggleBlogPost();
        });
      } else { // if we are at the last post (no next exists) go back to the start
        $('section#blog article:first').fadeIn(function() {
          toggleBlogPost();
        });
      }
    });
  }

  /*!
   * jQuery Konami code trigger v. 0.1
   *
   * Copyright (c) 2009 Joe Mastey
   * Dual licensed under the MIT and GPL licenses:
   *   http://www.opensource.org/licenses/mit-license.php
   *   http://www.gnu.org/licenses/gpl.html
   */
  (function($){
    $.fn.konami         = function( fn, params ) {
      params            = $.extend( {}, $.fn.konami.params, params );
      this.each(function(){
          var tgt       = $(this);
          tgt.bind( 'konami', fn )
             .bind( 'keyup', function(event) { $.fn.konami.checkCode( event, params, tgt ); } );
      });
      return this;
    };

    $.fn.konami.params  = {
      'code'      : [38, 38, 40, 40, 37, 39, 37, 39, 66, 65],
      'step'      : 0
    };

    $.fn.konami.checkCode = function( event, params, tgt ) {
      if(event.keyCode == params.code[params.step]) {
        params.step++;
      } else {
        params.step     = 0;
      }

      if(params.step == params.code.length) {
        tgt.trigger('konami');
        params.step     = 0;
      }
    };
  })(jQuery);

  $(document).konami(function(){
    // Add mo's to team pictures
    $('ul#staff li img').each(function() {
      if ($(this).attr('src').search('_mo') == -1) {
        var new_src = $(this).attr('src').replace(".jpg", '_mo.jpg');
        $(this).attr('src', new_src);
      }
    });
  });
});

// Show or hide the checkbox if the email is valid/un-valid
$(document).ready(function() {
  $("li.boolean").hide();
  var showHideNewsletterCallback = function(){
    var email = $("#enquiry_email_or_phone").val();
    if(email != 0)
    {
      if(isValidEmailAddress(email))
      {
        $("li.boolean").show();
        $('#enquiry_subscribe').attr('checked', true);
      } else {
        $("li.boolean").hide();
        $('#enquiry_subscribe').attr('checked', false);
      }
    } else {
      $("li.boolean").hide();
      $('#enquiry_subscribe').attr('checked', false);
    }
  };
  $("#enquiry_email_or_phone")
    .change(showHideNewsletterCallback) // for auto-complete + selenium
    .keyup(showHideNewsletterCallback); // for live typing
  showHideNewsletterCallback();
});

function isValidEmailAddress(emailAddress) {
  var pattern = new RegExp(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/i);
  return pattern.test(emailAddress);
}
;

