$(function() {
  steve.email.parse();
  
  if (steve.contact.form.isSlidable()) {
    steve.contact.form.setSlidable();
  } else {
    steve.contact.form.validate();
    steve.contact.info.style();
  }
  
  steve.cv.setPrintable();
  steve.social.dribbble.style();
  steve.fancybox();

  $.localScroll();
});

var steve = {
  contact: {
    form: {
      get: function() {
        $.get($(this).attr('href'), function(content) {
          $('body').prepend(content);

          $('body > .contact > div')
            .append(
              $('<button/>', {text: 'Close'}).click(function() {
                $('body > .contact').slideUp();
                $('#header').css('border-color', '#555');
              })
            )
            .parent()
            .prependTo('body')
            .slideDown();

          $('#header').css('border-color', '#333');

          steve.email.parse();
          steve.contact.form.validate();
          steve.contact.info.style();

          $('a[href="/contact"]').click(function() {
            $('body > .contact').slideDown();
            $('#header').css('border-color', '#333');
          });
        });
      },

      isSlidable: function() {
        return !$('.page .contact:not(.thank-you)').length;
      },
      
      setSlidable: function() {
        $('a[href="/contact"]')
          .click(steve.contact.form.get)
          .click(function(event) {
            $('html, body').animate({scrollTop:0}, 'slow');
            $('a[href="/contact"]').unbind('click', steve.contact.form.get);
            event.preventDefault();
          });
      },
      
      validate: function() {
        $(".contact form").validate({
          cancelSubmit: false,
          submitHandler: function(form){
            if (!this.sent) {
              this.sent = true;
              $(':submit', form).val('Sending...');
              form.submit();
            } else {
              return false;
            }
          }
        });
      }
    },
    
    info: {
      style: function() {
        var info = this;
        
        $('.contact .info').each(function() {
          info.twitter = $(this).find('a.twitter');
          
          info.twitter.expand = function() {
            $(this).siblings('.email-address').find('span').addClass('expand');
          };
          
          info.twitter.collapse = function() {
            $(this).siblings('.email-address').find('span').removeClass('expand');
          };
          
          info.twitter.hover(info.twitter.expand, info.twitter.collapse);
          
          info.twitter.focus(info.twitter.expand);
          info.twitter.blur(info.twitter.collapse);

          info.email = $('<a/>', {
            'class': 'email',
            'text': 'E-Mail',
            'href': $(this).find('.email-address').attr('href')
          });
          
          info.email.expand = function() {
            $(this).siblings('.email-address').addClass('active');
          };
          
          info.email.collapse = function() {
            $(this).siblings('.email-address').removeClass('active');
          };
          
          info.email.hover(info.email.expand, info.email.collapse);
          
          info.email.focus(info.email.expand);
          info.email.blur(info.email.collapse);
          
          $(this).append(info.email);
          
          $('a', this).show();
        });
      }
    }
  },

  cv: {
    setPrintable: function() {
      // Add print button to CV
      $('.cv .thin.column').append(
        $('<a/>', {title: 'This page is printable!', 'class': 'printable'}).append(
          $('<img/>', {src: '/images/actions/print.png', alt: 'This page is printable!'})
        ).click(function() {
          window.print();
        })
      );
    }
  },

  email: {
    parse: function() {
      // Convert human readable email strings into mailto: hyperlinks

      $('.email-address').each(function() {
        $(this).replaceWith(
          $('<a/>', {
            'href': 'mailto:' + steve.email.dehumanize($(this).text()),
            'class': 'email-address',
            'html': steve.email.dehumanize($(this).html())
          })
        );
      });
    },
    
    dehumanize: function(string) {
      // Convert a human readable email string into an email address
      return string.replace(/[\s]?at[\s]?/g, '@').replace(/[\s]?dot[\s]?/g, '.').replace(/steve[\s]+/g, 'steve').replace(/>[\s]+</g, '><');
    }
  },
  
  fancybox: function() {
    $("a[href $= '.jpg'], a[href $= '.jpeg'], a[href $= '.png'], a[href $= '.gif']").fancybox({
      'zoomOpacity': true,
      'overlayShow': false,
      'zoomSpeedIn': 500,
      'zoomSpeedOut': 500,
      'transitionIn': 'elastic',
      'transitionOut': 'elastic',
      'easingIn': 'easeOutBack',
      'easingOut': 'easeInBack',
      'centerOnScroll': true,
      'hideOnContentClick': false
    });
  },
  
  social: {
    dribbble: {
      style: function() {
        // Show screenshot info on shot hover
        
        $('ol.dribbbles li div.dribbble-img').hover(
          function () {
            $(this).find('a.dribbble-over').stop().fadeTo('fast', 1);
          },
          function () {
            $(this).find('a.dribbble-over').stop().fadeTo('fast', 0);
          }
        );
      }
    }
  }
};
