/*
/*
 * popup_base.js
 * Used: on the main page,. on sign in page, and anywhere else we want to show a "standard" popup.
 * Requires: lib/prototype.js, dom.js, lib/cssQuery-p.js 
 */

Dependencies.declare('popupBase');
Dependencies.dependsOn('prototype');
Dependencies.dependsOn('dom');
Dependencies.dependsOn('popupBase');

if (typeof(UI) == "undefined") { var UI = new Object(); UI.__objectName = 'UI'; }

UI._showElement = function(element) {
  if (!element) return;
  element = $(element);
  element.removeClassName('apopup_hide');
  element.addClassName('apopup_show');
}

UI._hideElement = function(element) {
  if (!element) return;
  element = $(element);
  element.removeClassName('apopup_show');
  element.addClassName('apopup_hide');
}

UI.show_popup = function(popupElement) {
  if ( ! hasClassName(popupElement, "popup")) {
    throw "UI.show_popup must be invoked with an element classed 'popup'.";
  }
  var alpha = $('popupAlpha');
  if (alpha) {  
    UI._showElement(alpha);
	 //SH: BUG#19788 and 19895
	document.body.style.overflow = 'auto';
  }
  UI._showElement(popupElement);
}

// variable: function called when someone attempts to cancel a popup.
UI._closePopupFormEventHandler = null;

// accessors for above:

// set a handler function that will be called when the popup is
// closed or hidden. Handler functions:
//  - take no parameters
//  - return undefined or false if they expect the hide_popup
//    function to close the popup for them
//  - return true if they take responsibility for closing the
//    popup, or render such closing moot - for example, by
//    refreshing the main page.
UI.setHandlerForPopupClose = function(handler) {
  UI._closePopupFormEventHandler = handler;
}

UI.getHandlerForPopupClose = function() {
   return UI._closePopupFormEventHandler;
}

UI.clearHandlerForPopupClose = function() {
  UI._closePopupFormEventHandler = null;
}

// some standard functions which can be used as handlers.

// handler that closes the main page.
UI.handler_refreshMain = function() {
  top.location.href = 'main.htm';
  return true;
}

UI.hide_popup = function(popupElement) {
  // make sure what we have is from prototype, accept string or element.
  popupElement = $(popupElement);

  // focus any form cursor to the window.
  // This keeps IE from keeping a blinking cursor on background document
  // after a user closes the popup iframe form. See bug #20502.
  popupElement.focus();

  // if the user added a custom event to be triggered, do that.
  var handler = UI.getHandlerForPopupClose();
  if (handler != null) {
    // clear hander, THEN invoke it. This means that the
    // handler is free to reinvoke other methods.
    UI.clearHandlerForPopupClose();

    if (handler()) {
       // handler returned true, so don't close the form
       // or do any of the cleanup associated with form closing.
       // Assume the handler is taking care of that.
      return;
    }
  }

  // if we're closing the current popup form, clear the current
  // form variable
  if (popupElement.id == UI._currentPopupFormId) {
    UI._currentPopupFormId = "";
  }

  UI._hideElement(popupElement);

  var alpha = $('popupAlpha');
  if (alpha) {
    UI._hideElement(alpha);
    //SH: BUG#19788 and 19895
    document.body.style.overflow = 'auto';
  }
  UI.showSelects(); // no param means "all of 'em"
}

// variable used to identify the currently popped up form, if any.
UI._currentPopupFormId = "";

UI.closePopupFormContaining = function (element) {

  // close popup form
  var popupElement = classedElementContaining(element, 'popup');
  UI.hide_popup(popupElement);
  UI.restoreOriginalIframeSrc(popupElement);
}

// show a popup form, and store its name to make it easier to
// hide later.
// popupElement is an optional parameter, to cut down on
// repeated calls to $()
UI.show_popup_form = function(popupName, popupElement) {

  if (popupElement == null){
	  popupElement = $(popupName);
  }

  UI._currentPopupFormId = popupName;
  UI.show_popup(popupElement);
}

UI.hide_current_popup_form = function() {
  if (UI._currentPopupFormId != "") {
    var element = $(UI._currentPopupFormId);
    UI.hide_popup(element);
    UI.restoreOriginalIframeSrc(element);
  }
}

UI.storeOriginalIframeSrc = function(popupElement, src) {
  popupElement.originalIframeSrc = src;
}

UI.restoreOriginalIframeSrc = function(popupElement) {
  var iframe = cssQuery(".popupForm-iframe", popupElement)[0];
  if (iframe && popupElement.originalIframeSrc != null) {
    iframe.src = popupElement.originalIframeSrc;
  }
}

// TODO: consolidate popup state and functions into an object!
UI.getIframeForPopupForm = function(popupElement) {
  return cssQuery(".popupForm-iframe", popupElement)[0];
}

UI.populate_form_popup = function(popupElement, iframeUrl, headingText) {
  if (iframeUrl) {
    var iframe = cssQuery(".popupForm-iframe", popupElement)[0];
    if (iframe) iframe.src = iframeUrl;
  }
  if (headingText) {
    var heading = cssQuery(".popup-heading", popupElement)[0];
    heading.innerHTML = headingText;
  }
}

UI.set_popup_href = function(popupId, newHref) {
  UI.populate_form_popup($(popupId), newHref);
}

UI.set_current_popup_href = function(newHref) {
   if (UI._currentPopupFormId != "") {
      UI.set_popup_href(UI._currentPopupFormId, newHref);
   }
}

