/*
 * $Revision: 1.15 $
 * $Author: jauldrid $
 */
if( window.jQuery )
{
  ( function( $ )
  {
    $.DynamicDialog = function( oOptions )
    {
      /* Defaults and Check for URL*/
      var oCallback = null;
      if( typeof oOptions !== 'object' || oOptions === null )
      {
        return null;
      }

      if(( typeof oOptions.sURL === 'undefined' ) ||
         ( oOptions.sURL.replace( /^\s*$/, '' ) == '' ))
      {
        return null;
      }

      if(( typeof oOptions.sLoadingMessage === 'undefined' ) ||
         ( oOptions.sLoadingMessage.replace( /^\s*$/, '' ) == '' ))
      {
        oOptions.sLoadingMessage = 'Loading...';
      }

      sDynamicDialogId = 'jqDynamicDialog';
      if(( typeof oOptions.sDynamicDialogId !== 'undefined' ) &&
         ( oOptions.sDynamicDialogId.replace( /^\s*$/, '' ) != '' ))
      {
        sDynamicDialogId = oOptions.sDynamicDialogId;
      }

      if( typeof oOptions.show !== 'undefined' )
      {
        delete oOptions.show;
      }

      if( typeof oOptions.oRequestParameters === 'undefined' )
      {
        oOptions.oRequestParameters = {};
      }

      if( typeof oOptions.oCallback === 'function'  )
      {
        oCallback = oOptions.oCallback;
        delete oOptions.oCallback;
      }

      /*Craete container*/
      var oThisContainer = $( '<div id="' + sDynamicDialogId + '"></div>').hide();
      var oLoadingContainer = $( '<div><img src="/images/loadingsmall.gif" />' + oOptions.sLoadingMessage + '...</div>');

      /*Show loading*/
      oLoadingContainer.dialog( {
        autoOpen: true,
        modal: true,
        bgiframe: true,
        position: "center",
        width: 200,
        height: 75,
        closeOnEscape: false,
        dialogClass: 'dialogLoading',
        overlay: {
          opacity: 0.5,
          background: "white"
        },
        resizable: false,
        /* Animations on this dialog cause a race condition where it may
         * get removed before it is done showing, leaving dead elements
         * on screen.  For the same reason, we won't begin loading the real
         * dynamic dialog until this one has opened (inside th open method)
         */
        show: null,
        open: function()
        {
          /*Get Requested Dialog*/
          $.post(
            oOptions.sURL,
            oOptions.oRequestParameters,
            function( oData )
            {
              /*Set Default Options*/
              var oDialogOptions = {
                autoOpen: true,
                modal: true,
                bgiframe: true,
                position: 'center',
                dialogClass: '',
                overlay:        {
                  opacity: 0.5,
                  background: 'white'
                },
                closeOnEscape:  false,
                close:          function()
                {
                  $( this ).dialog( 'destroy' ).remove();
                  if( typeof oCallback === 'function' )
                  {
                    oCallback();
                  }
                }
              };
              /*Overwrite default options with options from the request*/
              if( typeof oData.oDialogOptions !== 'undefined' && oData.oDialogOptions !== null )
              {
                oDialogOptions = $.extend( oDialogOptions, oData.oDialogOptions );
              }

              $( 'body' ).append( oThisContainer );

              /*Close and remove the loading dialog*/
              oLoadingContainer.dialog( 'close' ).remove();

              /*Display the new dialog*/
              oThisContainer.html( oData.page_data ).dialog( oDialogOptions );
              vneOnload.trigger();
            },
          'json' );
        },
        close: function()
        {
          $( this ).dialog( 'destroy' ).remove();
        }
      } );
    };
  } )( window.jQuery );
}