/**
 * C4U Javascript library for creating dialogs or windows.
 * 
 * Copyright (c) 1999-2010 by Community4you GmbH
 */

/**
 * Shows the progress dialog.
 * 
 * @param theProgressDialogType: - none: none - null or wait_dialog: modal
 * dialog - animation_small: 16x16 image - animation_large: 32x32 image
 * @param theParentID the optional id of the parent to display the symbol (used
 * for the icon types)
 * @param theWidth the optional dialog width
 */
function showProgressDialog(theProgressDialogType, theParentID, theWidth)
{
  if (!theProgressDialogType || (theProgressDialogType == 'wait_dialog'))
  {
    var _div = jQuery("#progressdialog");
    if (_div.size() >= 1)
    {
      _div.dialog('destroy');
      _div.dialog(
      {
        bgiframe : true,
        closeOnEscape: false,
        modal : true,
        autoOpen : false,
        resizable : false,
        draggable : false,
        zIndex : 9999,
        dialogClass : 'progress_div_active',
        height : 90,
        maxHeight : 90,
        minHeight : 90        
      });
      
      if (theWidth)
        _div.dialog('option', 'width', theWidth);
      
      _div.dialog('open');
    }
  }
  else if ((theProgressDialogType == 'animation_small')
      || (theProgressDialogType == 'animation_large'))
  {
    if (theParentID)
    {
      var _id = '#' + theParentID;
      var _div = jQuery(_id);

      if (_div.size() == 1)
      {
        var _newID = theParentID + '_wait_symbol';
        jQuery('body').append(
            '<div id="' + _newID + '" class="wait_' + theProgressDialogType
                + '" >&nbsp;</div>');

        _id = '#' + _newID;
        _div = jQuery(_id);
        _div.css('z-index', '99999');

        _div.center(theParentID, true);
      }
    }
    else
    {
      var _id = 'body';
      var _div = jQuery(_id);

      if (_div.size() == 1)
      {
        var _newID = 'body_wait_symbol';
        jQuery('body').append(
            '<div id="' + _newID + '" class="wait_' + theProgressDialogType
                + '" >&nbsp;</div>');

        _id = '#' + _newID;
        _div = jQuery(_id);
        _div.css('z-index', '99999');

        _div.center(null, true);
      }
    }
  }

  // return a value, because this is used in onsubmit
  return true;
}

/**
 * Closes the progress dialog.
 * 
 * @param theProgressDialogType
 * @param theParentID the optional id of the parent to display the symbol (used
 * for the icon types)
 */
function hideProgressDialog(theProgressDialogType, theParentID)
{
  if (!theProgressDialogType || (theProgressDialogType == 'wait_dialog'))
  {
    var _div = jQuery("#progressdialog");
    if (_div.size() >= 1)
    {
      _div.dialog('destroy');
    }
  }
  else if ((theProgressDialogType == 'animation_small')
      || (theProgressDialogType == 'animation_large'))
  {
    if (theParentID)
    {
      var _id = "#" + theParentID + '_wait_symbol';
      var _div = jQuery(_id);

      if (_div.size() == 1)
      {
        _div.remove();
      }
    }
  }
}

/**
 * Creates a dialog.
 * 
 * @param theID the id of the dialog element
 * @param theTitle the dialog title
 * @param theCSSClass the optional css class
 * @param aShowCloseLink if true, show the close link
 * @param aMoveable if true, the dialog is moveable
 * @param aResizeable if true, the dialog is resizeable
 * @param theWidth the optional width
 * @param theHeight the optional height
 * @param aShowButtonPanel the optional button panel
 */
