﻿//alert("Test for register");
///////////////////////////////////////////////////////////////////////////////////////////////////////////
var initialText = "ACS.Presentation.Web.DocPanel:";
var alertMessageText = initialText;

///////////////////////////////////////////////////////////////////////////////////////////////////////////
Type.registerNamespace('ACS.Presentation.Web.DocPanel');

///////////////////////////////////////////////////////////////////////////////////////////////////////////

ACS.Presentation.Web.DocPanel.DocSide = function() {
    throw Error.invalidOperation();
}

ACS.Presentation.Web.DocPanel.DocSide.prototype = {
    Top : 0,
    Left : 1,
    Right : 2,
    Bottom : 3,
    Middle : 4
}

ACS.Presentation.Web.DocPanel.DocSide.registerEnum("ACS.Presentation.Web.DocPanel.DocSide", false);
///////////////////////////////////////////////////////////////////////////////////////////////////////////



ACS.Presentation.Web.DocPanel.DocPanelBehavior = function(element) {

    ACS.Presentation.Web.DocPanel.DocPanelBehavior.initializeBase(this, [element]);

    // Add property variables here

    //Widget border panel
    this._widgetContainerId = null;
    //Widget content container panel
    this._contentContainerId  = null;
    //Widget content blocker panel
    this._contentBlockerId  = null;
    //Widget caption container panel
    this._captionContainerId = null;
    //Widget Toolbar container panel
    this._toolbarContainerId = null;
    //Widget footer container panel
    this._footerContainerId = null;
    //Widget Screen Blocking panel
    this._screenBlockerId = null;
    //The loader container panel
    this._loaderContainerId = null;
    //The offset from left side
    this._horizontalOffset = 0;
    //The offset from top side
    this._verticalOffset = 0;
    //The offset from right side
    this._horizontalSpan = 0;
    //The offset from bottom side
    this._verticalSpan = 0;
    //Docking info
    this._docSide = ACS.Presentation.Web.DocPanel.DocSide.Left;
    //Widget width
    this._width = 0;
    //Widget Height
    this._height = 0;
    //Widget minimum width, widget will not shorter then minimum width
    this._minWidth = 0;
    //Widget minimum Height
    this._minHeight = 0;     
    this._widthMode = 0;
    this._heightMode = 0;
    this._toolbarHeight = 35;
    this._zIndex = 10;    

}

