/**
 * Namespace for managing user interface components as they relate to AJAX activity.
 */
createNamespace("AjaxActivity");

/**
 * The element id of the AJAX activity elemnt indicator.
 * Typically this will be an animated gif image on the page.
 * An element with this id is required for this script to work properly.
 */
AjaxActivity.indicatorId = "ajaxActivityIndicator";

/**
 * Gets the AJAX activity indicator element.
 */
AjaxActivity.getIndicator = function() {
  
  if (!AjaxActivity.indicator) {
    AjaxActivity.indicator = null;
    indicator = $(AjaxActivity.indicatorId);
    if (indicator) {
      indicator.style.position = "absolute";
      indicator.style.zIndex = 999999;
      AjaxActivity.indicator = indicator;
    }
  }
  
  return AjaxActivity.indicator;
}

/**
 * Event listener for the mousemove event on the document.
 * Positions the activity indicator next to the cursor.
 * @param {Event} event
 */
AjaxActivity.onMousemove = function(event) {
  event = event || window.event;
  indicator = AjaxActivity.getIndicator();
  if (indicator) {
    indicator.style.top = (Event.pointerY(event) - 10) + "px";
    indicator.style.left = (Event.pointerX(event) + 5) + "px";
  }
}

/**
 * Toggles the visibility of the activity indicator.
 */
AjaxActivity.toggleIndicator = function() {
  indicator = AjaxActivity.getIndicator();
  if (indicator) {
    indicator.toggle();
  }
}

/**
 * Explicitly shows the activity indicator.
 */
AjaxActivity.showIndicator = function() { 
  indicator = AjaxActivity.getIndicator();
  if (indicator) {
    indicator.show();
  }
}

/** 
 * Explicitly hides the activity indicator.
 */
AjaxActivity.hideIndicator = function() { 
  indicator = AjaxActivity.getIndicator();
  if (indicator) {
    indicator.hide();
  }
}

document.observe("mousemove", AjaxActivity.onMousemove);