function createDialog(theID, theTitle, aShowCloseLink, aMoveable, aResizeable,
    aShowButtonPanel, theCSSClass, theWidth, theHeight)
{
  jQuery(document).ready(function()
  {
    var _id = '#' + theID;
    var _div = jQuery(_id);

    // destroy before
      jQuery(_div).dialog('destroy');

      // create dialog
      jQuery(_div).dialog(
      {
        autoOpen : false,
        modal : true,
        bgiframe : true,
        closeOnEscape: false
      });

      // set parameters
      if (theTitle)
        _div.dialog('option', 'title', theTitle);
      if (theCSSClass != null)
        _div.dialog('option', 'dialogClass', theCSSClass);
      if (theWidth)
        _div.dialog('option', 'width', theWidth);
      else
        _div.dialog('option', 'width', 'auto');
      if (theHeight)
        _div.dialog('option', 'height', theHeight);
      else
        _div.dialog('option', 'height', 'auto');
      if (aMoveable)
        _div.dialog('option', 'draggable', true);
      else
        _div.dialog('option', 'draggable', false);
      if (aResizeable)
        _div.dialog('option', 'resizable', true);
      else
        _div.dialog('option', 'resizable', false);

      // button panel
      if (aShowButtonPanel)
      {
        _div.dialog('option', 'buttons',
        {
          "Ok" : function()
          {
            jQuery(this).dialog("close");
          }
        });

        // remove the buttons, so that we can add other html stuff
        _div.parent().find(".ui-dialog-buttonpane").empty();
      }

      // hide close button?
      if (!aShowCloseLink)
      {
        var _anchor = _div.parent().find('.ui-dialog-titlebar-close');
        if (_anchor.size() == 1)
        {
          _anchor.css("display", "none");
        }
      }

      // if there is a button panel inside, copy it to the real panel
      var _panel = _div.find(".ui-dialog-titlebar");
      _div.parent().append(_panel.clone());
      _panel.css("display", "none");

      // patch the close action => try to find a cancel or close button first
      if (aShowCloseLink)
      {
        var _actionID = "_close";
        var _button = _div.find("input[id$='_close']");
        if (_button.size() == 0)
        {
          _button = _div.find("input[id$='_cancel']");
          _actionID = '_cancel';
          
          if (_button.size() == 0)
          {
            _button = _div.find("input[id$='_no']");
            _actionID = '_no';
            
            if (_button.size() == 0)
            {
              _button = _div.find("input[id$='_ok']");
              _actionID = '_ok';
            }
          }
        }

        if (_button.size() == 1)
        {
          _div.bind('dialogbeforeclose', function(theEvent, theUI)
          {
            // make a form submit
            var _action = _div.parent().find("input[name='_action']");
            if (_action.size() == 1)
            {
              _action[0].value = _actionID;
              return _div.find("form")[0].onsubmit();
            }

            return true;
          });
        }
      }

      // show dialog
      jQuery(_div).dialog('open');
    });
}

/**
 * Opens a previously created dialog.
 * 
 * @params theID the id of the dialog element.
 */
function openDialog(theID)
{
  jQuery(document).ready(function()
  {
    var _id = '#' + theID;
    var _div = jQuery(_id);
    if (_div.size() != 1)
      return;

    _div.dialog('open');
  });
}

/**
 * Closes a previously created dialog.
 * 
 * @params theID the id of the dialog element.
 */
function closeDialog(theID)
{
  jQuery(document).ready(function()
  {
    var _id = '#' + theID;
    var _div = jQuery(_id);
    
    if (_div.size() != 1)
      return;

    _div.dialog('destroy');
    
    _div = jQuery(_id);    
    if (_div.size() == 1)
      _div.detach();
  });
}

/**
 * Closes a previously created dialog.
 * 
 * @params theID the id of the dialog element.
 */
function closeWindow(theID)
{  
  window.close();
}

/**
 * Open an url in a new window.
 *
 * @param theWindowname the window name
 * @param theURL the url
 * @param theWidth the width
 * @param theHeight the height
 * @param aBrowserControls menubar, statusbar, ... on?
 * @param aScrollbars scrollbars on?
 * @param aResizeable resizeable on?
 * @param aFullScreen fullscreen?
 * @return the window
 */
function openWindow(theWindowname, theURL, theWidth, theHeight, aScrollbars, aBrowserControls, aResizeable, aFullScreen)
{
  if (!theWindowname)
  {
    var _now = new Date();

    theWindowname = _now.getMilliseconds();
  }
  
  var _parameter = "";
  
  if (aBrowserControls)
    _parameter = "location=yes,menubar=yes,status=yes,toolbar=yes";
  else
    _parameter = "location=no,menubar=no,status=no,toolbar=no";
  
  if (aScrollbars)
    _parameter += ",scrollbars=yes";
  else
    _parameter += ",scrollbars=no";
  
  if (aResizeable)
    _parameter += ",resizable=yes";
  else
    _parameter += ",resizable=no";
  
  if (aFullScreen)
  {
    if (!theWidth)
      theWidth = screen.availWidth;
    if (!theHeight)
      theHeight = screen.availHeight;
  }
  
  if (theWidth)
    _parameter += ",width=" + theWidth;
  if (theHeight)
    _parameter += ",height=" + theHeight;
  
  var _w = window.open(theURL, theWindowname, _parameter);
  
  if (aFullScreen)
  {
    _w.moveTo(0, 0);
    if (theWidth && theHeight)
      _w.resizeTo(theWidth, theHeight);
  }
}

/**
 * Checks the window name of the current window and opens a new popup window on demand.
 * 
 * @param theName the popup name
 * @param theHTMLText the html text for the popup hint. If null, this forces the popup.
 */
function openDocumentInPopup(theName, theHTMLText)
{
  var _window;
  if (window.top != null)
  {
    // call from top navigation
    _window = window.top;
  }
  else
    _window = window;
  
  // now check the window name
  if ((_window.name != theName) || !theHTMLText)
  {
    // open new window
    openWindow(theName, window.location.href, null, null, true, false, true, true);
    
    if (theHTMLText)
    {
      // remove the body elements and display the specified text
      jQuery("body").empty();
      
      if (theHTMLText)
        jQuery("body").append(theHTMLText);
    }
  }
}