﻿//DRI namespace
//alert('setting up DRI.UI');
var DRI = 
{
    UI:
    {
        General:
        {
            IE6Hacks:
            {
                //Places a "shim" to cover dropdown controls under the target element. Only needed in IE6.
                PutShimUnder: function(targetEl)
                {
                    if(!targetEl._hasShim)
                    {
                        var Shim = document.createElement("iframe");
                        Shim.style.height = targetEl.getBounds().height + 2;
                        Shim.style.width = targetEl.getBounds().width + 2;
                        Sys.UI.DomElement.addCssClass(Shim, "DropDownShim");
                        targetEl.get_element().appendChild(Shim);
                        targetEl._hasShim = true;
                    }
                }
            },
            Ajax:
            {
                UpdatePanelErrorHandler: function(sender, e)
                {
                    //Catches and handles any errors that get returned from server to 
                    //client during an asynchpostback
                    
                    if(e.get_error() != undefined)
                    {
                        //handle client err msg
                        //var ClientMessage = "";
                        //args.get_response().get_statusCode()
                        //args.get_error().message
                        //alert(ClientMessage);
                        
                        //Kills default ajax err popup
                        e.set_errorHandled(true);
                    }
                },
                ExecuteJavascriptInsideUpdatePanelAfterAsynchPostback: function(sender, e)
                {
                    //Finds and executes all javascript code that gets emited by any UpdatePanels
                    //after a postback. By default, this does not happen. This restores the familiar
                    //behavior of javascript getting executed after a postback.
                    
                    var UpdatePanels = sender._panelsToRefreshIDs;
                    
                    if(UpdatePanels == null)
                        return;
                    
                    for(var p = 0; p < UpdatePanels.length; p++)
                    {
                        var ThisPanel = $get(UpdatePanels[p].replace(/\$/g,"_"));   
                        
                        if(ThisPanel == null)
                            continue; //skip iteration
                                                
                        var ThisPanelScriptTags = ThisPanel.getElementsByTagName("script");
                        
                        //Execute the javascript inside the script tags
                        for(var s = 0; s < ThisPanelScriptTags.length; s++)
                        {
                            try
                            {
                                eval(ThisPanelScriptTags[s].innerHTML);
                            }
                            catch(err) {}
                        }
                    }
                },
                CancelPostBack: function()
                {
                    Sys.WebForms.PageRequestManager.getInstance().abortPostBack();
                },
                ShowProgress: function(sender, e)
                {
                    $get("ProgressModal").style.display = "block";
                },
                HideProgress: function(sender, e)
                {
                    $get("ProgressModal").style.display = "none";
                }
            }
        },
        LRU: function()
        {
            this.list = [];
            
            this._BubbleToTop = function(item)
            {
                for(var i = 0; i < this.list.length; i++)
                {
                    if(this.list[i] == item)
                    {
                        //bubble to the end of the array
                        for(var b = i; b < this.list.length - 1; b++)
                        {
                            //swap with the item above
                            var temp = this.list[b + 1];
                            this.list[b + 1] = this.list[b];
                            this.list[b] = temp;
                        }
                        
                        return true;
                    }
                }
                return false;
            };
            
            this.Use = function(usedItem)
            {
                var ItemExists = this._BubbleToTop(usedItem);
                
                //puts at the top of the list, length-1 index contains the most recently used item 
                if(!ItemExists)                  
                    this.list.push(usedItem);
                    
                return this;
            };
            
            this.GetSize = function()
            {
                return this.list.length;
            };
            
            this.LRU = function()
            {
                return this.LeastRecentlyUsed();
            }
            this.LeastRecentlyUsed = function()
            {
                if(this.list.length == 0)
                    return null;
                else
                {
                    //index 0 contains the least recently used item
                    return this.list[0];
                }
            };            
        },
        ClientControls: new Object() //DRI.UI.ClientControls provides a platform for all custom js objects
    }
};
function RegisterAjaxProgressEvents()
{
    
}