$(document).ready(function(){
  
  if ($.browser.webkit == true || $.browser.opera == true) {
    $('#menu').css('bottom', '-1px');
  }
  
  $("#writing-nav li").hover(function(){
    $(this).css('text-decoration', 'underline');
  }, function(){
    $(this).css('text-decoration', 'none');
  }).click(function(){
    var offset = 10;
    var item = parseInt($(this).attr('data-id'));

    // Switch content section.
    $('#writing-content .section:visible').fadeOut('fast', function(){
      $('#writing-content-'+item).fadeIn('fast');
    });
    
    // Update active state.
    $('#writing-nav li').removeClass('active');
    $(this).addClass('active');

    if (item > 0) {
      offset = 10+(26*item);
    }
    $('#writing-nav-wrapper').css('background-position', '100% '+offset+'px');
  });
  $('#mailform').submit(function(){
    
    // Clear any current errors
    $('div.form-error').remove();

    // Make sure all fields are filled out.
    var error = false;
    if ($('#from').val().length == 0) {
      bshoot.form_error($('#from'), 'You must enter your e-mail address.');
      error = true;
    }
    else if(bshoot.isValidEmailAddress($('#from').val()) == false) {
      bshoot.form_error($('#from'), 'The e-mail address you entered is not valid.');
      error = true;
    }
    else {
      bshoot.form_error_remove($('#from'));
    }

    if ($('#subject').val().length == 0) {
      bshoot.form_error($('#subject'), 'You must enter a subject.');
      error = true;
    }
    else {
      bshoot.form_error_remove($('#subject'));
    }

    if ($('#msg').val().length == 0) {
      bshoot.form_error($('#msg'), 'You must enter a message.');
      error = true;
    }
    else {
      bshoot.form_error_remove($('#msg'));
    }
    
    if (error) {
      return false;
    }
    
    $('#submit').attr('disabled', 'disabled').after(' Sending message…');
  });
  
  $(".carousel").jCarouselLite({
    auto:8000,
    circular:true,
    visible:2
  });
  $('.section.hidden').hide();
  
});


var bshoot = {};
bshoot.form_error = function(elem, msg) {
  $(elem).addClass('error');
  $(elem).after('<div class="form-error">'+msg+'</div>');
}
bshoot.form_error_remove = function(elem, msg) {
  $(elem).removeClass('error');
}
bshoot.isValidEmailAddress = function(emailAddress) {
  var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
  return pattern.test(emailAddress);
}


