(function ($) {


  $(document).ready(function () {

    $('#contact-toggle').hover(function () {
      $(this).addClass('hover', 250);
    }, function () {
      $(this).removeClass('hover');
    });

    $('#email_address, #name, #comments').focus(function () {
      var pattern = /(your email|your name|your comments)/i,
          value   = $(this).val();

      if (pattern.test(value)) {
        $(this).val('');
      }
    });

    /*
     * Contact page toggler
     */
    $('body').delegate('#contact-toggle', 'click', function (event) {
      event.preventDefault();

      var winHeight = $(window).height(),
          targetHeight = winHeight * 0.9;

      if ($('#header').hasClass('home-closed')) {
        $('#header').animate({'height': targetHeight + 'px'}, 1250);

        $('#contact .main').fadeOut(1250, function () {
          $('.footer .bg').fadeIn('fast');
          $('#header').removeClass('home-closed');
        });
      } else {
        $('#contact .main').show();
        $('.footer .bg').fadeOut('fast');

        $('#header').animate({'height': '175px'}, 1250, function () {
          $('#header').addClass('home-closed');
        });
      }
    });
    
    /*
     * Contact form quick validation
     */
    $('body').delegate('#submit', 'click', function (event) {
      event.preventDefault();
      var errors = [],
          noerrors = [],
          $email = $('#email_address'),
          $name  = $('#name'),
          $comments = $('#comments'),
          emailregex = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;

      if ($email.val().length === 0 
          || /your email/i.test($email.val())
          || !emailregex.test($email.val())) {
        errors.push($email);
      } else {
        noerrors.push($email);
      }

      if ($name.val().length === 0 || /your name/i.test($name.val())) {
        errors.push($name);
      } else {
        noerrors.push($name);
      }

      if ($comments.val().length === 0 
          || /your comments/i.test($comments.val())) {
        errors.push($comments);
      } else {
        noerrors.push($comments);
      }

      if (errors.length === 0) {
        $('#contact form').submit();
        return;
      }

      $.each(errors, function () {
        this.closest('div').find('.required_msg').show();
      });

      $.each(noerrors, function () {
        this.closest('div').find('.required_msg').hide();
      });
    });

  });

  function setHeaderHeight() {
    var winHeight = $(window).height(),
        targetHeight = winHeight * 0.9;
    $('#header:not(.home-closed)').height(targetHeight);
  }

  $(window).load(setHeaderHeight).resize(setHeaderHeight);

})(jQuery);

