<!--

var twitter_url = '';

function getTwitterURL() {
  if (twitter_url == '') { // Twitter URL not yet.
    BitlyClient.shorten(window.location.href, 'BitlyCB.shortenResponse'); // Start building Twitter URL.
  }
}

BitlyCB.shortenResponse = function(data) {
  var short_url = '';
  var first_result;
  // Results are keyed by longUrl, so we need to grab the first one.
  for (var r in data.results) {
    first_result = data.results[r];
    break;
  }
  for (var key in first_result) {
    if (key == 'shortUrl') {
      short_url = first_result[key].toString();
      break;
    }
  }

  if (short_url != '') {
    twitter_url = 'http://twitter.com/home?status=' + encodeURIComponent('Reading: ' + window.document.title + ' (' + short_url + ')');

		if (document.getElementById) {
      var twitterLinkObj = document.getElementById("twitterLink"); // Get link obj.
      if (twitterLinkObj) { // Link object exists.
        twitterLinkObj.href = twitter_url; // Set link's HREF to the URL that was just built.
      }
    }
  }
}

function launchTwitterURL(obj, tries) {
  if (twitter_url == '') { // Twitter URL not yet built.
    getTwitterURL(); // Start building Twitter URL.
    pause(4000); // Wait for Twitter URL to be built.
  }
  return true; // Launch link.
}

function pause(milliseconds) {
	var dt = new Date();
	while ((new Date()) - dt <= milliseconds) { /* Twiddle digits furiously! */ }
}

// -->
