/// <Reference Path="~/js/jquery-1.4.1-vsdoc.js" />

$(document).ready(function () {
  var tipStyle = {};

  tipStyle.name = 'cream';
  tipStyle.tip = true;

  tipStyle.border = {};

  tipStyle.border.width = 1;
  tipStyle.border.radius = 5;

  var tipPosition = {};

  tipPosition.corner = {};

  tipPosition.corner.target = 'rightMiddle';
  tipPosition.corner.tooltip = 'leftMiddle';

  $('a.tooltip').qtip({ style: tipStyle, position: tipPosition });

  $('a.tooltip').click(function (evt) {
    // Prevent tooltip links from navigating when clicked.
    evt.preventDefault();

    // Prevent tooltip links from being highlighted/focused when clicked.
    this.blur();
  });

  // If there are any inputs marked as required, then enable validation on them.
  // This assumes that the jQuery validation plugin is included on any page
  //  with a "required" input element.
  if ($('input.required').length) {
    $('form').validate({
      errorElement: 'span'
    });
  }

  if (document.getElementById('Twitter') !== null) {
    $.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?id=35Atl&skip_user=true&callback=?',
      function (response) {
        // Preferably, look for the first non-reply tweet.
        for (var i = 0; i < response.length; i++) {
          if (response[i].in_reply_to_screen_name === null) {
            displayStatus(response[i]);

            return;
          }
        }

        // Else, if I've been particularly chatty, take the first reply instead.
        displayStatus(response[0]);
    });
  }
});

function displayStatus(status) {
  // Linkify the status text.
  var status = status.text.replace(/(http:\/\/[A-Z0-9\/\._-]+\w)/gi, '<a href="$1" target="_blank">$1</a>');

  var $status = $('<blockquote/>');

  $status.css('display', 'none');

  $status.html(status);

  $('#Twitter').after($status);

  $status.fadeIn(500);
}
