/**
 *  myStatusDialog.js
 *    provide functionality to user for quickly updating status
 *
 *  @copyright Copyright (c) 2010, Yakabod, Inc.
 *
 *  @uses /js/vne/jquery/core/jquery.js
 *
 *  $Revision: 1.9 $
 *  $Date: 2012/01/11 15:37:37 $
 *  Last edited by: $Author: danderson $
 *
 *  Last eror free JSHint: 20110525 11:24
 *  @link http://jshint.com/
 *    Checked Options:
 *      Disallow bitwise operators
 *      Require curly braces around all blocks
 *      Require ===
 *      Require variables to be declared before usage
 *      Environment
 *        Browser
 *        jQuery
 *
 * TODO: generecizing the associated markup and modifying the structure of this code
 *       to make it all a UI widget would be very nice as it would allow this display
 *       to appear more than once per page, if necessary, and to be configurable
 */
( function( global )
{
  "use strict";

  var $,
      bSuspended, sSaveButtonText, sSuspendedSaveButtonClass,
      $EditStatusDialog, $EditStatusForm, $StatusEntryField, $ViewStatus, $UIDialogContainer,
      setPlaceholder, suspendAction, awakenAction, updateButton, toggleSaveSuspend, readStatusValue, doSave,
      oDialogButtons, oModuleOptions, oEditStatus;

  $ = global.jQuery;

  /*
   * Setting it all up
   */
  bSuspended = false;
  sSaveButtonText = 'Save';
  sSuspendedSaveButtonClass = 'ybx-status-savebutton-suspended';

  $EditStatusDialog = $( '#editStatus' );
  $EditStatusForm = $( '#myStatusForm' );
  $StatusEntryField = $( '#Title' );
  $ViewStatus = $( '#viewStatus' );

  setPlaceholder = function( sPlaceholder )
  {
    if( typeof sPlaceholder === 'undefined' )
    {
      sPlaceholder = $StatusEntryField.val();
    }

    $StatusEntryField.data( 'placeholder', sPlaceholder );
  };

  suspendAction = function( $button )
  {
    bSuspended = true;
    updateButton( $button, '<span class="ui-button-text"><img src="/images/loadingsmall.gif" style="float:left; *float:none;" />&nbsp; Saving</span>' );
  };

  awakenAction = function( $button )
  {
    bSuspended = false;
    updateButton( $button );
  };

  updateButton = function( $button, sButtonText )
  {
    if( typeof sButtonText === 'undefined' )
    {
      sButtonText = '<span class="ui-button-text">' + sSaveButtonText + '</span>';
    }

    $button
      .html( sButtonText )
      .toggleClass( sSuspendedSaveButtonClass )
      .prop( 'disabled', bSuspended );
  };

  toggleSaveSuspend = function( bDoSuspend )
  {
    var sAwakenedButtonSelector, sSuspendedButtonSelector;

    sAwakenedButtonSelector = 'div.ui-dialog-buttonpane button:contains(' + sSaveButtonText + ')';
    sSuspendedButtonSelector = 'button.' + sSuspendedSaveButtonClass;

    if( typeof bDoSuspend === 'boolean' )
    {
      if( bDoSuspend )
      {
        suspendAction( $UIDialogContainer.find( sAwakenedButtonSelector ) );
      }
      else
      {
        awakenAction( $UIDialogContainer.find( sSuspendedButtonSelector ) );
      }
    }
    else
    {
      if( bSuspended )
      {
        awakenAction( $UIDialogContainer.find( sSuspendedButtonSelector ) );
      }
      else
      {
        suspendAction( $UIDialogContainer.find( sAwakenedButtonSelector ) );
      }
    }
  };

  readStatusValue = function()
  {
    return $.trim( $StatusEntryField.val() );
  };

  doSave = function()
  {
    var sUpdatedStatus;

    if( ! bSuspended )
    {
      toggleSaveSuspend( true );

      sUpdatedStatus = readStatusValue();
      if( sUpdatedStatus === '' )
      {
        sUpdatedStatus = 'cleared status';
        $StatusEntryField.val( sUpdatedStatus );
      }

      $EditStatusForm.ajaxSubmit( {
        success: function( oServerResponse )
        {
          if( oServerResponse.redirectUrl )
          {
            global.location.href = oServerResponse.redirectUrl;
          }
          else
          {
            /* ollowing line removes markup */
            sUpdatedStatus = $( '<div>' + sUpdatedStatus + '</div>' ).text();

            setPlaceholder( sUpdatedStatus );
            $ViewStatus.text( sUpdatedStatus );
          }

          $EditStatusDialog.dialog( 'close' );
        },
        dataType: 'json'
      } );
    }
  };

  oDialogButtons = {};
  oDialogButtons.Cancel = function()
  {
    if( ! bSuspended )
    {
      $StatusEntryField.val( $StatusEntryField.data( 'placeholder' ) );
      $( this ).dialog( 'close' );
    }
  };
  oDialogButtons[sSaveButtonText] = function()
  {
    doSave();
  };
  /*
   * TODO: expand capability of status updates in the future such that an actual
   * vlogentry content-add can be opened
   *
    oDialogButtons['>'] = function()
    {
      $EditStatusDialog.dialog( 'close' );
      var eventTriggers =
      {
        success: {
          events: [ 'StatusUpdate' ],
          allowDefault: false
        }
      };
      $(document).bind('StatusUpdate', function(oEvent, oServerResponse)
      {
        $ViewStatus.text( $( '<div>' + oServerResponse.object.sText + '</div>' ).text() );
      });
      (new ContentAddModuleController((new ContentAddSettings_VLogEntry()).setContainerUrn( 'vne::VLOG:' ).setEventTriggers(eventTriggers).getFullSettings()));
      return false;
    }
  */

  /*
   * Dialog the update form
   */
  oModuleOptions = {
    autoOpen: false,
    position: ['center' , 100],
    width: 550,
    height: 210,
    resizable: false,
    buttons: oDialogButtons,
    open: function()
    {
      if( readStatusValue() === '' )
      {
        $StatusEntryField.val( $StatusEntryField.data( 'placeholder' ) );
      }
    },
    beforeClose: function()
    {
      $StatusEntryField
        .each( function()
        {
          this.blur();
        } );
      toggleSaveSuspend( false );
    },
    close: function()
    {
      /* Override default from jquery.yakabox.settings.js to avoid dialog destruction */
    }
  };

  /* Add show effect if not IE. (IE messes up with the animations) */
  oEditStatus = global.document.getElementById( 'editStatus' );
  if(( oEditStatus ) && ( typeof oEditStatus.style.opacity !== 'undefined' ))
  {
    oModuleOptions.show =  {effect: 'bounce', duration: 180, times:3};
  }

  /*
   * Connecting all the parts to the DOM
   */
  $EditStatusDialog
    .show()
    .dialog( oModuleOptions );

  $UIDialogContainer = $EditStatusDialog
    .parents( '.ui-dialog' );

  $ViewStatus
    .click( function()
    {
      $EditStatusDialog.dialog( 'open' );
      $StatusEntryField
        .focus( function ()
        {
          if( $StatusEntryField.val() === $StatusEntryField.data( 'placeholder' ) )
          {
            $StatusEntryField.val( '' );
          }
        } );
    } );

  setPlaceholder();
} )( window );
