/*
	06-Apr-2010	SAK
	This JavaScript file downloaded from http://s.bit.ly/TweetAndTrack.js?v=1.01
	and customized to add blog post Title to Twitter.
	
    requires bitly javascript api:
    http://code.google.com/p/bitly-api/wiki/JavascriptClientApiDocumentation
*/

var TweetAndTrack = {};
TweetAndTrack.open = function(targ, url, title) {

    //var child_spans = targ.getElementsByTagName('span');
    //    if (child_spans && child_spans.length > 0) {
    //        var msg = child_spans[0].innerHTML.toString();
    //    } else {
    //        msg = '';
    //    }

    //Limit the title to first hundred characters, as the Twitter limitation is 140 characters, including the URL.
    var msg = title;
    if (msg.length > 100) {
        msg = msg.substring(0, 97) + "...";
    }

    var callback_name = url.replace(/\W/g, '');
    BitlyCB[callback_name] = function(data) {
        var result = TweetAndTrack.popResult(data);
        var tweet_url = "http://twitter.com/home?status=" + encodeURIComponent( msg + " - " + result.shortUrl );
        TweetAndTrack.newWindow(tweet_url, "600", "400");
    };
    BitlyClient.call('shorten', {'longUrl': url, 'history': '1'}, 'BitlyCB.' + callback_name);
    // BitlyClient.shorten(url, 'BitlyCB.' + callback_name);
    return false;
};

TweetAndTrack.popResult = function(data) {
    // Results are keyed by longUrl, so we need to grab the first one.
    for (var r in data.results) {
        return data.results[r];
    }
};

TweetAndTrack.newWindow = function(url, width, height) {
  var a = function() {
    if(!window.open(url,'t','scrollbars=yes,toolbar=1,resizable=1,status=1,width='+width+',height='+height))document.location.href=url;
  };
  
  if( /Firefox/.test(navigator.userAgent)) {
    setTimeout(a,0);
  } else {
    a();
  }
};