﻿Type.registerNamespace("Occitane.WebControls");

// Specifie l'alignement horizontal.
Occitane.WebControls.HorizontalSide = function() {
    throw Error.invalidOperation();
}
Occitane.WebControls.HorizontalSide.prototype = {
    Left : 0,
    Center : 1,
    Right : 2
}
Occitane.WebControls.HorizontalSide.registerEnum("Occitane.WebControls.HorizontalSide", false);

// Specifie l'alignement vertical.
Occitane.WebControls.VerticalSide = function() {
    throw Error.invalidOperation();
}
Occitane.WebControls.VerticalSide.prototype = {
    Top : 0,
    Middle : 1,
    Bottom : 2
}
Occitane.WebControls.VerticalSide.registerEnum("Occitane.WebControls.VerticalSide", false);


Occitane.WebControls.AlwaysVisiblePopup = function(element) {
    Occitane.WebControls.AlwaysVisiblePopup.initializeBase(this, [element]);
    
    this._horizontalOffset = 0;
    this._horizontalSide = Occitane.WebControls.HorizontalSide.Center;
    this._verticalOffset = 0;
    this._verticalSide = Occitane.WebControls.VerticalSide.Middle;
    this._animate = false;
    this._isModal = false;
    this._modalElement = null;
    //this._modalBgWidth = 0;
    
    this._onreposition$delegate = Function.createDelegate(this, this._reposition);;
    this._app_onload$delegate = Function.createDelegate(this, this._app_onload);
}