ACS.Presentation.Web.DocPanel.DocPanelBehavior.prototype = {

    initialize: function() {
        ACS.Presentation.Web.DocPanel.DocPanelBehavior.callBaseMethod(this, 'initialize');

        //Add initalization code here

        var element = this.get_element();
        if (!element) return;
        //////////////////////////////////////////////////////////////////              
        // Create the resposition handler used to place the element
        this._repositionHandler = Function.createDelegate(this, this._reposition);
        // Attach the onResize handler
        $addHandler(window, 'resize', this._repositionHandler);
        // Move to the initial position
        this._reposition();
    },

    dispose: function() {
        // TODO: add your cleanup code here
        ACS.Presentation.Web.DocPanel.DocPanelBehavior.callBaseMethod(this, 'dispose');
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////
        $removeHandler(window, 'resize', this._repositionHandler);
        this._repositionHandler = null
    },

    _reposition: function(eventObject) {
        var element = this.get_element();
        if (!element) return;

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////  
        this.raiseRepositioning(Sys.EventArgs.Empty);
        //////////////////////////////////////////////////////////////////
        // Compute the width and height of the client
        var clientWidth;
        var clientHeight;
        switch (Sys.Browser.agent) {
            case Sys.Browser.InternetExplorer:
                clientWidth = document.documentElement.clientWidth;
                clientHeight = document.documentElement.clientHeight;
                break;
            case Sys.Browser.Safari:
                clientWidth = window.innerWidth;
                clientHeight = window.innerHeight;
                break;
            case Sys.Browser.Opera:
                clientWidth = Math.min(window.innerWidth, document.body.clientWidth);
                clientHeight = Math.min(window.innerHeight, document.body.clientHeight);
                break;
            default:  // Sys.Browser.Firefox, etc.                
                clientWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
                //clientHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
                clientHeight = window.innerHeight;
                break;
        }
        //////////////////////////////////////////////////////////////////
        var widgetContainer = element;
        if (!widgetContainer) return;

        //////////////////////////////////////////////////////////////////
        var contentContainer = null;
        if (this._contentContainerId != null) {
            if (Sys.UI.DomElement.getElementById(this._contentContainerId)) {
                contentContainer = Sys.UI.DomElement.getElementById(this._contentContainerId);
            }
        }

        var contentBlocker = null;
        if (this._contentBlockerId != null) {

            if (Sys.UI.DomElement.getElementById(this._contentBlockerId)) {
                contentBlocker = Sys.UI.DomElement.getElementById(this._contentBlockerId);
            }
        }

        var captionContainer = null;
        if (this._captionContainerId != null) {
            if (Sys.UI.DomElement.getElementById(this._captionContainerId))
                captionContainer = Sys.UI.DomElement.getElementById(this._captionContainerId);
        }

        var toolbarContainer = null;
        if (this._toolbarContainerId != null) {
            if (Sys.UI.DomElement.getElementById(this._toolbarContainerId))
                toolbarContainer = Sys.UI.DomElement.getElementById(this._toolbarContainerId);
        }

        var footerContainer = null;
        if (this._footerContainerId != null) {
            if (Sys.UI.DomElement.getElementById(this._footerContainerId))
                footerContainer = Sys.UI.DomElement.getElementById(this._footerContainerId);
        }

        var screenBlocker = null;
        if (this._screenBlockerId != null) {
            if (Sys.UI.DomElement.getElementById(this._screenBlockerId))
                screenBlocker = Sys.UI.DomElement.getElementById(this._screenBlockerId);
        }

        var loaderContainer = null;
        if (this._loaderContainerId != null) {
            if (Sys.UI.DomElement.getElementById(this._loaderContainerId))
                loaderContainer = Sys.UI.DomElement.getElementById(this._loaderContainerId);
        }
        //////////////////////////////////////////////////////////////////        
        var x = 0;
        var y = 0;

        if (isNaN(this._minWidth)) {
            alertMessageText += "\n - _minWidth - value is not numeric!";
        }
        else
            var width = this._minWidth;
        if (isNaN(this._minHeight)) {
            alertMessageText += "\n - _minHeight - value is not numeric!";
        }
        else
            var height = this._minHeight;
        //////////////////////////////////////////////////////////////////                
        if (isNaN(this._width)) {
            alertMessageText += "\n - _width - value is not numeric!";
        }

        if (isNaN(this._height)) {
            alertMessageText += "\n - _height - value is not numeric!";
        }

        if (isNaN(this._horizontalOffset)) {
            alertMessageText += "\n - _horizontalOffset - value is not numeric!";
        }

        if (isNaN(this._verticalOffset)) {
            alertMessageText += "\n - _verticalOffset - value is not numeric!";
        }

        if (isNaN(this._horizontalSpan)) {
            alertMessageText += "\n - _horizontalSpan - value is not numeric!";
        }

        if (isNaN(this._verticalSpan)) {
            alertMessageText += "\n - _verticalSpan - value is not numeric!";
        }

        if (isNaN(this._zIndex)) {
            alertMessageText += "\n - _zIndex - value is not numeric!";
        }

        if (isNaN(this._toolbarHeight)) {
            alertMessageText += "\n - _toolbarHeight - value is not numeric!";
        }

        if (isNaN(this._widthMode)) {
            alertMessageText += "\n - _widthMode - value is not numeric!";
        }

        if (isNaN(this._heightMode)) {
            alertMessageText += "\n - _heightMode - value is not numeric!";
        }

        if (alertMessageText != initialText) {
            alert(alertMessageText);
            alertMessageText = initialText;
        }
        //////////////////////////////////////////////////////////////////
        switch (this._docSide) {
            case ACS.Presentation.Web.DocPanel.DocSide.Middle:
                if (this._width > 0)
                    if (this._horizontalOffset > 0 || this._horizontalSpan > 0) {
                    x = Math.max(0, (Math.floor((clientWidth) / 2.0) + this._horizontalOffset - this._horizontalSpan));
                }
                else
                    x = Math.max(0, Math.floor((clientWidth - this._width) / 2.0));
                else
                    x = 0;
                if (this._width > 0)
                    width = this._width;
                else
                    width = clientWidth;

                if (this._height > 0) {
                    if (this._verticalOffset > 0)
                        y = this._verticalOffset;
                    else
                        y = Math.max(0, Math.floor((clientHeight - this._height) / 2.0));

                    height = this._height;
                }
                else {
                    if (this._verticalOffset > 0)
                        y = this._verticalOffset;
                    else
                        y = Math.max(0, Math.floor((clientHeight - this._height) / 2.0));
                        
                    var calculatedHeight = clientHeight - this._verticalOffset - this._verticalSpan;
                    if (calculatedHeight > this._minHeight)
                        height = calculatedHeight;
                    else
                        height = this._minHeight;
                }
                break;
            case ACS.Presentation.Web.DocPanel.DocSide.Left:

                x = this._horizontalOffset;
                if (this._width == 0) {
                    var calculatedWidth = clientWidth - this._horizontalOffset - this._horizontalSpan;
                    if (calculatedWidth > this._minWidth)
                        width = calculatedWidth;
                    else
                        width = this._minWidth;
                }
                else
                    width = this._width;

                y = this._verticalOffset;

                if (this._height > 0) {
                    height = this._height;
                }
                else {
                    var calculatedHeight = clientHeight - this._verticalOffset - this._verticalSpan;
                    if (calculatedHeight > this._minHeight)
                        height = calculatedHeight;
                    else
                        height = this._minHeight;
                }
                break;
            case ACS.Presentation.Web.DocPanel.DocSide.Right:
                x = clientWidth - this._width - this._horizontalSpan;
                width = this._width;

                y = this._verticalOffset;

                if (this._height > 0) {
                    height = this._height;
                }
                else {
                    var calculatedHeight = clientHeight - this._verticalOffset - this._verticalSpan;
                    if (calculatedHeight > this._minHeight)
                        height = calculatedHeight;
                    else
                        height = this._minHeight;
                }
                break;
            case ACS.Presentation.Web.DocPanel.DocSide.Bottom:
                x = this._horizontalOffset;
                var calculatedWidth = clientWidth - this._horizontalOffset - this._horizontalSpan;
                if (calculatedWidth > this._minWidth)
                    width = calculatedWidth;
                else
                    width = this._minWidth;

                y = clientHeight - this._height - this._verticalSpan;
                height = this._height;
                break;
            case ACS.Presentation.Web.DocPanel.DocSide.Top:
            default:
                x = this._horizontalOffset;
                if (this._width > 0) {
                    width = this._width;
                }
                else {
                    var calculatedWidth = clientWidth - this._horizontalOffset - this._horizontalSpan;
                    if (calculatedWidth > this._minWidth)
                        width = calculatedWidth;
                    else
                        width = this._minWidth;


                }
                y = this._verticalOffset;
                height = this._height;
                break;
        }

        //////////////////////////////////////////////////////////////////
        //widgetContainer          
        widgetContainer.style.position = 'absolute';
        widgetContainer.style.zIndex = this._zIndex;

        widgetContainer.style.left = x + 'px';
        if (this._widthMode != 1) {
            if (width > 0)
                widgetContainer.style.width = width + 'px';
        }

        widgetContainer.style.top = y + 'px';
        if (this._heightMode != 1) {
            if (height > 0)
                widgetContainer.style.height = height + 'px';
        }

        widgetContainer.style.visibility = "visible";
        //////////////////////////////////////////////////////////////////   
        //contentContainer   
        if (contentContainer != null) {
            if (this._widthMode != 1) {
                contentContainer.style.left = widgetContainer.style.left;
                if (width > 0)
                    contentContainer.style.width = width + 'px';
            }

            if (this._heightMode != 1) {
                if (captionContainer != null) {
                    height = height - 30;
                    y = y + 20;
                }
                if (toolbarContainer != null) {
                    height = height - this._toolbarHeight;
                    y = y + this._toolbarHeight;
                }

                if (footerContainer != null) {
                    height = height - 30;
                }

                contentContainer.style.top = y + 'px';
                if (height > 0)
                    contentContainer.style.height = height + 'px';
            }
        }
        if (footerContainer != null & contentContainer != null) {

            if (this._widthMode != 1) {
                footerContainer.style.left = widgetContainer.style.left;
                if (width > 0)
                    footerContainer.style.width = width + 'px';
            }

            if (this._heightMode != 1) {
                footerContainer.style.top = y + height + 'px';
            }
        }
        //////////////////////////////////////////////////////////////////    
        //contentBlocker;
        if (contentBlocker != null) {
            contentBlocker.style.position = 'absolute';
            contentBlocker.style.zIndex = (parseInt(widgetContainer.style.zIndex) + 10) + '';
            contentBlocker.style.left = contentContainer.style.left;
            contentBlocker.style.width = (parseInt(contentContainer.style.width) - 18) + 'px';
            contentBlocker.style.top = contentContainer.style.top;
            contentBlocker.style.height = (parseInt(contentContainer.style.height) - 16) + 'px';
            contentBlocker.style.display = 'block';
        }
        //////////////////////////////////////////////////////////////////   
        //loaderContainer
        if (loaderContainer != null) {
            loaderContainer.style.position = 'absolute';
            loaderContainer.style.zIndex = (parseInt(widgetContainer.style.zIndex) + 10) + '';
            loaderContainer.style.left = contentContainer.style.left;
            loaderContainer.style.width = (parseInt(contentContainer.style.width) - 18) + 'px';
            loaderContainer.style.top = contentContainer.style.top;
            loaderContainer.style.height = (parseInt(contentContainer.style.height) - 16) + 'px';
            loaderContainer.style.visibility = 'visible';
        }
        //////////////////////////////////////////////////////////////////
        // screenBlocker
        if (screenBlocker != null) {
            screenBlocker.style.position = 'absolute';
            widgetContainer.style.zIndex = (parseInt(widgetContainer.style.zIndex) + 1000) + '';
            screenBlocker.style.left = 0 + 'px';
            screenBlocker.style.top = 0 + 'px';
            screenBlocker.style.width = clientWidth + 'px';
            screenBlocker.style.height = clientHeight + 'px';
            screenBlocker.style.visibility = 'visible';
        }
        //////////////////////////////////////////////////////////////////                                   
        this.raiseRepositioned(Sys.EventArgs.Empty);
        //////////////////////////////////////////////////////////////////
        if (contentContainer != null) {
            if (contentContainer.className.indexOf("ShowScroll") != -1) {
                var widgetName = (contentContainer.id).substring(0, (contentContainer.id).indexOf("_"));
                if (document.getElementById(widgetName + "_ScrlPos"))
                    contentContainer.scrollTop = parseInt(document.getElementById(widgetName + "_ScrlPos").value);
            }
        }
    },


    // Add property accessors
    get_WidgetContainerId: function() {
        return this._widgetContainerId;
    },

    set_WidgetContainerId: function(value) {
        this._widgetContainerId = value;
    },

    get_ContentContainerId: function() {
        return this._contentContainerId;
    },

    set_ContentContainerId: function(value) {
        this._contentContainerId = value;
    },

    get_ContentBlockerId: function(value) {
        this._contentBlockerId = value;
    },

    set_ContentBlockerId: function(value) {
        this._contentBlockerId = value;
    },

    get_CaptionContainerId: function() {
        return this._captionContainerId;
    },

    set_CaptionContainerId: function(value) {
        this._captionContainerId = value;
    },

    get_ToolbarContainerId: function() {
        return this._toolbarContainerId;
    },

    set_ToolbarContainerId: function(value) {
        this._toolbarContainerId = value;
    },

    get_FooterContainerId: function() {
        return this._footerContainerId;
    },

    set_FooterContainerId: function(value) {
        this._footerContainerId = value;
    },

    get_ScreenBlockerId: function() {
        return this._screenBlockerId;
    },

    set_ScreenBlockerId: function(value) {
        this._screenBlockerId = value;
    },

    get_LoaderContainerId: function() {
        return this._loaderContainerId;
    },

    set_LoaderContainerId: function(value) {
        this._loaderContainerId = value;
    },

    get_DocSide: function() {
        return this._docSide;
    },

    set_DocSide: function(value) {
        this._docSide = value;
    },

    get_HorizontalOffset: function() {
        return this._horizontalOffset;
    },

    set_HorizontalOffset: function(value) {
        this._horizontalOffset = value;
    },

    get_VerticalOffset: function() {
        return this._verticalOffset;
    },

    set_VerticalOffset: function(value) {
        this._verticalOffset = value;
    },

    get_HorizontalSpan: function() {
        return this._horizontalSpan;
    },

    set_HorizontalSpan: function(value) {
        this._horizontalSpan = value;
    },

    get_VerticalSpan: function() {
        return this._verticalSpan;
    },

    set_VerticalSpan: function(value) {
        this._verticalSpan = value;
    },

    get_Width: function() {
        return this._width;
    },

    set_Width: function(value) {
        this._width = value;
    },

    get_Height: function() {
        return this._height;
    },

    set_Height: function(value) {
        this._height = value;
    },

    get_MinWidth: function() {
        return this._minWidth;
    },

    set_MinWidth: function(value) {
        this._minWidth = value;
    },

    get_MinHeight: function() {
        return this._minHeight;
    },

    set_MinHeight: function(value) {
        this._minHeight = value;
    },

    get_WidthMode: function() {
        return this._widthMode;
    },

    set_WidthMode: function(value) {
        this._widthMode = value;
    },

    get_HeightMode: function() {
        return this._heightMode;
    },

    set_HeightMode: function(value) {
        this._heightMode = value;
    },

    get_ToolbarHeight: function() {
        return this._toolbarHeight;
    },

    set_ToolbarHeight: function(value) {
        this._toolbarHeight = value;
    },

    get_ZIndex: function() {
        return this._zIndex;
    },

    set_ZIndex: function(value) {
        this._zIndex = value;
    },


    add_repositioning: function(handler) {
        /// <summary>
        /// Add an event handler for the repositioning event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().addHandler('repositioning', handler);
    },

    remove_repositioning: function(handler) {
        /// <summary>
        /// Remove an event handler from the repositioning event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().removeHandler('repositioning', handler);
    },

    raiseRepositioning: function(eventArgs) {
        /// <summary>
        /// Raise the repositioning event
        /// </summary>
        /// <param name="eventArgs" type="Sys.EventArgs" mayBeNull="false">
        /// Event arguments for the repositioning event
        /// </param>
        /// <returns />

        var handler = this.get_events().getHandler('repositioning');
        if (handler) {
            handler(this, eventArgs);
        }
    },

    add_repositioned: function(handler) {
        /// <summary>
        /// Add an event handler for the repositioned event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().addHandler('repositioned', handler);
    },

    remove_repositioned: function(handler) {
        /// <summary>
        /// Remove an event handler from the repositioned event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().removeHandler('repositioned', handler);
    },

    raiseRepositioned: function(eventArgs) {
        /// <summary>
        /// Raise the repositioned event
        /// </summary>
        /// <param name="eventArgs" type="Sys.EventArgs" mayBeNull="false">
        /// Event arguments for the repositioned event
        /// </param>
        /// <returns />

        var handler = this.get_events().getHandler('repositioned');
        if (handler) {
            handler(this, eventArgs);
        }
    }
}

ACS.Presentation.Web.DocPanel.DocPanelBehavior.registerClass('ACS.Presentation.Web.DocPanel.DocPanelBehavior', AjaxControlToolkit.BehaviorBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();