// goroharumi.com

$(document).ready(function(){
  
  
  // // XHTML-complaint target="_blank" substitute for external links
  // $("a[href^='http://']").not("a[href^='http://gorosaur.us']").click(function(event){
  //   window.open( $(this).attr('href') );
  //   event.preventDefault();
  // });
  
  
  // Prefill form elements
  function prefill(selector, defValue) {
     $(selector).each(function() {
       if($.trim(this.value) == "" || $.trim(this.value) == defValue) {
         this.value = defValue;
         $(this).addClass('prefill');
       }
     });

     $(selector).focus(function() {
       if(this.value == defValue) {
         this.value = "";
         $(this).removeClass('prefill');
       }
     });

     $(selector).blur(function() {
       if($.trim(this.value) == "") {
         this.value = defValue;
         $(this).addClass('prefill');
       }
     });
  }
  
  // Form default values and hide default label
  $('label').hide();
  prefill('input#contact_name', 'Name');
  prefill('input#contact_email', 'Email Address');
  prefill('textarea#contact_message', 'Message');
  
  // Reset values on submit
  $('div#footer_inner form').submit(function(){
    if ($('input#contact_name').val() == 'Name') {
      $('input#contact_name').val('');
    }
    if ($('input#contact_email').val() == 'Email Address') {
      $('input#contact_email').val('');
    }
    if ($('textarea#contact_message').val() == 'Message') {
      $('textarea#contact_message').val('');
    }
  });
  
  
  // Scroll to hash
  $('a[href*=#]').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var $target = $(this.hash);
      $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
      if ($target.length) {
        var targetOffset = $target.offset().top;
        $('html,body').animate({scrollTop: targetOffset}, 1000);
       return false;
      }
    }
  });
  
  
  // Contact form stuff
  $('input.submit').click(function() {
    
    // Reset each click
    $('.errors').hide();
    
    var contact_errors = [];
    
    // Check if name is blank
    var name = $('input#contact_name').val();
    if ($.trim(name) == '' || $.trim(name) == 'Name') {
      $('div#error').append('<p class="errors" id="error_name_blank">Name can\'t be blank.</p>');
      $('input#contact_name').focus();
      return false;
    }
    
    var tooLong = 64;
    
    // Check if name is too long
    if (name.length >= tooLong) {
      $('div#error').append('<p class="errors" id="error_name_blank">Name is too long. (64 character maximum)</p>');
      $('input#contact_name').focus();
      return false;
    }
    
    // Check if email is blank
    var email = $('input#contact_email').val();
    if ($.trim(email) == '' || $.trim(email) == 'Email Address') {
      $('div#error').append('<p class="errors" id="error_email_blank">Email can\'t be blank.</p>');
      $('input#contact_email').focus();
      return false;
    }
    
    // Check if email is too long
    if (email.length >= tooLong) {
      $('div#error').append('<p class="errors" id="error_email_blank">Email is too long. (64 character maximum)</p>');
      $('input#contact_email').focus();
      return false;
    }
    
    var goodEnough = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    
    // Check if email is valid
    if (!goodEnough.test($.trim(email))) {
      $('div#error').append('<p class="errors" id="error_email_blank">Email appears to be invalid.</p>');
      $('input#contact_email').focus();
      return false;
    }
    
    // Check if message is blank
    var message = $('textarea#contact_message').val();
    if ($.trim(message) == '' || $.trim(message) == 'Message') {
      $('div#error').append('<p class="errors" id="error_message_blank">Message can\'t be blank.</p>');
      $('textarea#contact_message').focus();
      return false;
    }
    
    var token = $('input#contact_token').val();
    
    // If valid, submit form
    var dataString = 'inquiry[name]='+ name + '&inquiry[email]=' + email + '&inquiry[message]=' + message + '&inquiry[auth_token]=' + token + '&say_hello=true';
    $('input.submit').hide();
    $('img#ajax_loader').show();
    

    $.ajax({
      type: "POST",
      data: dataString,
      url: 'index.php',
      success: function() {
        $('img#ajax_loader').hide();
        $('div#footer_inner form input').hide();
        $('div#footer_inner form textarea').hide();
        $('div#footer_inner form').append('<p id="success" class="hidden"><strong>Your message has been sent successfully.</strong></p>');
        $('p#success').fadeIn(1500);
      },
      error: function() {
        $('img#ajax_loader').hide();
        $('div#footer_inner form input').hide();
        $('div#footer_inner form textarea').hide();
        $('div#footer_inner form').append('<p id="success" class="hidden"><strong>There was an error processing your message.</strong></p>');
        $('p#success').fadeIn(1500);
      }
    });
    
    return false;
  });
  
  
  // Code sample animation
  $('li#code_home a').click(function(event) {
    moveCodeSample('0');
    event.preventDefault();
  });

  $('li#design_link_5 a').click(function(event) {
    moveCodeSample('-680px');
    event.preventDefault();
  });
  
  $('li#design_link_6 a').click(function(event) {
    moveCodeSample('-1360px');
    event.preventDefault();
  });
  
  $('li#design_link_7 a').click(function(event) {
    moveCodeSample('-2040px');
    event.preventDefault();
  });
  
  $('li#design_link_8 a').click(function(event) {
    moveCodeSample('-2720px');
    event.preventDefault();
  });
  
  $('li#design_link_9 a').click(function(event) {
    moveCodeSample('-3400px');
    event.preventDefault();
  });
  
  $('li#design_link_10 a').click(function(event) {
    moveCodeSample('-4080px');
    event.preventDefault();
  });
  
  function moveCodeSample(thisFar) {
    $('div#code ul#code_descriptions').animate(
      { left: thisFar }, 1500
    );
  }
  
});
