$(document).ready(function(){
  
  // Links
  $("a[rel=external]").attr('target', '_blank');
  
  // Form
  $("#contact label").inFieldLabels();
  $("textarea").elastic();
  $("#contact form").validate({
      rules: {
        email: {
          email: true
        }
      }
    });
    
  // Tooltips
  $('ul#nav-secondary a[href][title], #newsletter > a[href][title]').qtip({
    content: {
      text: false // Use each elements title attribute
    },
    show: {
      delay: 0
    },
    position: {
      target: 'mouse',
      corner: {
        target: 'topMiddle',
        tooltip: 'topMiddle'
      },
      adjust: { 
        x: 0, y: 21 
      }
    },
    style: {
      background: '#000',
      border: {
        width: 1,
        radius: 5,
        color: '#000'
      },
      tip: {
        corner: 'topMiddle',
        size: {
          x: 20,
          y : 8
        }
      },
      padding: 2,
      color: '#ccc',
      name: 'dark'
    }
  });
    
  
  // Email Form
  $(document).click(function() {
    if ($("#newsletter input[name='EMAIL']").val() == 'Enter your email address...' && $("#newsletter form").is(":visible")) {
      $("#newsletter form").slideToggle(200);
    }
  });
  $(window).focus(function() {
    $("#newsletter form").hide();
  });
  
  $("#newsletter input[name='EMAIL']").attr({value: "Enter your email address..."});
  
  $("#newsletter input[name='EMAIL']").focus(function() {
  	if ($(this).val() == '') {
      $(this).attr({value: "Enter your email address..."});
    }
  }).blur(function() {
  	if ($(this).val() == '') {
      $(this).attr({value: "Enter your email address..."});
    }
  }).click(function() {
    if ($(this).val() == 'Enter your email address...') {
      $(this).attr({value: ""});
    }
  });
  
  
  $(document).keyup(function(e) {
    if (e.keyCode == 27) { $("#newsletter form").hide(); }   // esc
  });
  $("#newsletter a").click(function() {
    $("#newsletter form").slideToggle(200);
    return false;
  });
  
  // Brand Rollovers
  $("div#tutorial li p").fadeTo(0,0).css({bottom: "-69px"});
  $(document).click(function() {
    $("div#tutorial li p").fadeTo(0,0).css({bottom: "-69px"});
  });
  $("div#tutorial li a").hover(function() {
  		var thumbOver = $(this).find("img").attr("src");
  		$(this).css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});
  		$(this).find("img").stop().fadeTo(200, 0 , function() {
  			$(this).hide()
  		});
  		$(this).next("p").hoverFlow('mouseenter', { opacity:"1.0",bottom: "-84px" }, 500);
  	} , function() {
  		$(this).find("img").stop().fadeTo(200, 1).show();
  		$(this).next("p").hoverFlow('mouseout', { opacity:"0",bottom: "-69px" }, 100);
  	});
  	
});
