/*
Script to expand bit.ly links to their original form
*/
CmdUtils.CreateCommand({
  names: ["bitly-expand"],
  icon: "http://bit.ly/static/images/favicon.png",
  description: "Expand bit.ly link",
  help: "Highlight a bit.ly link.  The preview pane should show some bit.ly info and pressing enter will take you to the bit.ly info page.",
  author: {name: "Alex Beutel", email: "alex.beutel@gmail.com"},
  license: "GPL",
  homepage: "http://blog.ambmediadesign.com/",
  arguments: [{role: 'object', label: 'bit.ly address', nountype: noun_arb_text}],
  self: this,
  newUrl: '',
  preview: function preview(pblock, args) {
    var ar = args.object.text.split("/");
    var hash = ar[ar.length - 1];
    var url = "http://api.bit.ly/expand?version=2.0.1&shortUrl="+args.object.text+"&login=ambpublic&apiKey=R_ab4bccbd378ad4a647019ad9d36a3ca4 ";
    var prev = pblock;
    pblock.innerHTML = "Looking up expanded URL for  <b>" + args.object.text + "</b>.<br /> AJAX go!";
    $.ajax({
       type: "GET",
       url: url,
       async: true,
       dataType: 'json',
       success: function(json) {
			newUrl = json['results'][hash]['longUrl'];
			prev.innerHTML = newUrl."<br /><br />Press enter to replace in your highlighted text.";
       }
    });
  },
  execute: function execute(args) {
    var ar = args.object.text.split("/");
    var hash = ar[ar.length - 1];
    CmdUtils.setSelection("<a href='"+newUrl+"' target='_blank'>" + newUrl + "</a>");
  }
});
