// trunc.it API

// Creates a <script src='trunc.it'> call that
// circumvents the inability of XMLHttpRequest to make cross-domain calls

var TruncIt=new TruncItObject();
function shorten_complete(url) {
 if(TruncIt.callback) {
  TruncIt.url=url;
  TruncIt.success=true;
  TruncIt.callback();
 }
 TruncIt.remove('shrtrm');
 TruncIt.busy = false;
}
function lookup_complete(url,type,desc) {
 if(TruncIt.callback && typeof(url) != 'number') {
  TruncIt.url=url;
  TruncIt.type=type;
  TruncIt.desc=desc;
  TruncIt.success=true;
  TruncIt.callback();
 }
 TruncIt.remove('lookrm');
 TruncIt.busy = false;
}
function tweet_complete(msg) {
 if(TruncIt.callback) {
  TruncIt.msg=msg;
  TruncIt.success=true;
  TruncIt.callback();
 }
 TruncIt.remove('tweetrm');
 TruncIt.busy = false;
}
function TruncItObject()
{
this.success = // Status
this.busy =    // Mutex
this.url =     // Short URL
this.source =  // Long URL
this.type =    // Type
this.desc =    // Description
this.msg =     // Tweet Message
this.callback = false; // User Supplied Callback
this.shorten=function(source,callback) { this.proc(source,callback,'http://trunc.it/addurl_api.htm?key='+source,'shrtrm'); }
this.lookup=function(source,callback) { this.proc(source,callback, source + '?api=TRUE','lookrm'); }
this.tweet=function(source,callback) { this.proc(source,callback,'http://trunc.it/shorten_tweet.htm?type=api&text='+encodeURIComponent(source),'tweetrm'); }
this.append=function(src,id) { // Create <script> and append, this calls our servers
 var e = document.createElement('script');
 e.setAttribute('id',id);
 e.setAttribute('src',src);
 document.getElementsByTagName("body")[0].appendChild(e);
}
this.remove=function(id) { setTimeout(function() { document.getElementsByTagName("body")[0].removeChild(document.getElementById(id)); }, 0); }
this.proc=function(source,callback,url,id) {
 if(this.busy) return;
 this.busy=true;
 this.success=false;
 this.source=source;
 this.callback=callback;
 this.append(url,id);
}

}

