/*
To use this you will need to ensure that your page has references to this file and Helper.js.
You can then instanciate the class using the required values.
*/

function ShowProgress(progressImage, buttonControl, hasEvents, cancelButtonId)
{
    var me = this;

    // Set up prpoerties
    this.ProgressImage = document.getElementById(progressImage);
    this.ButtonControl = document.getElementById(buttonControl);
    if (cancelButtonId) {
        this.CancelButton = document.getElementById(cancelButtonId);
    }
    else {
        this.CancelButton = null;
    }
    this.HasEvents = hasEvents;
    this.UseAbsolute = true;
    
    this.showProgress = function()
    {
	    // Display the progress image
	    me.showProgressImage();
    }
    
    this.Initialise = function()
    {
        // Set window event if required
        if (this.HasEvents)
            addEvent(window, "beforeunload", this.showProgress);
    }
    
    this.Initialised = this.Initialise();
}

ShowProgress.prototype.showProgressImage = function() {
    // Display progress image first in order to work out margin
    this.ProgressImage.style.display = 'block';

    if (this.UseAbsolute == true) {
        // Display progress image first
        this.ProgressImage.style.zIndex = '999';
        this.ProgressImage.style.position = 'absolute';

        // Absolutely position the image
        var xPos = getAscendingLefts(this.ButtonControl, false);
        var yPos = getAscendingTops(this.ButtonControl, false);

        // Work out offset of the image
        var xMiddlePos = parseInt(this.ButtonControl.offsetWidth / 2);
        var xOffset = xMiddlePos - parseInt(this.ProgressImage.offsetWidth / 2);

        var yMiddlePos = parseInt(this.ButtonControl.offsetHeight / 2);
        var yOffset = yMiddlePos - parseInt(this.ProgressImage.offsetHeight / 2);

        // Set progress image position
        this.ProgressImage.style.top = (yPos + yOffset) + 'px';
        this.ProgressImage.style.left = (xPos + xOffset) + 'px';
    }

    // Hide the button
    this.ButtonControl.style.display = 'none';

    if (this.CancelButton != null) {
        this.CancelButton.style.display = 'none';
    }
    // Cause animated gif to reload and start animation 
    var imgSrc = this.ProgressImage.src;
    setTimeout("document.getElementById('" + this.ProgressImage.id + "').src = '" + imgSrc + "'", 0);
}
