﻿// CustomPage.Tools.js

Type.registerNamespace('CustomPage.Tools');
CustomPage.Tools.ImageDisplayer = function CustomPage$Tools$ImageDisplayer(associatedElement) {
    CustomPage.Tools.ImageDisplayer.initializeBase(this, [associatedElement]);
    
    this._elements;
    this._imgSources;
    this._sizesH;
    this._sizesW;    
    this._currentId;
    this._onMouseOver$delegate = Function.createDelegate(this, this._onMouseOverHandler);
}

CustomPage.Tools.ImageDisplayer.prototype = {

    get_elements : function this$get_elements() {
        return this._elements;
    },

    set_elements : function this$set_elements(value) {
        this._elements = value;
    },

    get_imgSources : function this$get_imgSources() {
        return this._imgSources;
    },

    set_imgSources : function this$set_imgSources(value) {
        this._imgSources = value;
    },
    
    get_sizesH : function this$get_sizesH() {
        return this._sizesH;
    },

    set_sizesH : function this$set_sizesH(value) {
        this._sizesH = value;
    },
    
    get_sizesW : function this$get_sizesH() {
        return this._sizesW;
    },

    set_sizesW : function this$set_sizesH(value) {
        this._sizesW = value;
    },
    
    initialize : function this$initialize() {
        CustomPage.Tools.ImageDisplayer.callBaseMethod(this, "initialize");
        
        var elt = this.get_element();
        
        for(var i = 0; i < this._imgSources.length; i++) {
            if (this._imgSources[i].length > 0) {
                this._setupElm(i);
                break;
            }
        }
        if (elt.src.length == 0)
            elt.style.display = 'none';
        else {
            for(var i = 0; i < this._elements.length; i++) {
                if (this._imgSources[i].length > 0)
                    $addHandler($get(this._elements[i]), 'mouseover', this._onMouseOver$delegate);
            }
            elt.style.marginLeft = "3px";
        }
    },
    
    dispose : function this$dispose() {
        CustomPage.Tools.ImageDisplayer.callBaseMethod(this, "dispose");
        
        for(var i = 0; i < this._elements.length; i++) {
            if (this._imgSources[i].length > 0) {
                var item = $get(this._elements[i]);
                if (item != null)
                    $removeHandler(item, 'mouseover', this._onMouseOver$delegate);
            }
        }
    },
    
    _onMouseOverHandler : function(e) {
        var targetId = e.target.id;
        if (targetId == this._currentId) return;
        this._currentId = targetId;
        for (var i = 0; i < this._elements.length; i ++) {
            if (this._elements[i] == targetId) {
                if (this._imgSources[i].length > 0) {
                    this._setupElm(i);
                }
                break;
            }
        }
    },
    
    _resizeW : function(w) {
        return w > 120 ? 120 : w;
    },
    
    _resizeH : function(w, h) {
        if (w != 120)
            return (120 * h / w);
        return h;
    },
    
    _setupElm : function(i) {
        var elt = this.get_element();
        elt.src = this._imgSources[i];
        elt.width = "120";
        elt.height = this._resizeH(this._sizesW[i], this._sizesH[i]);
        var a = $get(this._elements[i]);
        var p = elt.parentNode;
        p.title = a.innerText;
        p.href = a.href;
    }
}

CustomPage.Tools.ImageDisplayer.registerClass("CustomPage.Tools.ImageDisplayer", Sys.UI.Control);








CustomPage.Tools.SettingsDialog = function CustomPage$Tools$SettingsDialog(associatedElement) {
    CustomPage.Tools.SettingsDialog.initializeBase(this, [associatedElement]);

    this._okHandler;
    this._popup;
    this._tbMaxRows = null;
    this._selRefreshInterval = null;
    this._okBtn;
    this._cancelBtn;
    this._popupControlID;
    
    this._ownerItem;
    this._itemInfo;
    
    this._onKeyDown$delegate;
}

