CmdUtils.CreateCommand({
  names: ['mixi-voice'],
  homepage: 'http://www.greenspace.info/ubiquity/',
  author: { name: 'Hajime Kobayashi'},
  description: 'post a message to the Mixi Voice.',
  help: "You'll need a <a href=\"http://mixi.jp\">mixi account</a> and loggin in.",
  icon: 'http://mixi.jp/favicon.ico',
  arguments: [ { role: 'object', nountype: noun_arb_text, label: 'Your voice' } ],

  preview: function( pBlock, args ){
      pBlock.innerHtml = this.description;
  },

  execute: function( args ){
    var statusText = args.object.text;

    if( statusText.length < 1 ){
      displayMessage("Please enter some message.");
    }
    else if( statusText.length > 140 ){
      displayMessage("Your message is too long!");
    }
    else {
      jQuery.ajax( {
        type: "GET",
        url: "http://mixi.jp/recent_echo.pl",
        dataType: "html",
        error: function(responseData){
          displayMessage( "An error has been occured." );
        },
        success: function(responseData){
          responseData = jQuery( responseData );
          var key = null;
          if( responseData.find('#post_key') ){
            key = responseData.find('#post_key').eq(0).val();
          }

          if( key == null ){
            displayMessage( "It has failed to get post key.(meybe you are not logged in to mixi.)" );
          }
          else {
            jQuery.post({
              type: "POST",
              url: "http://mixi.jp/add_echo.pl",
              data: {
                post_key: key,
                body: statusText,
                redirect: 'recent_echo'
              },
              dataType: "string",
              error: function(){
                displayMessage( "Update has been failed..." );
              },
              success: function(){
                displayMessage( "Your echo has been updated." );
              }
            });
          }
        }
      } );
    }
  }
});


