/* Settings */
var logoNormalOpacity = 0.3;
var logoHoverOpacity = 1;
var tooltipMaxOpacity = 0.85;

$(function(){
  $(document).click(removeExistingTooltips);
  $(".section a").click(showNewTooltip);
  $(".logo").css("opacity", logoNormalOpacity)
    .mouseover(onLogoMouseOver)
    .mouseout(onLogoMouseOut);
  $("#contact-form-name").inlineFieldLabel("Name");
  $("#contact-form-email").inlineFieldLabel("Email");
  $("#contact-form-website").inlineFieldLabel("Website");
  $("#contact-form-message").inlineFieldLabel("Message");
});

jQuery.fn.inlineFieldLabel = function(label) {  
  return this.each(function() {
    var textInput = jQuery(this); // your text input field
    var fieldLabel = label || textInput.attr("title");   // string to put in your text input
    
    if (textInput.val()=="") {
      textInput.addClass("intra-field-label").val(fieldLabel);
    }
    
    textInput.focus(function()
    {
      if (!textInput.hasClass("intra-field-label")) return;
      textInput.removeClass("intra-field-label").val("");
    });
    
    textInput.blur(function()
    {
      if (textInput.val()=="") textInput.addClass("intra-field-label").val( fieldLabel );
    });
    
    textInput.closest("form").submit(function() {
      if (!textInput.hasClass("intra-field-label")) return;
      textInput.val("");
    });
  });
};

function onLogoMouseOver(e) {
  $(this).stop().animate({opacity:logoHoverOpacity}, 300);
}

function onLogoMouseOut(e) {
  $(this).stop().animate({opacity:logoNormalOpacity}, 300);
}

function removeExistingTooltips() {
  $(".tooltip").fadeOut(300, function() { $(this).remove(); });
}

function showNewTooltip(e) {
  removeExistingTooltips();
  var parentClasses = $(e.currentTarget).closest("div.section").attr("class").split(" ");
  var extraClass = "";
  for( var i in parentClasses ) {
    if (String(parentClasses[i]).indexOf("section-")!==false) extraClass = String(parentClasses[i]).replace("section-", "");
  }
  var tooltip = $('<div class="tooltip tooltip-'+extraClass+'">'+$(this).closest("div.section").find(".content").html()+'</div>');
  $("body").append(tooltip);
  tooltip.animate({opacity:tooltipMaxOpacity}, 300);
  return false;
}

/* IE HTML5 shiv */
/*@cc_on'abbr article aside audio canvas details figcaption figure footer header hgroup mark menu meter nav output progress section summary time video'.replace(/\w+/g,function(n){document.createElement(n)})@*/
