/*
 * $Revision: 1.14 $
 * $Author: danderson $
 */
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;
      }

      var sDefaultShowEffect = 'clip';
      if( !jQuery.support.opacity )
      {
        sDefaultShowEffect = null;
        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>');
      $( 'body' ).append( oLoadingContainer.hide() );

      /*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,
                                 draggable:      false,
                                 close:          function()
                                                     {
                                                       $( this ).dialog( 'destroy' ).remove();
                                                     }
                                 } ).show();

      /*Get Requested Dialog*/
      $.ajaxSetup( { cache:true } );
      $.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"
                                                 },
                                 resizable:      true,
                                 draggable:      false,
                                 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' ).empty().remove();

          /*Display the new dialog*/
          oThisContainer.html( oData.page_data ).dialog( oDialogOptions )/*.dialog( 'open' ).show();*/
          vneOnload.trigger();
        },
        'json' );

    };
  } )( window.jQuery );
}