jQuery(function($) {
  // add class for Mac OS
  if (navigator.userAgent.indexOf('Mac') > 0) {
    $('body').addClass('mac-os');
  }
  
  // mainn avigation
  if ($('#navigation').length) {
    $('#navigation > ul').dropmenu({
      effect: 'none',
      nbsp: true
    });
  }

  // Tipsy
  $('#social li a img').tipsy({delayIn: 1200, delayOut: 1200, gravity: 'e'});
  
  // thumbnail slider
  if ($('#slider').length) {
    $('#slider').nivoSlider();
  }
  
  // hide default input vlaue
  $('input[type=text]').each(function() {
    $(this).data('default-value', $(this).val());
  });
  $('input[type=text]').focusin(function() {
    if ($(this).val() == $(this).data('default-value')) {
      $(this).val('');
    }
  });
  $('input[type=text]').focusout(function() {
    if ($(this).val() == '') {
      $(this).val($(this).data('default-value'));
    }
  });
  
  // lightbox gallery slider
  if ($('.gallery-slider').length) {
  $('.gallery-slider a[data-gal]').each(function() {
    $(this).attr('rel', $(this).data('gal'));
  });
  $(".gallery-slider a[rel^='prettyPhoto']").prettyPhoto({ animationSpeed: 'slow',
                                                           theme: 'dark_rounded',
                                                           slideshow: 4000,
                                                           deeplinking: false,
                                                           autoplay_slideshow: false });
  }

  // lightbox gallery footer
  $('.gallery a[data-gal]').each(function() {
    $(this).attr('rel', $(this).data('gal'));
  });
  $(".gallery a[rel^='prettyPhoto']").prettyPhoto({ animationSpeed: 'slow',
                                                    theme: 'dark_rounded',
                                                    slideshow: 4000,
                                                    deeplinking: false,
                                                    autoplay_slideshow: false });
                                                    
  // preload button hover images
  var tmp_image1 = $('<img />').attr('src', 'images/button-big-hover.png');
  var tmp_image2 = $('<img />').attr('src', 'images/button-newsletter-hover.png');
  var tmp_image3 = $('<img />').attr('src', 'images/button-small-hover.png');
  
  // load captcha question
  if ($('#captcha-img').length) {
  $.get('captcha.php?generate', function(response) {
      $('#captcha-img').html(response);
    }, 'html');
  }
  
  // init newsletter subscription AJAX handling
  if ($('#newsletterform').length > 0) {
    if ($('#newsletter-type').hasClass('mailchimp')) {
      $('#newsletterform').attr('action', 'newsletter-subscribe-mailchimp.php');
      $('#newsletterform').ajaxForm({ dataType: 'json',
                                      timeout: 2000,
                                      success: newsletterResponseMailchimp});
    } else {
      $('#newsletterform').attr('action', 'newsletter-subscribe.php');
      $('#newsletterform').ajaxForm({ dataType: 'json',
                                      timeout: 2000,
                                      success: newsletterResponse});
    }
    $('#button-newsletter').click(function() { $('#newsletterform').submit(); return false; });
    
  } // if newsletter form
  
  // init contact form validation and AJAX handling
  jQuery.validator.addMethod('name2', function(name, element) {
    name = $.trim(name); 
    return name != 'name' && name.length > 2;
  }, 'Please enter a valid name.');

  if ($('#contactform').length > 0) {
    $('#contactform').validate({ rules: { name: { required: true, name2: true},
                                          email: { required: true, email: true },
                                          message: { required: true },
                                          captcha: { required: true, remote: 'captcha.php' }},
                                 messages: { name: 'This field is required.',
                                             email: { required: 'This field is required.',
                                                      email: 'Please enter a valid email address.'},
                                             message: 'This field is required.',
                                             captcha: 'Are you sure you\'re a human? Please recheck.'},
                                 submitHandler: function(form) {  $(form).ajaxSubmit({ dataType: 'json',
                                                                                        success: contactFormResponse }); }
                              });
  } // if contact form
}); // onload

// handle contact form AJAX response
function contactFormResponse(response) {
  if (response.responseStatus == 'err') {
    if (response.responseMsg == 'ajax') {
      alert('Error - this script can only be invoked via an AJAX call.');
    } else if (response.responseMsg == 'notsent') {
      alert('We are having some mail server issues. Please refresh the page or try again later.');
    } else {
      alert('Undocumented error. Please refresh the page and try again.');
    }
  } else if (response.responseStatus == 'ok') {
    alert('Thank you for contacting us! We\'ll get back to you ASAP.');
  } else {
    alert('Undocumented error. Please refresh the page and try again.');
  }
} // contactFormResponse

// handle newsletter subscribe AJAX response
function newsletterResponse(response) {
  if (response.responseStatus == 'err') {
    if (response.responseMsg == 'ajax') {
      alert('Error - this script can only be invoked via an AJAX call.');
    } else if (response.responseMsg == 'fileopen') {
      alert('Error opening $emailsFile. Please refer to documentation for help.');
    } else if (response.responseMsg == 'name') {
      alert('Please enter a valid name.');
    } else if (response.responseMsg == 'email') {
      alert('Please enter a valid email address.');
    } else if (response.responseMsg == 'duplicate') {
      alert('You are already subscribed to our newsletter.');
    } else if (response.responseMsg == 'filewrite') {
      alert('Error writing to $emailsFile. Please refer to documentation for help.');
    } else {
      alert('Undocumented error. Please refresh the page and try again.');
    }
  } else if (response.responseStatus == 'ok') {
    alert('Thank you for subscribing to our newsletter! We will not abuse your address.');
  } else {
    alert('Undocumented error. Please refresh the page and try again.');
  }
} // newsletterResponse

// handle newsletter subscribe AJAX response - Mailchimp ver
function newsletterResponseMailchimp(response) {
  if (response.responseStatus == 'err') {
    if (response.responseMsg == 'ajax') {
      alert('Error - this script can only be invoked via an AJAX call.');
    } else if (response.responseMsg == 'name') {
      alert('Please enter a valid name.');
    } else if (response.responseMsg == 'email') {
      alert('Please enter a valid email address.');
    } else if (response.responseMsg == 'listid') {
      alert('Invalid MailChimp list name');
    } else if (response.responseMsg == 'duplicate') {
      alert('You are already subscribed to our newsletter.');
    } else {
      alert('Undocumented error (' + response.responseMsg + '). Please refresh the page and try again.');
    }
  } else if (response.responseStatus == 'ok') {
    alert('Thank you for subscribing! Please confirm your subscription in the email you\'ll receive shortly.');
  } else {
    alert('Undocumented error. Please refresh the page and try again.');
  }
} // newsletterResponse
