// A boolean value to prevent the registering of multiple begin / end request events
var eventsRegistered = false;
// Array of items to have loading effect applied too
var UpdateLoadEffectIDs = new Array();
// Array of items for location flash updates
var LocationUpdateIDs = new Array();
// Holding array so store UpdateLoadEffectIDs array
var holdingArray = new Array();

function pageLoad(sender, args){
    if (!eventsRegistered){ 
        // register our events
        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);
        eventsRegistered = true;
    }
} 

function beginRequest(sender, args){
    // Save the Array of IDs to an holding array for restoration after the postback has completed
    holdingArray = UpdateLoadEffectIDs.slice();
    
   //alert(args.get_postBackElement().id);
    
    // Get the postback initiater
    var initID = args.get_postBackElement().id;
    
    //alert(initID);
    
    // Set the default rows selcted value
    var ArrayRowSelected = -1;
    
    // This checks if the initiater of the async postback is in UpdateLoadEffectIDs array, we
    // only post back via the panel for pagination change, hence only apply load effect to that panel
    for(var i=0; i<UpdateLoadEffectIDs.length; i++){
        if (UpdateLoadEffectIDs[i][2] == initID){
            ArrayRowSelected = [i];
        }
    }
    
    // If we are only applut loading effect to 1 panel filer the rest from UpdateLoadEffectIDs array
    if (ArrayRowSelected >= 0){
        var tempArray = new Array();
        tempArray[0] = UpdateLoadEffectIDs[ArrayRowSelected].slice();;
        UpdateLoadEffectIDs.length = 0;
        UpdateLoadEffectIDs[0] = tempArray[0].slice();
        tempArray = null;
    }
    
    // Display the update panels & loader images contained in the array
    for(var i=0; i<UpdateLoadEffectIDs.length; i++){
        $get(UpdateLoadEffectIDs[i][0]).style.display = '';
        $get(UpdateLoadEffectIDs[i][1]).style.display = '';
    } 
 
    // Position the load effect panels
    for(var i=0; i<UpdateLoadEffectIDs.length; i++){
        // Get eh panels to apply effects too
        var _updateProgressDiv = $get(UpdateLoadEffectIDs[i][0]);     
        var _backgroundDiv = $get(UpdateLoadEffectIDs[i][1]);
        var _searchPanel = $get(UpdateLoadEffectIDs[i][2]);
       
        // get the bounds of both the updatepanel and the progress div
        var UpdatePanelBounds = Sys.UI.DomElement.getBounds(_searchPanel);
        var updateProgressDivBounds = Sys.UI.DomElement.getBounds(_updateProgressDiv);
                
        //  set the dimensions of the background div to the same as the updatepanel
        _backgroundDiv.style.width = UpdatePanelBounds.width + 'px';
        _backgroundDiv.style.height = UpdatePanelBounds.height + 'px';

        //  center of updatepanel
        var x = UpdatePanelBounds.x + Math.round(UpdatePanelBounds.width / 2) - Math.round(updateProgressDivBounds.width / 2);
        var y = UpdatePanelBounds.y + Math.round(UpdatePanelBounds.height / 2) - Math.round(updateProgressDivBounds.height / 2); 

        //  set the progress element to this position
        Sys.UI.DomElement.setLocation(_updateProgressDiv, x, y);
 
        //  place the div over the updatepanel
        Sys.UI.DomElement.setLocation(_backgroundDiv, UpdatePanelBounds.x, UpdatePanelBounds.y); 

        // Get the Bounds & x value of the Progress panel again now the overlay has been set and apply x location
        updateProgressDivBounds = Sys.UI.DomElement.getBounds(_updateProgressDiv);
        x = UpdatePanelBounds.x + Math.round(UpdatePanelBounds.width / 2) - Math.round(updateProgressDivBounds.width / 2);
        Sys.UI.DomElement.setLocation(_updateProgressDiv, x, y);
    }
}

// Turn of the loading panels after postback has finished
function endRequest(sender, args) {
    for(var i=0; i<UpdateLoadEffectIDs.length; i++){
        _updateProgressDiv = $get(UpdateLoadEffectIDs[i][0]);
        _backgroundDiv = $get(UpdateLoadEffectIDs[i][1]);
        _updateProgressDiv.style.display = 'none';
        _backgroundDiv.style.display = 'none';
    } 
    // restore the ID's array from the holding array and empty the holding array   
    UpdateLoadEffectIDs = holdingArray.slice();
    holdingArray.length = 0;
    
    if (args.get_error() != undefined) { $get('Error').style.visibility = "visible";
        args.set_errorHandled(true); 
    }
}

// code to find flash movie
function getFlashObj(movie) {
    if (window.document[movie]) {
        return window.document[movie];
    }
    if (navigator.appName.indexOf("Microsoft Internet") == -1) {
        if (document.embeds && document.embeds[movie]) {
            return document.embeds[movie];
        }
    } else {
        return document.getElementById(movie);
    }
}

function ReWriteLinks(ContainerEl, hfToUpdate, ctrlToPost)
{
    var PagContainer = document.getElementById(ContainerEl);
    try
    {
        var Links = PagContainer.getElementsByTagName('a');
        var unknown; 
        
        for(var i=0;i<Links.length;i++){
            if (Links[i].getAttribute('href') != null && Links[i].getAttribute('href') != "") {
                    Links[i].setAttribute('href', '#aj');
                    Links[i].onclick=function() { 
                    AjaxPageChanged(this.name, hfToUpdate, ctrlToPost);
                }
            }
        } 
    }
    catch(err)
    {
        //alert(err + " - This is casued by the link re-writer, there are no links to find in the paramater element");
    }  
    // Add opacity to load effect overlays dynamically (otherwise CSS will be invalid)
    for(var i=0; i<UpdateLoadEffectIDs.length; i++){
        if (document.getElementById(UpdateLoadEffectIDs[i][1])){
		    var oe = document.getElementById(UpdateLoadEffectIDs[i][1]);
            oe.setAttribute("style", "opacity:0.75; display:none;")
		if (oe.style.setAttribute) //IE
		    oe.style.setAttribute("filter", "alpha(opacity=75);")
	    }
    }
}

function AjaxPageChanged(command, hfToUpdate, pnlToPost)
{
   var obj = document.getElementById(hfToUpdate);
   obj.value = command;
   __doPostBack(pnlToPost,"");
}
