function closeBox( elem, ajaxId )
{
  try
  {
    jQuery( ajaxId ).slideUp();
    jQuery( elem ).siblings()
             .find( 'a' )
             .removeClass( 'active' )
    jQuery( elem ).hide();
  }
  catch ( e ) {
//    alert( e );
  }

  return false;
}

function hype( boxId, url )
{
  try
  {
    jQuery.post( url,
                 function( data ) {
                  processResponseActionBar( data, '', boxId, 'hype' );
                 }
              );
  }
  catch ( e ) {
//    alert( e );
  }

  return false;
}

function unhype( boxId, url )
{
  try
  {
    jQuery.post( url,
                 function( data ) {
                  processResponseActionBar( data, '', boxId, 'unhype' );
                 }
              );
  }
  catch ( e ) {
//    alert( e );
  }

  return false;
}

function support( boxId, url )
{
  try
  {
    jQuery.post( url,
                 function( data ) {
                  processResponseActionBar( data, '', boxId, 'support' );
                 }
              );
  }
  catch ( e ) {
//    alert( e );
  }

  return false;
}

function leave( boxId, url, params )
{
  try
  {
    jQuery.post( url,
                 function( data ) {
                  processResponseActionBar( data, '', boxId, 'leave', '', params );
                 }
              );
  }
  catch ( e ) {
//    alert( e );
  }
}

function join( boxId, url, freeJoin, params )
{
  try
  {
    jQuery.post( url,
                 function( data ) {
                  processResponseActionBar( data, '', boxId, 'join', freeJoin, params );
                 }
              );
  }
  catch ( e ) {
//    alert( e );
  }
}

function joinFb( boxId, url, freeJoin, params )
{
  jQuery.alerts.okButton = __( 'L_YES' ).ucfirst();
  jQuery.alerts.cancelButton = __( 'L_NO' ).ucfirst();

  jConfirm(
    __( 'Q_PUBLISH_ACTIVITY_ON_FACEBOOK' ),
    '',
    function( result )
    {
      if ( result )
      {
        url += '/fbpublish:1';
      }

      join( boxId, url, freeJoin, params );
    }
  );
}

function leaveFb( boxId, url, params )
{
  jQuery.alerts.okButton = __( 'L_YES' ).ucfirst();
  jQuery.alerts.cancelButton = __( 'L_NO' ).ucfirst();

  jConfirm(
    __( 'Q_PUBLISH_ACTIVITY_ON_FACEBOOK' ),
    '',
    function( result )
    {
      if ( result )
      {
        url += '/fbpublish:1';
      }

      leave( boxId, url, params );
    }
  );
}

function processResponseActionBar( xhr, successMessage, boxId, action, freeJoin, params )
{
  var result = [false];

  try
  {
    if ( xhr )
    {
      var response = jQuery.secureEvalJSON( xhr );

      if ( response )
      {
        switch ( response.status )
        {
          case 'error':
            var msg = '';
            jQuery.each( response.errors, function() {
              msg += this + '<br />';
            } );

            jQuery( '#error_' + boxId + ' .msg' ).html( msg );
            jQuery( '#error_' + boxId ).show().fadeOut( 2000 );
            result = [ false, msg ];
            break;

          case 'success':
            result = [ true, 'Action ok.' ];

            jQuery( '#success_' + boxId + ' .msg' ).html( response.message );
            jQuery( '#success_' + boxId ).show().fadeOut( 2000 );

            var box = jQuery( '#' + boxId );
            var action_bar = box.parent().prev( '.action-bar' );
            var count = action_bar.find( '.hypes .count_inner' ).html() * 1;

            if ( action == 'hype' )
            {
              action_bar.find( '.hypes .count_inner' ).html( count + 1 );
              action_bar.find( '.hype-this a' )
                .toggleClass( 'hidden' )
                .parent().removeClass( 'hypes' );
              action_bar.find( '.hypes' ).show();
            }
            else if ( action == 'unhype' )
            {
              action_bar.find( '.hypes .count_inner' ).html( count - 1 );

              if ( count - 1 == 0 )
              {
                action_bar.find( '.hypes' ).hide();
                action_bar.find( '.hype-this a' )
                  .parent().addClass( 'hypes' );
              }
              action_bar.find( '.hype-this a' ).toggleClass( 'hidden' );
            }
            else if ( action == 'join' )
            {
              if ( freeJoin == '1' )
              {
                action_bar.find( '.join-leave-this' ).toggleClass( 'hidden' );

                if ( params.boxId && params.objId && params.objType )
                {
                  if ( params.toolId )
                  {
                    jQuery( '#' + params.toolId ).find( '.join-leave-this' ).toggleClass( 'hidden' );
                  }

                  getObjectParticipants( params );
                }
              }
            }
            else if ( action == 'leave' )
            {
              action_bar.find( '.join-leave-this' ).toggleClass( 'hidden' );

              if ( params.boxId && params.objId && params.objType )
              {
                if ( params.toolId )
                {
                  jQuery( '#' + params.toolId ).find( '.join-leave-this' ).toggleClass( 'hidden' );
                }

                getObjectParticipants( params );
              }
            }

            closeAjaxBox( boxId, 5000 );

            break;

          default:
            result = [ false, 'Unknown error.' ];
            break;
        }
      }

    }
  }
  catch ( e ) {
//    alert( e );
  }

  return result;
}

