/**
 * @file	app_status.js		JS support for SEWM application status bars.
 * @author	Wes Garland
 * @date	Sept 2008
 * @version	$Id:$
 *
 * This code implements a static object, appStatus, whose methods are used to manipulate
 * the application status bars.
 *
 * If the HTML component of the application status bar does not exist, it will fall back
 * to the legacy requestStatus element.
 *
 * The HTML component is shown/hidden with CSS visibility and always takes up the same 
 * amount of screen real estate, whether it is hidden or not.
 */

var appStatus = 
{
  textElement: null,
  textContainer: null,

  requestStatusDiv: null,
  recentRequestStatus: null,

  /** Initialize method. Not required for normal operation, but can be called during
   *  page load if we think we might need to support messages from legacy (requestStatus) code.
   */
  init: function appStatus_init()
  {
    var defaultTextElement;

    if (!appStatus.textElement)
    {
      appStatus.textElement = document.getElementById('appStatusText');
      appStatus.container = document.getElementById('appStatusContainer');

      if (!appStatus.textElement)
      {
	appStatus.textElement = document.getElementById('requestText');	/* fall-back to old behaviour */
	appStatus.container = appStatus.textElement;
      }
      else
      {
	appStatus.requestStatusDiv = document.getElementById('requestText');	/* enable stealing old code's text */
	if (appStatus.requestStatusDiv)
  	  appStatus.recentRequestStatus = appStatus.requestStatusDiv.innerHTML;
      }
    }

    defaultTextElement = document.getElementById('appStatusDefaultText');
    if (defaultTextElement && defaultTextElement.innerHTML)
      appStatus.textElement.innerHTML = defaultTextElement.innerHTML;

    if (appStatus.container)
      appStatus.container.style.visibility = "hidden";
  },

  /** Reveal the status bar with whatever the most recent text was. */
  show: function appStatus_show()
  {
    var rdHTML;
    appStatus.init();

    if (appStatus.requestStatusDiv)
    {
      rdHTML = appStatus.requestStatusDiv.innerHTML;
      if (rdHTML && appStatus.recentRequestStatus != rdHTML)
      {
	/* Some legacy code has updated the request status on us, but we have a new bar. */

	if (rdHTML.length)
	  appStatus.setText(appStatus.requestStatusDiv.innerHTML);

	appStatus.recentRequestStatus = rdHTML;
      }
    }

    if (appStatus.container)
    {
      appStatus.container.style.visibility = 'visible';
      setTimeout("appStatus.container.style.visibility = 'visible';", 1);
    }
  },

  /** Hide the status bar */
  hide: function appStatus_hide()
  {
    appStatus.init();
    if (appStatus.container)
      appStatus.container.style.visibility = 'hidden';
  },

  isVisible: function appStatus_isVisible()
  {
    if (appStatus.container)
      if (appStatus.container.style.visibility == 'visible')
        return true;
    return false;
  },

  /** Set the text without revealing the status bar */
  setText: function appStatus_setText(html)
  {
    appStatus.init();
    appStatus.textElement.innerHTML = html;
    if (appStatus.container)
    {
      if (appStatus.container.style.visibility == 'visible')
        appStatus.container.style.visibility = 'visible';	/* Give FF3 a headshake */
    }
  },

  addText: function appStatus_addText(html)
  {
    appStatus.init();
    appStatus.textElement.innerHTML += html;
  },

  getText: function appStatus_getText(html)
  {
    appStatus.init();
    return appStatus.textElement.innerHTML;
  },

  /** Set the text and reveal the status bar */
  showText: function appStatus_showText(html)
  {
    appStatus.init();
    appStatus.textElement.innerHTML = html;
    if (appStatus.container)
      appStatus.container.style.visibility = 'visible';
  }
}



