
$.fn.form_validation = (function(){
  var validate = (function() {
    var c = { log: function() {} };
    // var c = console;

    var f = function() {
      $(this).each(function() {
        var t = $(this);

        if (validate_presence_of(t)) return;
        if (t.attr('name').match(/email/i))
          if (validate_email(t)) return;
        if (t.attr('data-confirm'))
          if (validate_confirmation_of(t, t.parents('form').find('[name=' + t.attr('data-confirm') + ']'))) return;
      });
    };
    f.has_errors = false;

    var validate_presence_of = function(t) {
      c.log('validate_presence_of', t);
      if ($.trim(t.val()) != "") {
        return hide_error(t);
      } else {
        return show_error(t, "Required");
      }
    };

    var email_regex = /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i;
    var validate_email = function(t) {
      c.log('validate_email', t);
      if (email_regex.test(t.val())) {
        return hide_error(t);
      } else {
        return show_error(t, "Invalid Email");
      }
    };

    var validate_confirmation_of = function(t, password_field) {
      c.log('validate_confirmation_of', t, password_field);
      if (t.val() == password_field.val()) {
        return hide_error(t);
      } else {
        return show_error(t, 'must match password');
      }
    };

    var show_error = function(t, message) {
      c.log('show error', t, message);
      var container = t.parents('li'),
        inline_message = container.find('.inline_message');

      f.has_errors = true;

      if (inline_message.length == 0) {
        inline_message = $('<span>').addClass('inline_message');
        container.append(inline_message);
      }

      container.addClass('error');
      inline_message.text(message).show();

      return true;
    };

    var hide_error = function(t) {
      c.log('hide error', t);
      var container = t.parents('li'),
        inline_message = container.find('.inline_message');
      container.removeClass('error');
      inline_message.hide();

      return false;
    };

    return f;
  })();

  var validateable_inputs = 'input[type=text],input[type=password]';

  return function() {
    return this.submit(function() {
      validate.has_errors = false;
      validate.apply($(validateable_inputs, this));

      return !validate.has_errors;
    }).find(validateable_inputs).blur(validate);
  };
})();

//on page load
$(document).ready(function()
{
  $('#nav li').hover( function(){
    $(this).children('ul').fadeTo(100, 1);
  }, function(){
    $(this).children('ul').fadeOut(100);
  });

  $('#nav li li.current').parents('li').addClass('current');

  $('#sidebar li').hover( function(){
    $(this).toggleClass('hover');
  });

  $('#sidebar li').click( function(){
    window.location = $("a", this).attr('href');
  });


  $(".modal").fancybox({
    'titleShow'     : true,
    'transitionIn'  : 'elastic',
    'transitionOut'  : 'elastic',
    'easingIn'      : 'easeOutBack',
    'easingOut'     : 'easeInBack',
       'titlePosition' : 'inside'
  });

   $(".video_modal").fancybox({
    'titleShow'     : false,
      'type': 'iframe',
      'width' : 640,
      'height': 424,
      'margin': 0,
      'padding': 0,
      'overlayOpacity': .5,
    'transitionIn': 'none',
    'transitionOut': 'none'
  });

  $(".video_modalB").fancybox({
    'titleShow'     : false,
      'type': 'iframe',
      'width' : 360,
      'height': 643,
      'margin': 0,
      'padding': 0,
      'overlayOpacity': .5,
    'transitionIn': 'none',
    'transitionOut': 'none'
  });



  $("#masthead").cycle({
        timeout:       6000,
        speed:         1000,
        pause:         1
  });

  $(".testimonials .testimonial").append('<p><a href="clients.php">Read more>></a></p>');

  $(".testimonials").cycle({
        timeout:       12000,
        speed:         1000,
        pause:         1
  });

  $("span.tooltip[title]").tooltip({
    effect: 'fade',
    opacity: .8
  });

  $(".problem_block a").append('<span>Click for Examples</span>');

  $('form').form_validation();
});

