(function ($) {
  Cufon.replace([
    'p.intro',
    '#primary nav a',
    '#circles li.header',
    '#circles li a',
    '#content > header h2',
    '#content > header h1',
    '#content > header p',
    '#content article footer'
  ].join(', '));
    
    
  $.fn.modalClose = function () {
    $("#overlay").fadeOut(300);
    return this.hide();
  };
  
  $.fn.modalOpen = function () {
    $("#overlay").fadeTo(300, 0.5);
    return this.show();
  };
  
  $(function () {
    // Extra markup added via JS
    $("<span />", {className: "point"}).prependTo('li.current a');
    
    // Auto tie all a.submit buttons to 
    // trigger the submit function on the form
    $("form.normal a.submit").live("click", function (e) {
      e.preventDefault();
      $(this).closest('form').submit();
    });
    
    // For fields already on the page
    $("select").uniform(); 
    
    // Setup Contact Us form on page
    $.get('_contact.html', function (data) {
      $(data)
        .hide()
        .find("select")
          .uniform()
          .end()
        .find("form")
          .submit(function (e) {
            e.preventDefault();
            
            var form = $(this), actions = form.find(".actions");
            
            $(this).find("input:text, textarea").each(function () {
              $(this).toggleClass('invalid', $(this).val() === "");
            });
            
            if ($(this).find(".invalid").length) {
              return;
            }
            
            actions.addClass('loading');
            
            $.post('send.php', $(this).serialize(), function (data) {
              actions.removeClass('loading');
              if (data && data.success) {
                form.closest(".modal").modalClose();
              } else {
                form.find(".error").remove();
                $("<div />", {className: "error", text: "There was a problem submitting your suggestion. Please make sure the form is filled out correctly and try again."}).prependTo(form);
              }
            }, 'json');
            
          })
          .end()
        .appendTo(document.body);
        
      $("<div />", {id: "overlay"}).hide().appendTo(document.body);
    }, "html");
    
    // Open form
    $("#suggest").click(function (e) {
      e.preventDefault();
      $("#submit_contact").modalOpen();
    });

    // Open contact form
    $(".contact-popup").click(function (e) {
      e.preventDefault();
      $("#submit_contact").modalOpen();
    });
    
    // Close any modal box
    $("div.modal a.cancel").live("click", function (e) {
      e.preventDefault();
      $(this).closest('.modal').modalClose();
    });
    
    var 
      // Search for tweets TO @carbonads:
      // url          = "http://search.twitter.com/search.json?callback=?&q=to%3Acarbonads&rpp=1",
      // Search for tweets FROM @carbonads:
      url          = "http://search.twitter.com/search.json?callback=?&q=from%3Acarbonads&rpp=1",
      prepareTweet = function (text) {
      // Code from: http://css-tricks.com/snippets/jquery/jquery-tweetify-text/
      return text.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi, '<a href="$1">$1</a>')
                 .replace(/(^|\s)#(\w+)/g, '$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>')
                 .replace(/(^|\s)@(\w+)/g, '$1<a href="http://twitter.com/$2">@$2</a>');
    };
    
    $.getJSON(url, function (data) {
      if (data && data.results && data.results.length) {
        var tweet = prepareTweet(data.results[0].text);
        $("#tweet")
          .html(tweet + " &mdash; " + prepareTweet("@" + data.results[0].from_user))
          .add("body > footer h3")
          .fadeIn(500);
      }
    });
    
  });
}(jQuery));

