/*********************************************************
 *	File:		  spinner.js
 *	Author:		James Brennan
 *	Created:	April 16, 2007
 *
 *	Description:
 *	Javascript class for pop up spinner
 ********************************************************/

var gPopupMask = null;
var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");
var gTabIndexes = new Array();

/*--------------------------------------------------------------
 * Class: Spinner
 *
 * Javascript class for creating div pop up with "wait for me"
 * text and graphic. Used on pages that have long wait times.
 *------------------------------------------------------------*/

var Spinner = Class.create();
Spinner.prototype = {

/*--------------------------------------------------------------
   * Method: initialize
   *
   * Creates new spinner object and sets needed properties.
   *
   * Parameters:
   * id                   string        id for container DIV
   * message              string        Text to be displayed to user
   * brand                string(enum)  Brand name.
   *
   * Return:
   * None
   *------------------------------------------------------------*/

    initialize: function(id, message, progressBarPath, logoPath, logoString)
    {
    
        this.id = id;
        this.message = message; 
        this.progressBarPath = progressBarPath;
        this.logoPath = logoPath;
        this.logoString = logoString;

        //GB this._preLoad();
        this._pane = document.createElement("div");
        
        // added for Mask
        var theBody = document.body;
        popmask = document.createElement('div');
	      popmask.id = 'spinnerMask';
	      theBody.appendChild(popmask);
	      gPopupMask = document.getElementById("spinnerMask");
    },
    
/*--------------------------------------------------------------   
   * Method: show
   *
   * Constructs message div. If a DOM element with given ID already
   * exists it is removed before new DIV appended as child element 
   * of BODY.
   *
   * Parameters:
   * None
   *
   * Return:
   * None
   *------------------------------------------------------------*/  
    
    show: function()
    {
    
        this._pane.id = this.id;
        this._pane.innerHTML = this.logoString + '<br />' + this.message  +  '<br />';
        this._pane.className = 'progressPage';

        if($(this.id))
        {
            document.body.removeChild($(this.id)); 
        }
        document.body.appendChild(this._pane);
        
	      divElement = document.createElement('div');
	      divElement.className = 'progressBar';

        centerPopWinSpinner(this.id);
        disableTabIndexes();
	      gPopupMask.style.display = "block";
	    	// for IE
	      if (gHideSelects == true) {
      		hideSelectBoxes();
	      }

	      this._pane.appendChild(divElement);

        // animate the progress bar by changing background position
	      progressBarPath = this.progressBarPath; // create global var
	      offset = 0; // initialize offset
	      animateProgressBar();

    },
    
/*--------------------------------------------------------------
   * Method: _preLoad
   *
   * Preloads image files.
   *
   * Parameters:
   * None
   *
   * Return:
   * None    
   *------------------------------------------------------------*/    
    
    _preLoad: function() 
    {

        var preProgressBar = new Image();
        preProgressBar.src = this.progressBarPath;
        
        var preLogo = new Image();
        preLogo.src = this.logoPath;
        
    }
}


/**
 * Spinner needs its own unique centering
 *
 */
function centerPopWinSpinner() {
  // need to hard code ID for spinner
  var id = 'SpinnerPopUp';
  // This should only occur if the SpinnerPopUp is present
  if ($(id)) {
    // get dimensions of container div
    var dimensions = $(id).getDimensions();
    var width = dimensions.width;
    var height = dimensions.height;
    if (width == null || isNaN(width)) {
      width = $(id).offsetWidth;
    }
    if (height == null) {
      height = $(id).offsetHeight;
    }

    // get current scroll position
	  var theBody = document.documentElement;
	  //theBody.style.overflow = "hidden"; // included to disable scroll bars; causing page to scroll to top in FF
	  //var scTop = parseInt(theBody.scrollTop,10);
	  var scTop = parseInt(getScrollTop(),10);
	  var scLeft = parseInt(theBody.scrollLeft,10);
  		
	  gPopupMask.style.top = 0;
	  gPopupMask.style.left = 0;

	  setMaskSize();

    // get viewport dims		
	  var fullHeight = getViewportHeight();
	  var fullWidth = getViewportWidth();
  	
	  // calculate centered position
    var posTop = (scTop + ((fullHeight - height) / 2)) + "px";
    var posLeft = (scLeft + ((fullWidth - width) / 2)) + "px";
    
    // set position for display of container div
    $(id).style.top = posTop;
    $(id).style.left = posLeft;
  }
}

function setMaskSize() {
	var theBody = document.documentElement;
			
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();
	
	// Determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > theBody.scrollHeight) {
		popHeight = fullHeight;
	} else {
		popHeight = theBody.scrollHeight;
	}
	
	gPopupMask.style.height = popHeight + "px";
	gPopupMask.style.width = theBody.scrollWidth + "px";
}

/*--------------------------------------------------------------   
  * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
  * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
  * Gets the full width/height because it's different for most browsers.
  *------------------------------------------------------------*/  
 
function getViewportHeight() {
  if (window.innerHeight!=window.undefined) return window.innerHeight;
  if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
  if (document.body) return document.body.clientHeight; 
  return window.undefined; 
}
        
function getViewportWidth() {
  if (window.innerWidth!=window.undefined) return window.innerWidth; 
  if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
  if (document.body) return document.body.clientWidth; 
  return window.undefined;     
}

// For IE.  Go through predefined tags and disable tabbing into them.
function disableTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				gTabIndexes[i] = tagElements[k].tabIndex;
				tagElements[k].tabIndex="-1";
				i++;
			}
		}
	}
}

/**
* Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*
* Thanks for the code Scott!
*/
function hideSelectBoxes() {
	for(var i = 0; i < document.forms.length; i++) {
		for(var e = 0; e < document.forms[i].length; e++){
			if(document.forms[i].elements[e].tagName == "SELECT") {
				document.forms[i].elements[e].style.visibility="hidden";
			}
		}
	}
}

/**
* Makes all drop down form select boxes on the screen visible so they do not reappear after the dialog is closed.
* IE has a problem with wanting select form tags to always be the topmost z-index or layer
*/
function displaySelectBoxes() {
	for(var i = 0; i < document.forms.length; i++) {
		for(var e = 0; e < document.forms[i].length; e++){
			if(document.forms[i].elements[e].tagName == "SELECT") {
			document.forms[i].elements[e].style.visibility="visible";
			}
		}
	}
}

/**
 * Gets the real scroll top
 * (Fix for Safari)
 */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}

/*--------------------------------------------------------
 *  Function:  animateProgressBar
 *  
 *  Description:
 *  Creates an animated, graphical progress bar by calling
 *  itself repeatedly using setTimeout and decrementing the
 *  offset for background position.
 *
 *  Usage:
 *  animateProgressBar();
 *  
 *  Parameters:
 *  none
 *  
 *  Return:
 *  none
 *-------------------------------------------------------*/
function animateProgressBar()
{
  divElement.style.background = "url('" + progressBarPath + "') 0px " + offset + "px no-repeat";
  offset = offset - 10;
  if (offset < -90)
  {
    // reset
    offset = 0;
  }
  id = window.setTimeout("animateProgressBar();",1000);
}

// add event handler for re-centering upon browser window resize
//addEvent(window, "resize", centerPopWinSpinner);
Event.observe(window, 'resize', centerPopWinSpinner, false);

var gHideSelects = false;

var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
	gHideSelects = true;
}