CustomPage.Tools.SettingsDialog.prototype = {
    
    get_ownerItem : function() {
        return this._ownerItem;
    },
    
    set_ownerItem : function (value) {
        this._ownerItem = value;
        this._itemInfo = this._ownerItem.get_pageItemInfo();
    },  
    
    initialize : function() {

        CustomPage.Tools.SettingsDialog.callBaseMethod(this, 'initialize');

        var elt = this.get_element();
        
        var container = document.createElement("div");
        container.id = elt.id + "SettingsDialog";
        container.className = "settings_dialog";
        this._popupControlID = container.id;
        
        var header = document.createElement("div");
        header.style.width = "100%";
        header.innerHTML = "Opzioni";
        header.className = "settings_header";
        container.appendChild(header);
        
        
        var body = document.createElement("div");
        body.className = "settings_body";
        container.appendChild(body);
        
        var table = document.createElement('table');
        table.cellSpacing = table.cellPadding = table.border = 0;
        table.style.width = "100%";
        body.appendChild(table);
        
        this._onKeyDown$delegate = Function.createDelegate(this, this._onKeyDown);
        
        if (this._itemInfo.PageItemType == "List") {
            this._tbMaxRows = document.createElement("input");
            this._tbMaxRows.id = elt.id + "maxrows";
            this._tbMaxRows.type = "text";
            this._tbMaxRows.style.width = "20px";
            this._tbMaxRows.style.height = "12px";
            this._tbMaxRows.style.marginLeft = "5px";
            this._tbMaxRows.value = this._itemInfo.MaximumRows;
            $addHandler(this._tbMaxRows, 'keydown', this._onKeyDown$delegate);
        
            var label = document.createElement("span");
            label.innerHTML = "Titoli in elenco (max. 50)";
        
            var row = table.insertRow(table.rows.length);
            var cell1 = row.insertCell(row.cells.length);
            cell1.appendChild(label);
            
            var cell2 = row.insertCell(row.cells.length);
            cell2.align = "right"; cell2.vAlign = "top"; 
            cell2.appendChild(this._tbMaxRows);
        }
        
        if (this._itemInfo.ContentType == "Dynamic") {
            var sp = table.insertRow(table.rows.length);
            sp.insertCell(sp.cells.length).style.height = "5px";
            
            this._selRefreshInterval = document.createElement("select");
            this._selRefreshInterval.id = elt.id + "refreshInt";
            this._selRefreshInterval.options[this._selRefreshInterval.options.length] = new Option("--", 0);
            this._selRefreshInterval.options[this._selRefreshInterval.options.length] = new Option("10", 10);
            this._selRefreshInterval.options[this._selRefreshInterval.options.length] = new Option("20", 20);
            this._selRefreshInterval.options[this._selRefreshInterval.options.length] = new Option("30", 30);
            this._selRefreshInterval.options[this._selRefreshInterval.options.length] = new Option("60", 60);
            this._selRefreshInterval.value = this._itemInfo.RefreshInterval;
            this._selRefreshInterval.style.marginLeft = "5px";
            $addHandler(this._selRefreshInterval, 'keydown', this._onKeyDown$delegate);
            
            var label = document.createElement("span");
            label.innerHTML = "Freq. aggiornamento (min.)";
        
            var row = table.insertRow(table.rows.length);
            var cell1 = row.insertCell(row.cells.length);
            cell1.appendChild(label);
            
            var cell2 = row.insertCell(row.cells.length);
            cell2.align = "right"; cell2.vAlign = "top"; 
            cell2.appendChild(this._selRefreshInterval);
        }
        
        this._okBtn = document.createElement("input");
        this._okBtn.id = elt.id + "this._okBtn";
        this._okBtn.className = "s_button50";
        this._okBtn.type ="button";
        this._okBtn.value = "OK";
        this._okBtn.style.marginRight = "2px";
        this._okBtn.style.width = "50px";
        
        this._cancelBtn = document.createElement("input");
        this._cancelBtn.id = elt.id + "this._cancelBtn";
        this._cancelBtn.className = "s_button50";
        this._cancelBtn.type ="button";
        this._cancelBtn.value = "Annulla";
        this._cancelBtn.style.marginLeft = "2px";
        this._cancelBtn.style.width = "50px";
        
        var footer = document.createElement("div");
        footer.className = "settings_footer";
        footer.appendChild(this._okBtn);
        footer.appendChild(this._cancelBtn);
        container.appendChild(footer);
        
        document.body.appendChild(container);
        
        this._okHandler = Function.createDelegate(this, this._raiseConfirmed);
    },
    
    dispose : function() {
        if (this._popup && this._okHandler) {
            this._popup.remove_confirmed(this._okHandler);
            this._okHandler = this._popup = null;
        }
        
        if (this._tbMaxRows != null) 
            $removeHandler(this._tbMaxRows, 'keydown', this._onKeyDown$delegate);
        
        if (this._selRefreshInterval != null) 
            $removeHandler(this._selRefreshInterval, 'keydown', this._onKeyDown$delegate);
        
        CustomPage.Tools.SettingsDialog.callBaseMethod(this, 'dispose');
    },
    
    _raiseConfirmed : function() {        
        if (this._tbMaxRows != null) {
            try {
                var maxRows = Number.parse(this._tbMaxRows.value);
                this._itemInfo.MaximumRows = Math.min(50, maxRows);
                this._itemInfo.MaximumRows = Math.max(1, this._itemInfo.MaximumRows);
            }
            catch (e) {
                var i = 0;
            }
        }
        if (this._selRefreshInterval != null) {
            this._itemInfo.RefreshInterval = this._selRefreshInterval.value;
        }
        
        this._ownerItem.populate();
        this._ownerItem.saveProfile();
        this._ownerItem.setupAutoRefresh(this._itemInfo.RefreshInterval);
    },
    
    _onKeyDown : function(e) {
        if(e.which || e.keyCode){
            if ((e.which == 13) || (e.keyCode == 13)) {
                this._popup._onOk();
            }
            else if ((e.which == 27) || (e.keyCode == 27)) {
                this._popup._onCancel();
            }
        }
    },
    
    show : function() {
        if (this._tbMaxRows != null) {
            this._tbMaxRows.value = this._itemInfo.MaximumRows;
        }
        if (this._popup != null) {
            this._popup.dispose();
            this._popup = null;    
        }
        var elt = this.get_element();
        var loc = Sys.UI.DomElement.getLocation(elt);
        var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
        var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

        this._popup = $create(CustomPage.Tools.ModalPopupBehavior, {PopupControlID: this._popupControlID, DropShadow: true, DropShadowWidth: 2, DropShadowOpacity:0.75, OkControlID: this._okBtn.id, CancelControlID: this._cancelBtn.id, X: loc.x - scrollLeft - 200, Y: loc.y - scrollTop}, {confirmed: this._okHandler}, null, elt);

        this._popup.show();
        
        if (this._tbMaxRows != null)
            this._tbMaxRows.focus();
        else if (this._selRefreshInterval != null)
            this._selRefreshInterval.focus();
    }
}