Occitane.WebControls.AlwaysVisiblePopup.prototype = {
    get_horizontalOffset : function() {
        return this._horizontalOffset;
    },
    set_horizontalOffset : function(value) {
        if (this._horizontalOffset != value) {
            this._horizontalOffset = value;
            this._reposition();
            this.raisePropertyChanged('HorizontalOffset');
        }
    },
    
    get_horizontalSide : function() {
        return this._horizontalSide;
    },
    set_horizontalSide : function(value) {
        if (this._horizontalSide != value) {
            this._horizontalSide = value;
            this._reposition();
            this.raisePropertyChanged('HorizontalSide');
        }
    },
//    get_modalBgWidth : function() {
//        return this._modalBgWidth;
//    },
//    set_modalBgWidth : function(value) {
//        if (this._modalBgWidth != value) {
//            this._modalBgWidth = value;
//            this._reposition();
//            this.raisePropertyChanged('ModalBgWidth');
//        }
//    },
    
    get_verticalOffset : function() {
        return this._verticalOffset;
    },
    set_verticalOffset : function(value) {
        if (this._verticalOffset != value) {
            this._verticalOffset = value;
            this._reposition();
            this.raisePropertyChanged('VerticalOffset');
        }
    },
    
    get_verticalSide : function() {
        return this._verticalSide;
    },
    set_verticalSide : function(value) {
        if (this._verticalSide != value) {
            this._verticalSide = value;
            this._reposition();
            this.raisePropertyChanged('VerticalSide');
        }
    },
    
    get_isModal : function() {
        return this._isModal;
    },
    set_isModal : function(value) {
        if (this._isModal != value) {
            this._isModal = value;
            this.raisePropertyChanged('IsModal');
        }
    },
    
    // Initialise le controle.
    initialize : function() {
        Occitane.WebControls.AlwaysVisiblePopup.callBaseMethod(this, 'initialize');
        
        var element = this.get_element();
        if (!element) throw Error.invalidOperation("Element requis.");

        // modal
        if (this._isModal)
        {
            this._modalElement = $get(element.id + "Modal");
        }

        this._animate = (Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version < 7);
        if (this._animate) {
            element.style.position = 'absolute';
            if (this._isModal) this._modalElement.style.position = 'absolute';
        } else {
            element.style.position = 'fixed';
            if (this._isModal) this._modalElement.style.position = 'fixed';
        }
                
        $addHandler(window, 'resize', this._onreposition$delegate);
        
        if (this._animate) {
            $addHandler(window, 'scroll', this._onreposition$delegate);
        }
        
        Sys.Application.add_load(this._app_onload$delegate);
    },
    
    // Libere les ressources.
    dispose : function() {
        Sys.Application.remove_load(this._app_onload$delegate);
    
        if (this._onreposition$delegate) {
            if (this._animate) {
                $removeHandler(window, 'scroll', this._onreposition$delegate);
            }
            $removeHandler(window, 'resize', this._onreposition$delegate);
            this._onreposition$delegate = null;
        }
        
        Occitane.WebControls.AlwaysVisiblePopup.callBaseMethod(this, 'dispose');
    },
    
    hide : function() {
        var element = this.get_element();
        if (!element) return;
        
        element.style.visibility = "hidden";
        element.style.display = "none";
        if (this._modalElement) this._modalElement.style.display = "none";
    },
    
    show : function() {
        var element = this.get_element();
        if (!element) return;
        
        element.style.visibility = "visible";
        element.style.display = "block";
        if (this._modalElement) this._modalElement.style.display = "block";
    },
    
    // Handlers
    _app_onload : function(sender, e) {
        this._reposition();
    },

    _reposition : function(e) {
        var element = this.get_element();
        if (!element) return;
        
        var displayBefore = element.style.display;
        element.style.display = "block";
        
        // prise en compte d'un element parent avec position absolute ou relative
        var offsetX = 0;
        var offsetY = 0;
        var parent = element.offsetParent;
        while (parent)
        {
            offsetX += parent.offsetLeft;
            offsetY += parent.offsetTop;
            
            parent = parent.offsetParent;
        }
        
        var x = 0;
        var y = 0;

        // Compute the initial offset if we're animating
        if (this._animate) {
            if (document.documentElement && document.documentElement.scrollTop) {
                x = document.documentElement.scrollLeft;
                y = document.documentElement.scrollTop;
            } else {
                x = document.body.scrollLeft;
                y = document.body.scrollTop;
            }
        }
        
        // Compute the width and height of the client
        //var clientBounds = CommonToolkitScripts.getClientBounds();
        var width;
        var height;
        switch(Sys.Browser.agent) {
            case Sys.Browser.InternetExplorer:
                width = document.documentElement.clientWidth;
                height = document.documentElement.clientHeight;
                break;
            case Sys.Browser.Safari:
                width = window.innerWidth;
                height = window.innerHeight;
                break;
            case Sys.Browser.Opera:
                width = Math.min(window.innerWidth, document.body.clientWidth);
                height = Math.min(window.innerHeight, document.body.clientHeight);
                break;
            default:  // Sys.Browser.Firefox, etc.
                width = Math.min(window.innerWidth, document.documentElement.clientWidth);
                height = Math.min(window.innerHeight, document.documentElement.clientHeight);
                break;
        }

        // Modal background
        if (this._modalElement)
        {
            this._modalElement.style.left = (-offsetX) + 'px';
            this._modalElement.style.top = (-offsetY) + 'px';
        
//            if (this._modalBgWidth != 0)
//            {
//                this._modalElement.style.width = this._modalBgWidth + 'px';
//            }
//            else
//            {
                this._modalElement.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), width) + 'px';
//            }
            
            this._modalElement.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), height) + 'px';
        }

        // Compute the horizontal coordinate
        switch (this._horizontalSide) {
            case Occitane.WebControls.HorizontalSide.Center :
                x = Math.max(0, Math.floor(x + width / 2.0 - element.offsetWidth / 2.0 - this._horizontalOffset) - offsetX);
                break;
            case Occitane.WebControls.HorizontalSide.Right :
                x = Math.max(0, x + width - element.offsetWidth - this._horizontalOffset - offsetX);
                break;
            case Occitane.WebControls.HorizontalSide.Left :
            default :
                x += this._horizontalOffset - offsetX;
                break;
        }
           
        // Compute the vertical coordinate
        switch (this._verticalSide) {
            case Occitane.WebControls.VerticalSide.Middle :
                y = Math.max(0, Math.floor(y + height / 2.0 - element.offsetHeight / 2.0 - this._verticalOffset) - offsetY);
                break;
            case Occitane.WebControls.VerticalSide.Bottom :
                y = Math.max(0, y + height - element.offsetHeight - this._verticalOffset - offsetY);
                break;
            case Occitane.WebControls.VerticalSide.Top :
            default :
                y += this._verticalOffset - offsetY;
                break;
        }

        element.style.left = x + 'px';
        element.style.top = y + 'px';
                
        element.style.display = displayBefore;
    }
}
Occitane.WebControls.AlwaysVisiblePopup.registerClass('Occitane.WebControls.AlwaysVisiblePopup', Sys.UI.Control);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();