CmdUtils.CreateCommand( {
	names: [ "sphinn", "share using sphinn" ],
	homepage: "http://erikvold.com/tools/ubiquity/sphinn/sphinn.cfm",
	icon: "http://sphinn.com/favicon.ico",
	author: { name:"Erik Vergobbi Vold", email: "erikvvold@gmail.com" },
	description: "If not yet submitted, submits the page to Sphinn. Otherwise, it takes you to the story's Sphinn page.",
	version: "0.3",
	execute: function( args ) {
		var selectedURL = CmdUtils.getWindow().location;

		jQuery.ajax( {
			type: "GET",
			dataType: "text",
			url: "http://sphinn.com/evb/url.php?url=" + selectedURL,
			success: function( response ) {
				var urlStr = (response+"").match( /http:\/\/sphinn.com\/story\/\d+/i );
				if( urlStr != null ) {
					Utils.openUrlInBrowser( urlStr );
				}
				else {
					Utils.openUrlInBrowser( "http://sphinn.com/index.php?c=post&m=submit&link=" + encodeURIComponent( selectedURL ) );
				}
			}
		});
		return true;
	},
	preview: function( pblock, args ) {
		var selectedURL = CmdUtils.getWindow().location;

		CmdUtils.previewAjax( pblock, {
			type: "GET",
			url: "http://sphinn.com/evb/url.php?url=" + selectedURL,
			error: function() {
				pblock.innerHTML = _( "Sphinn this page." );
			},
			success: function( response ) {
				response = jQuery.trim( response );

				if( !response.match( /<b>sphinn it!<\/b>/i ) ) {
					var tempText = "Sphinn this page. This page has been Sphinned ${sphinnCount} time(s).";
					var previewData = { sphinnCount: response.match( /<b>(\d+)<\/b>/i )[1] };
					pblock.innerHTML = _( CmdUtils.renderTemplate( tempText, previewData ) );
				}
				else{
					pblock.innerHTML = _( "Sphinn this page. It has not been submitted yet, be the first." );
				}
			}
		});
		return true;
	}
});