CustomPage.Tools.SettingsDialog.registerClass("CustomPage.Tools.SettingsDialog", Sys.UI.Control);








CustomPage.Tools.ModalPopupBehavior = function(element) {
    /// <summary>
    /// The ModalPopupBehavior is used to display the target element as a modal dialog
    /// </summary>
    /// <param name="element" type="Sys.UI.DomElement" domElement="true">
    /// DOM Element the behavior is associated with
    /// </param>
    CustomPage.Tools.ModalPopupBehavior.initializeBase(this, [element]);
    
    // Properties
    this._PopupControlID = null;
    this._PopupDragHandleControlID = null;
    this._BackgroundCssClass = null;
    this._DropShadow = false;
    this._Drag = false;    
    this._OkControlID = null;
    this._CancelControlID = null;
    this._OnOkScript = null;
    this._OnCancelScript = null;
    this._xCoordinate = -1;
    this._yCoordinate = -1;
    this._DropShadowOpacity = 1.0;
    this._DropShadowWidth = 5;
    
    // Variables
    this._backgroundElement = null;
    this._foregroundElement = null;
    this._popupElement = null;
    this._dragHandleElement = null;
    this._showHandler = null;
    this._okHandler = null;
    this._cancelHandler = null;
    this._scrollHandler = null;
    this._resizeHandler = null;
    this._windowHandlersAttached = false;

    this._dropShadowBehavior = null;
    this._dragBehavior = null;

    this._saveTabIndexes = new Array();
    this._saveDesableSelect = new Array();
    this._tagWithTabIndex = new Array('A','AREA','BUTTON','INPUT','OBJECT','SELECT','TEXTAREA','IFRAME');
    
    this._bodyClickHandler;
}
CustomPage.Tools.ModalPopupBehavior.prototype = {
    initialize : function() {
        /// <summary>
        /// Initialize the behavior
        /// </summary>
        
        /*
            <div superpopup - drag container resizable><div -- drag handle\dropshadow foreground></div></div>
        */
        CustomPage.Tools.ModalPopupBehavior.callBaseMethod(this, 'initialize');
        if(this._PopupDragHandleControlID)
            this._dragHandleElement = $get(this._PopupDragHandleControlID);

        this._popupElement = $get(this._PopupControlID);
        if(this._DropShadow)
        {
            this._foregroundElement = document.createElement('div');
            this._popupElement.parentNode.appendChild(this._foregroundElement);
            this._foregroundElement.appendChild(this._popupElement);
        }
        else
        {
            this._foregroundElement = $get(this._PopupControlID);
        }
        this._backgroundElement = document.createElement('div');
        this._backgroundElement.style.display = 'none';
        this._backgroundElement.style.position = 'absolute';
        this._backgroundElement.style.left = '0px';
        this._backgroundElement.style.top = '0px';
        // Want zIndex to big enough that the background sits above everything else
        // CSS 2.1 defines no bounds for the <integer> type, so pick arbitrarily
        this._backgroundElement.style.zIndex = 10000;
        if (this._BackgroundCssClass) {
            this._backgroundElement.className = this._BackgroundCssClass;
        }
        this._foregroundElement.parentNode.appendChild(this._backgroundElement);

        this._foregroundElement.style.display = 'none';
        this._foregroundElement.style.position = 'absolute';
        this._foregroundElement.style.zIndex = CommonToolkitScripts.getCurrentStyle(this._backgroundElement, 'zIndex', this._backgroundElement.style.zIndex) + 1;
        
        this._showHandler = Function.createDelegate(this, this._onShow);
        $addHandler(this.get_element(), 'click', this._showHandler);

        if (this._OkControlID) {
            this._okHandler = Function.createDelegate(this, this._onOk);
            $addHandler($get(this._OkControlID), 'click', this._okHandler);
        }

        if (this._CancelControlID) {
            this._cancelHandler = Function.createDelegate(this, this._onCancel);
            $addHandler($get(this._CancelControlID), 'click', this._cancelHandler);
        }

        this._scrollHandler = Function.createDelegate(this, this._onLayout);
        this._resizeHandler = Function.createDelegate(this, this._onLayout);

        // Need to know when partial updates complete
        this.registerPartialUpdateEvents();
        
        this._bodyClickHandler = Function.createDelegate(this, this._onBodyClick);
        $addHandler(document.body, 'click', this._bodyClickHandler);
    },

    dispose : function() {
        /// <summary>
        /// Dispose the behavior
        /// </summary>
        this._detachPopup();

        if(this._DropShadow)
        {
            // Remove DIV wrapper added in initialize
            this._foregroundElement.parentNode.appendChild(this._popupElement);
            this._foregroundElement.parentNode.removeChild(this._foregroundElement);
        }

        this._scrollHandler = null;
        this._resizeHandler = null;
        if (this._cancelHandler && $get(this._CancelControlID)) {
            $removeHandler($get(this._CancelControlID), 'click', this._cancelHandler);
            this._cancelHandler = null;
        }
        if (this._okHandler && $get(this._OkControlID)) {
            $removeHandler($get(this._OkControlID), 'click', this._okHandler);
            this._okHandler = null;
        }
        if (this._showHandler) {
            $removeHandler(this.get_element(), 'click', this._showHandler);
            this._showHandler = null;
        }
        
        if (this._bodyClickHandler) {
            $removeHandler(document.body, 'click', this._bodyClickHandler);
            this._bodyClickHandler = null;
        }
        
        CustomPage.Tools.ModalPopupBehavior.callBaseMethod(this, 'dispose');
    },

    _attachPopup : function() {
        /// <summary>
        /// Attach the event handlers for the popup
        /// </summary>

        if (this._DropShadow && !this._dropShadowBehavior) {
            this._dropShadowBehavior = $create(AjaxControlToolkit.DropShadowBehavior, {Opacity: this._DropShadowOpacity, Width: this._DropShadowWidth}, null, null, this._popupElement);
        }
        if (this._dragHandleElement && !this._dragBehavior) {
            this._dragBehavior = $create(AjaxControlToolkit.FloatingBehavior, {"handle" : this._dragHandleElement}, null, null, this._foregroundElement);
        }        
//        $addHandler(window, 'resize', this._resizeHandler);
//        $addHandler(window, 'scroll', this._scrollHandler);
//        this._windowHandlersAttached = true;
    },

    _detachPopup : function() {
        /// <summary>
        /// Detach the event handlers for the popup
        /// </summary>

        if (this._windowHandlersAttached) {
            if (this._scrollHandler) {
                $removeHandler(window, 'scroll', this._scrollHandler);
            }

            if (this._resizeHandler) {
                $removeHandler(window, 'resize', this._resizeHandler);
            }
            this._windowHandlersAttached = false;
        }
        
        if (this._dragBehavior) {
            this._dragBehavior.dispose();
            this._dragBehavior = null;
        }       
        
        if (this._dropShadowBehavior) {
            this._dropShadowBehavior.dispose();
            this._dropShadowBehavior = null;
        }

    },

    _onBodyClick : function(sender, e) {
        var elt = sender.target;
        while (elt != null) {
            if (elt.id == this._popupElement.id) {
                sender.stopPropagation();
                return;
            }
            elt = elt.parentNode;
        }
        this.hide();
    },
    
    _onShow : function(e) {
        /// <summary>
        /// Handler for the target's click event
        /// </summary>
        /// <param name="e" type="Sys.UI.DomEvent">
        /// Event info
        /// </param>

        if (!this.get_element().disabled) {
            this.show();
            e.preventDefault();
            return false;
        }
    },

    _onOk : function(e) {
        /// <summary>
        /// Handler for the modal dialog's OK button click
        /// </summary>
        /// <param name="e" type="Sys.UI.DomEvent">
        /// Event info
        /// </param>

        this.raiseConfirmed();
        var element = $get(this._OkControlID);
        if (element && !element.disabled) {
            this.hide();
            if (e != null)
                e.preventDefault();
            if (this._OnOkScript) {
                window.setTimeout(this._OnOkScript, 0);
            }
            return false;
        }
    },

    _onCancel : function(e) {
        /// <summary>
        /// Handler for the modal dialog's Cancel button click
        /// </summary>
        /// <param name="e" type="Sys.UI.DomEvent">
        /// Event info
        /// </param>

        var element = $get(this._CancelControlID);
        if (element && !element.disabled) {
            this.hide();
            if (e != null)
                e.preventDefault();
            if (this._OnCancelScript) {
                window.setTimeout(this._OnCancelScript, 0);
            }
            return false;
        }
    },

    _onLayout : function() {
        /// <summary>
        /// Handler for scrolling and resizing events that would require a repositioning of the modal dialog
        /// </summary>
        this._layout();
    },

    show : function() {
        /// <summary>
        /// Display the element referenced by PopupControlID as a modal dialog
        /// </summary>
        CustomPage.Tools.ModalPopupBehavior.callBaseMethod(this, 'populate');

        this.raiseShowing();

        this._attachPopup();

        this._backgroundElement.style.display = '';
        this._foregroundElement.style.display = '';
        this._popupElement.style.display = '';

        // Disable TAB
        this.disableTab();

        this._layout();
        // On pages that don't need scrollbars, Firefox and Safari act like
        // one or both are present the first time the layout code runs which
        // obviously leads to display issues - run the layout code a second
        // time to work around this problem
        this._layout();
        
        this.raiseShown();
    },

    disableTab : function() {
        /// <summary>
        /// Change the tab indices so we only tab through the modal popup
        /// (and hide SELECT tags in IE6)
        /// </summary>

        var i = 0;
        var tagElements;
        var tagElementsInPopUp = new Array();
        Array.clear(this._saveTabIndexes);

        //Save all popup's tag in tagElementsInPopUp
        for (var j = 0; j < this._tagWithTabIndex.length; j++) {
            tagElements = this._foregroundElement.getElementsByTagName(this._tagWithTabIndex[j]);
            for (var k = 0 ; k < tagElements.length; k++) {
                tagElementsInPopUp[i] = tagElements[k];
                i++;
            }
        }

        i = 0;
        for (var j = 0; j < this._tagWithTabIndex.length; j++) {
            tagElements = document.getElementsByTagName(this._tagWithTabIndex[j]);
            for (var k = 0 ; k < tagElements.length; k++) {
                if (Array.indexOf(tagElementsInPopUp, tagElements[k]) == -1)  {
                    this._saveTabIndexes[i] = {tag: tagElements[k], index: tagElements[k].tabIndex};
                    tagElements[k].tabIndex="-1";
                    i++;
                }
            }
        }

        //IE6 Bug with SELECT element always showing up on top
        i = 0;
        if ((Sys.Browser.agent === Sys.Browser.InternetExplorer) && (Sys.Browser.version < 7)) {
            //Save SELECT in PopUp
            var tagSelectInPopUp = new Array();
            for (var j = 0; j < this._tagWithTabIndex.length; j++) {
                tagElements = this._foregroundElement.getElementsByTagName('SELECT');
                for (var k = 0 ; k < tagElements.length; k++) {
                    tagSelectInPopUp[i] = tagElements[k];
                    i++;
                }
            }

            i = 0;
            Array.clear(this._saveDesableSelect);
            tagElements = document.getElementsByTagName('SELECT');
            for (var k = 0 ; k < tagElements.length; k++) {
                if (Array.indexOf(tagSelectInPopUp, tagElements[k]) == -1)  {
                    this._saveDesableSelect[i] = {tag: tagElements[k], visib: CommonToolkitScripts.getCurrentStyle(tagElements[k], 'visibility')} ;
                    tagElements[k].style.visibility = 'hidden';
                    i++;
                }
            }
        }
    },

    restoreTab : function() {
        /// <summary>
        /// Restore the tab indices so we tab through the page like normal
        /// (and restore SELECT tags in IE6)
        /// </summary>

        for (var i = 0; i < this._saveTabIndexes.length; i++) {
            this._saveTabIndexes[i].tag.tabIndex = this._saveTabIndexes[i].index;
        }

        //IE6 Bug with SELECT element always showing up on top
        if ((Sys.Browser.agent === Sys.Browser.InternetExplorer) && (Sys.Browser.version < 7)) {
            for (var k = 0 ; k < this._saveDesableSelect.length; k++) {
                this._saveDesableSelect[k].tag.style.visibility = this._saveDesableSelect[k].visib;
            }
        }
    },

    hide : function() {
        /// <summary>
        /// Hide the modal dialog
        /// </summary>

        this.raiseHiding();
        
        this._backgroundElement.style.display = 'none';
        this._foregroundElement.style.display = 'none';
        
        this.restoreTab();

        this._detachPopup();
        
        this.raiseHidden();
    },

    _layout : function() {
        /// <summary>
        /// Position the modal dialog in the center of the screen
        /// </summary>

        var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
        var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
        var clientBounds = CommonToolkitScripts.getClientBounds();
        var clientWidth = clientBounds.width;
        var clientHeight = clientBounds.height;
        this._backgroundElement.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth)+'px';
        this._backgroundElement.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight)+'px';

        var isIE6 = true; //(Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version < 7);
        if(this._xCoordinate < 0)
        {
            this._foregroundElement.style.left = scrollLeft+((clientWidth-this._foregroundElement.offsetWidth)/2)+'px';
        }
        else
        {
            if(isIE6)
            {
                this._foregroundElement.style.position = 'absolute';
                this._foregroundElement.style.left = (this._xCoordinate + scrollLeft) + 'px';
            }
            else
            {
                this._foregroundElement.style.position = 'fixed';
                this._foregroundElement.style.left = this._xCoordinate + 'px';
            }
        }
        if(this._yCoordinate < 0)
        {
            this._foregroundElement.style.top = scrollTop+((clientHeight-this._foregroundElement.offsetHeight)/2)+'px';
        }
        else
        {
            if(isIE6)
            {
                this._foregroundElement.style.position = 'absolute';
                this._foregroundElement.style.top = (this._yCoordinate + scrollTop) + 'px';
            }
            else
            {
                this._foregroundElement.style.position = 'fixed';
                this._foregroundElement.style.top = this._yCoordinate + 'px';
            }
        }
        
        if (this._dropShadowBehavior) {
            this._dropShadowBehavior.setShadow();
            window.setTimeout(Function.createDelegate(this, this._fixupDropShadowBehavior), 0);
        }
    },

    _fixupDropShadowBehavior : function() {
        /// <summary>
        /// Some browsers don't update the location values immediately, so
        /// the location of the drop shadow would always be a step behind
        /// without this method
        /// </summary>

        if (this._dropShadowBehavior) {
            this._dropShadowBehavior.setShadow();
        }
    },

    // Show the popup if asked to
    _partialUpdateEndRequest : function(sender, endRequestEventArgs) {
        CustomPage.Tools.ModalPopupBehavior.callBaseMethod(this, '_partialUpdateEndRequest', [sender, endRequestEventArgs]);

        if (this.get_element()) {
            // Look up result by element's ID
            var action = endRequestEventArgs.get_dataItems()[this.get_element().id];
            if ("show" == action) {
                this.show();
            } else if ("hide" == action) {
                this.hide();
            }
        }

        // Async postback may have added content; re-layout to accomodate it
        this._layout();
    },

    _onPopulated : function(sender, eventArgs) {
        CustomPage.Tools.ModalPopupBehavior.callBaseMethod(this, '_onPopulated', [sender, eventArgs]);

        // Dynamic populate may have added content; re-layout to accomodate it
        this._layout();
    },
    
    raiseShowing : function() {
        /// <summary>
        /// Raise the <code>showing</code> event
        /// </summary>
        /// <returns />
        var handlers = this.get_events().getHandler('showing');
        if (handlers) {
            handlers(this, Sys.EventArgs.Empty);
        }
    },
    
    add_showing : function(handler) {
        /// <summary>
        /// Adds an event handler for the <code>showiwng</code> event.
        /// </summary>
        /// <param name="handler" type="Function">
        /// The handler to add to the event.
        /// </param>
        /// <returns />
        this.get_events().addHandler("showing", handler);
    },
    
    remove_showing : function(handler) {
        /// <summary>
        /// Removes an event handler for the <code>showing</code> event.
        /// </summary>
        /// <param name="handler" type="Function">
        /// The handler to remove from the event.
        /// </param>
        /// <returns />
        this.get_events().removeHandler("showing", handler);
    },
    
    raiseShown : function() {
        /// <summary>
        /// Raise the <code>shown</code> event
        /// </summary>
        /// <returns />
        var handlers = this.get_events().getHandler('shown');
        if (handlers) {
            handlers(this, Sys.EventArgs.Empty);
        }
    },
    
    add_shown : function(handler) {
        /// <summary>
        /// Adds an event handler for the <code>shown</code> event.
        /// </summary>
        /// <param name="handler" type="Function">
        /// The handler to add to the event.
        /// </param>
        /// <returns />
        this.get_events().addHandler("shown", handler);
    },
    
    remove_shown : function(handler) {
        /// <summary>
        /// Removes an event handler for the <code>shown</code> event.
        /// </summary>
        /// <param name="handler" type="Function">
        /// The handler to remove from the event.
        /// </param>
        /// <returns />
        this.get_events().removeHandler("shown", handler);
    },
    
    raiseHiding : function() {
        /// <summary>
        /// Raise the <code>hiding</code> event
        /// </summary>
        /// <returns />
        var handlers = this.get_events().getHandler('hiding');
        if (handlers) {
            handlers(this, Sys.EventArgs.Empty);
        }
    },
    
    add_hiding : function(handler) {
        /// <summary>
        /// Adds an event handler for the <code>hiding</code> event.
        /// </summary>
        /// <param name="handler" type="Function">
        /// The handler to add to the event.
        /// </param>
        /// <returns />
        this.get_events().addHandler("hiding", handler);
    },
    
    remove_hiding : function(handler) {
        /// <summary>
        /// Removes an event handler for the <code>hiding</code> event.
        /// </summary>
        /// <param name="handler" type="Function">
        /// The handler to remove from the event.
        /// </param>
        /// <returns />
        this.get_events().removeHandler("hiding", handler);
    },
    
    raiseHidden : function() {
        /// <summary>
        /// Raise the <code>hidden</code> event
        /// </summary>
        /// <returns />
        var handlers = this.get_events().getHandler('hidden');
        if (handlers) {
            handlers(this, Sys.EventArgs.Empty);
        }
    },
    
    add_hidden : function(handler) {
        /// <summary>
        /// Adds an event handler for the <code>hidden</code> event.
        /// </summary>
        /// <param name="handler" type="Function">
        /// The handler to add to the event.
        /// </param>
        /// <returns />
        this.get_events().addHandler("hidden", handler);
    },
    
    remove_hidden : function(handler) {
        /// <summary>
        /// Removes an event handler for the <code>hidden</code> event.
        /// </summary>
        /// <param name="handler" type="Function">
        /// The handler to remove from the event.
        /// </param>
        /// <returns />
        this.get_events().removeHandler("hidden", handler);
    },

    raiseConfirmed : function() {
        var handlers = this.get_events().getHandler('confirmed');
        if (handlers) {
            handlers(this, Sys.EventArgs.Empty);
        }
    },
    
    add_confirmed : function(handler) {
        this.get_events().addHandler("confirmed", handler);
    },
    
    remove_confirmed : function(handler) {
        this.get_events().removeHandler("confirmed", handler);
    },
    
    get_PopupControlID : function() {
        /// <value type="String">
        /// The ID of the element to display as a modal popup
        /// </value>
        return this._PopupControlID;
    },
    set_PopupControlID : function(value) {
        if (this._PopupControlID != value) {
            this._PopupControlID = value;
            this.raisePropertyChanged('PopupControlID');
        }
    },

    get_X: function() {
        /// <value type="Number" integer="true">
        /// The number of pixels from the left of the browser to position the modal popup.
        /// </value>
        return this._xCoordinate;
    },
    set_X: function(value) {
        if (this._xCoordinate != value) {
            this._xCoordinate = value;
            this.raisePropertyChanged('X');
        }
    },

    get_Y: function() {
        /// <value type="Number" integer="true">
        /// The number of pixels from the top of the browser to position the modal popup.
        /// </value>
        return this._yCoordinate;
    },
    set_Y: function(value) {
        if (this._yCoordinate != value) {
            this._yCoordinate = value;
            this.raisePropertyChanged('Y');
        }
    },
       
    get_PopupDragHandleControlID : function() {
        /// <value type="String">
        /// The ID of the element to display as the drag handle for the modal popup
        /// </value>
        return this._PopupDragHandleControlID;
    },
    set_PopupDragHandleControlID : function(value) {
        if (this._PopupDragHandleControlID != value) {
            this._PopupDragHandleControlID = value;
            this.raisePropertyChanged('PopupDragHandleControlID');
        }
    },

    get_BackgroundCssClass : function() {
        /// <value type="String">
        /// The CSS class to apply to the background when the modal popup is displayed
        /// </value>
        return this._BackgroundCssClass;
    },
    set_BackgroundCssClass : function(value) {
        if (this._BackgroundCssClass != value) {
            this._BackgroundCssClass = value;
            this.raisePropertyChanged('BackgroundCssClass');
        }
    },

    get_DropShadow : function() {
        /// <value type="Boolean">
        /// Whether or not a drop-shadow should be added to the modal popup
        /// </value>
        return this._DropShadow;
    },
    set_DropShadow : function(value) {
        if (this._DropShadow != value) {
            this._DropShadow = value;
            this.raisePropertyChanged('DropShadow');
        }
    },

    get_DropShadowOpacity : function() {
        return this._DropShadowOpacity;
    },
    
    set_DropShadowOpacity : function(value) {
        this._DropShadowOpacity = value;
    },
    
    get_DropShadowWidth : function() {
        return this._DropShadowWidth;
    },
    
    set_DropShadowWidth : function(value) {
        this._DropShadowWidth = value;
    },
    
    get_Drag : function() {
        /// <value type="Boolean">
        /// Obsolete: Setting the _Drag property is a noop
        /// </value>
        return this._Drag;
    },
    set_Drag : function(value) {
        if (this._Drag != value) {
            this._Drag = value;
            this.raisePropertyChanged('Drag');
        }
    },

    get_OkControlID : function() {
        /// <value type="String">
        /// The ID of the element that dismisses the modal popup
        /// </value>
        return this._OkControlID;
    },
    set_OkControlID : function(value) {
        if (this._OkControlID != value) {
            this._OkControlID = value;
            this.raisePropertyChanged('OkControlID');
        }
    },

    get_CancelControlID : function() {
        /// <value type="String">
        /// The ID of the element that cancels the modal popup
        /// </value>
        return this._CancelControlID;
    },
    set_CancelControlID : function(value) {
        if (this._CancelControlID != value) {
            this._CancelControlID = value;
            this.raisePropertyChanged('CancelControlID');
        }
    },

    get_OnOkScript : function() {
        /// <value type="String">
        /// Script to run when the modal popup is dismissed with the OkControlID
        /// </value>
        return this._OnOkScript;
    },
    set_OnOkScript : function(value) {
        if (this._OnOkScript != value) {
            this._OnOkScript = value;
            this.raisePropertyChanged('OnOkScript');
        }
    },

    get_OnCancelScript : function() {
        /// <value type="String">
        /// Script to run when the modal popup is dismissed with the CancelControlID
        /// </value>
        return this._OnCancelScript;
    },
    set_OnCancelScript : function(value) {
        if (this._OnCancelScript != value) {
            this._OnCancelScript = value;
            this.raisePropertyChanged('OnCancelScript');
        }
    }
}
CustomPage.Tools.ModalPopupBehavior.registerClass('CustomPage.Tools.ModalPopupBehavior', AjaxControlToolkit.DynamicPopulateBehaviorBase);


