$(function(){
  
  var bannerObj = $('#banner-images'), banners = $('img', bannerObj), animTime = 1000, waitTime = 3000, prevBannerId = 0, currBannerId = 1;
  
  function switchBanner() {
    $(banners).eq(prevBannerId).removeClass('top');
    $(banners).eq(currBannerId).addClass('top').fadeOut(1).delay(waitTime).fadeIn(animTime, function(){
      $(banners).eq(prevBannerId).hide();
      prevBannerId = currBannerId;
      if(currBannerId+1 >= banners.length) {
        currBannerId = 0;
      } else {
        currBannerId++;
      }
      switchBanner();
    });
  }
  
  $(banners).hide();
  $(banners).eq(prevBannerId).show().addClass('top');
  
  switchBanner();
  
  $('input[type="text"],input[type="email"],textarea').defaultText();
  
  var galBanLinks = $('#gallery-banner-link span');
  $('#feature-buttons a:not(.active)').hover(function(){
    var index = $(this).parent().index();
    $(galBanLinks).eq(index).stop(true, true).fadeIn(200);
  },function(){
    var index = $(this).parent().index();
    $(galBanLinks).eq(index).stop(true, true).fadeOut(200);
  });
  
  var content = $('#content');
  
  $('a.gallery-image, .project-images a, .teammember a.image', content).fancybox();
  
  $('#nav li').hover(function(){
    $(this).addClass('hover');
  },function(){
    $(this).removeClass('hover');
  });
  
});

jQuery.fn.defaultText = function() {
  var self = this;
  $('form').submit(function() {
    $(self).each(function() {
      var t = $(this);
      if (t.val() == t.attr('title') && t.hasClass('default')) {
        t.val('');
      }
    });
  });
  return $(this).blur(function() {
    var t = $(this);
    if (t.val() == '') {
      t.val(t.attr('title'));
      t.addClass('default');
    }
  }).focus(function() {
    var t = $(this);
    if (t.val() == t.attr('title') && t.hasClass('default')) {
      t.val('');
      t.removeClass('default');
    }
  }).blur();
};
