CmdUtils.CreateCommand({
	name: "relshortlink",
	homepage: "http://www.erikvold.com/tools/ubiquity/relshortlink/relshortlink.cfm",
	icon:"",
	description:'Checks if the link provided has published a shortened version of the given page using a HTML link element with rel="shortlink".',
	help:"",
	author: { name: "Erik Vold", email: "erikvvold@gmail.com"},
	contributors: ["Erik Vold"],
	license: "MPL",
	version: "0.1",
	takes: {"word": noun_arb_text},
	preview: function (pblock, directObject) {
		// Display immediate preview message.
		pblock.innerHTML = "Replaces the selected url with ";

		// get selected url
		var selectedURL=jQuery.trim(directObject.text);
		if(selectedURL==""){
			selectedURL=context.focusedWindow.document.location;
		}

		// get relshortlink url
		CmdUtils.previewAjax(pblock,{
			type:"GET",
			dataType:"text",
			url:"http://rel-shortlink.appspot.com/api?url="+encodeURIComponent(selectedURL),
			error:function(){
				displayMessage("There was an error.");
			},
			success:function(response){
				var shortURLString = jQuery.trim(response+"");

				// display the relshortlink url
				pblock.innerHTML = "Replaces the selected url with "+shortURLString;

				return;
			}
		});

		return;
	},
	execute: function( directObject ) {
		// get selected url
		var selectedURL=jQuery.trim(directObject.text);
		if(selectedURL==""){
			selectedURL=context.focusedWindow.document.location;
		}

		// get relshortlink url
		jQuery.ajax({
			type:"GET",
			dataType:"text",
			url:"http://rel-shortlink.appspot.com/api?url="+encodeURIComponent(selectedURL),
			error:function(){
				displayMessage("There was an error.");
			},
			success:function(response){
				var shortURLString = jQuery.trim(response+"");

				// set the selection to the revcanonical url
				CmdUtils.setSelection(shortURLString);	

				return;
			}
		});

		return;
	}
});

