jQuery(document).ready(function($) {
  // External Links
  $("a[rel=external]").attr('target', '_blank');

  // Overlay
  theOv = $("a[rel=overlay]").each(function(i) {
    //var thetarget = $(this).attr("href")
    $(this).overlay({
      target : $(this).attr("href"),
      left: 'center',
      top: 'center',
      expose: {
        color: '#111',
        loadSpeed: 200,
        opacity: 0.8
      },
      closeOnClick: true,
    api: true
    });
  });

  // Set input values from its label
  $.fn.valFromLabel = function(){
    var inputs = $('.input', this);
    inputs.each(function() {
      var l = $(this).siblings('label');
      l.hide();
      var v = l.html();
      $(this).val(v);
    });
  }

  // Swap input values on focus
  $.fn.swapVals = function() {
    var values = [];
    var inputs = $('.input', this);
    inputs.each(function(i){
      values[i] = $(this).val();
      $(this)
        .focus(function(){
          if ($(this).val() == values[i]) {
            $(this).val("");
          }
        })
        .blur(function(){
          var l = $(this).siblings('label');
          l.hide();
          var v = l.html();
          var theVal = $.trim($(this).val())
          if ( theVal == "" || theVal == v ) {
            $(this).val(values[i]);
            $(this).removeClass('valid');
            $(this).addClass('error');
          } else {
            $(this).removeClass('error');
            $(this).addClass('valid');
          }
        });
    });
  }

  $.fn.checkVal = function(){
    var iForm = $(this);
    var result = null;
    var reqF = $('.req', iForm);
    reqF.each(function() {
      $(this).blur(function(){
        if ($.trim($(this).val()) == "") {
          $(this).addClass('error');
          $(this).removeClass('valid');
        } else {
          $(this).removeClass('error');
          $(this).addClass('valid');
        }
      });
    });
    return result;
  }

  // Validate form
  function validate(formData, jqForm, options){
    send = null;
    var theForm = jqForm[0];
    var submit = $('.submit', theForm);
    var reqF = $('.req', theForm);
    //if (theForm.not('.swap')) {checkVal(theForm);}
    reqF.each(function() {
      if ( $(this).hasClass('valid') ) {
        send = true;
        return true;
      }
      else {
        $(this).addClass('error');
        alert('Please fill in all required fields!');
        send = false;
        return false;
      }
    });
    if ( !send ) { return false; }
    else { submit.attr('disabled', 'disabled'); alert('Thank You! We\'ll get back too as soon as possible'); }
  }

  $.fn.checkReq = function(){
    var theForm = $(this);
    var reqF = $('.req', theForm);

    reqF.each(function() {
      if ( $(this).hasClass('valid') ) {
        send = true;
        return true;
      }
      else {
        $(this).addClass('error');
        alert('Please fill in all required fields!');
        send = false;
        return false;
      }
    });
    if ( !send ) { return false; }
  }


  scForm = $('#sidecontact');
  scForm.valFromLabel();
  scForm.swapVals();

  $('#vForm').checkVal();
  $('#cForm').checkVal();

  $('#thebook form').checkVal();
  $('#thebook form').submit(function(){
    return $(this).checkReq();
    theOv.close();
  });

  cForm = $('form.theForm');
  cForm.ajaxForm({
      beforeSubmit: validate
  });


  $('div.scrollable-nav ul.items').tabs( 'div.scrollable div.item', {
    effect: 'fade',
    fadeInSpeed: 2000,
    fadeOutSpeed: 1500,
    rotate: true,
    onClick: function( event, tabIndex ) {}
  }).slideshow({
    autoplay: true,
    interval: 5000
  });

  $('div.slideshow ul.slides-nav').tabs( 'div.slideshow div.slide', {
    effect: 'fade',
    //event: 'mouseover',
    fadeInSpeed: 800,
    fadeOutSpeed: 800,
    rotate: true
  }).slideshow({
    autoplay: true,
    interval: 7000
  });

  $('ul.slides-nav a').click(function() {
    return false;
  });
});