CustomPage.Tools.UpdateProgressFixer = function CustomPage$Tools$ImageDisplayer(associatedElement) {
    CustomPage.Tools.UpdateProgressFixer.initializeBase(this);
    
    this._updPanels = new Array();
    this._updProgresses = new Array();
    this._initialized = false;
    
    this._initializeRequest$delegate = Function.createDelegate(this, this._initializeRequest);
    this._endRequest$delegate = Function.createDelegate(this, this._endRequest);
    this._postBackElement;
}
CustomPage.Tools.UpdateProgressFixer.prototype = {
    
    initialize : function this$initialize() {
        CustomPage.Tools.UpdateProgressFixer.callBaseMethod(this, "initialize");

    },
    
    dispose : function this$dispose() {
        CustomPage.Tools.UpdateProgressFixer.callBaseMethod(this, "dispose");
        
        if (this._initialized) {
            var prm = Sys.WebForms.PageRequestManager.getInstance();
            prm.remove_initializeRequest(this._initializeRequest$delegate);
//            prm.remove_endRequest(this._endRequest$delegate);
            this._initialized = false;
        }
    },
    
    addEntry : function(updPanelId, updProgressId) {
        if (this._initialized) {
            var prm = Sys.WebForms.PageRequestManager.getInstance();
            prm.add_initializeRequest(this._initializeRequest$delegate);
//            prm.add_endRequest(this._endRequest$delegate);
            this._initialized = true;
        }
        Array.add(this._updPanels, updPanelId);
        Array.add(this._updProgresses, updProgressId);
    },
    
    _initializeRequest : function(sender, args) {
        this._postBackElement = args.get_postBackElement();
        for (   var i=0; i < this._updPanels.length; i++) {
            if (this._postBackElement.id.startsWith(this._updPanels[i])) {
                $get(this._updProgresses[i]).style.display = 'block';
                break;    
            }
        }
    },
    
    _endRequest : function(sender, args) {
        for (var i=0; i < this._updPanels.length; i++) {
            if (this._postBackElement.id.startsWith(this._updPanels[i])) {
                $get(this._updProgresses[i]).style.display = '';
                break;    
            }
        }
    }
     
}

CustomPage.Tools.UpdateProgressFixer.registerClass('CustomPage.Tools.UpdateProgressFixer', Sys.Component);

UpdateProgressDisplayer = $create(CustomPage.Tools.UpdateProgressFixer, null, null, null, null);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();
