﻿// README
//
// There are two steps to adding a property:
//
// 1. Create a member variable to store your property
// 2. Add the get_ and set_ accessors for your property.
//
// Remember that both are case sensitive!
//

Type.registerNamespace('OnLineExt.ModalPopup');

OnLineExt.ModalPopup.MPCloserBehavior = function(element) {
    OnLineExt.ModalPopup.MPCloserBehavior.initializeBase(this, [element]);

    this._TTByClick = true;
    this._OffsetX = 0;
    this._OffsetY = 0;

    this._onBackgroundHandler = null;

    //���������� ���������� �������� ����
    this._CurrclientY = 0;
    this._CurrclientX = 0;

    this._UserPostBack = null;

}

OnLineExt.ModalPopup.MPCloserBehavior.prototype = {

    initialize: function() {

        this._popupElement = $get(this._PopupControlID);
        this._onCommonCancelHandler = Function.createDelegate(this, this._onCommonCancel);
        this._onCommonPstHandler = Function.createDelegate(this, this._onCommonPst);


        OnLineExt.ModalPopup.MPCloserBehavior.callBaseMethod(this, 'initialize');

        // ����� ������ ����
        this._angl = FindShablon(this._popupElement, "angl");

        // ����� ��� ��������� �� �� �� ���� ��������������
        if (this._backgroundElement)
            $addHandler(this._backgroundElement, 'click', this._onCommonCancelHandler);

        this.attachContentHandles();

    },

    attachContentHandles: function() {
        // ��� ������� ��������������
        var lbCloser = FindShablon(this._popupElement, "closer");
        if (lbCloser)
            $addHandler(lbCloser, 'click', this._onCommonCancelHandler);

        // ��� �� �� ������ ��������������   
        var BtnClose = FindShablon(this._popupElement, "BtnClose");
        if (BtnClose)
            $addHandler(BtnClose, 'click', this._onCommonCancelHandler);

        // ��� �� �� ������ ��������������   
        var BtnPst = FindShablon(this._popupElement, "BtnPst");
        if (BtnPst)
            $addHandler(BtnPst, 'click', this._onCommonPstHandler);


    },

    detachContentHandles: function() {
        // ������������ � ������ ������� ������ ��������������.
        var lbCloser = FindShablon(this._popupElement, "closer");
        if (lbCloser && this._onCommonCancelHandler && CheckHandler(lbCloser, 'click', this._onCommonCancelHandler))
            $removeHandler(lbCloser, 'click', this._onCommonCancelHandler);

        // ���� ��� ������� � �� ��� ��� ���� �������  ����� ��� ������� � ����� ��� ���������.
        var BtnClose = FindShablon(this._popupElement, "BtnClose");
        if (BtnClose && this._onCommonCancelHandler && CheckHandler(BtnClose, 'click', this._onCommonCancelHandler))
            $removeHandler(BtnClose, 'click', this._onCommonCancelHandler);

        // ���� ��� ������� � �� ��� ��� ���� �������  ����� ��� ������� � ����� ��� ���������.
        var BtnPst = FindShablon(this._popupElement, "BtnPst");
        if (BtnPst && this._onCommonPstHandler && CheckHandler(BtnPst, 'click', this._onCommonPstHandler))
            $removeHandler(BtnPst, 'click', this._onCommonPstHandler);


    },


    // ���������� �������� ������� ��� ����� ����
    _onShow: function(e) {
        this._CurrclientY = e.clientY;
        this._CurrclientX = e.clientX;
        // ��� ��� ������ ���� this._TTByClick = true, �������� ��������
        if (this._TTByClick) {
            // ����������� ��������� �� �������� ����
            this._xCoordinate = e.clientX + this._OffsetX;
            this._yCoordinate = e.clientY + this._OffsetY;

            var clientBounds = CommonToolkitScripts.getClientBounds();
            var clientWidth = clientBounds.width;
            var clientHeight = clientBounds.height;
            var popup = FindShablon(this._popupElement, "vispopup");
            if (popup) // ������� ���������
            {
                var pxH = parseInt(popup.style.height.replace('px', '')); // For FF Comp.
                var pxW = parseInt(popup.style.width.replace('px', '')); // For FF Comp.

                this._xCoordinate = this._xCoordinate - pxW - 20;

                var dY = clientHeight - (this._yCoordinate + pxH);
                if (dY < 0) {
                    this._yCoordinate = this._yCoordinate + dY - 20;
                }
            }
            else {
                var popup1 = FindShablon(this._popupElement, "PopupMenuSt");
                if (popup1) // ���� � �������
                {
                    var pxW = popup1.clientWidth;
                    this._xCoordinate = this._xCoordinate - pxW - 40;
                    this._yCoordinate = this._yCoordinate - 50;
                }
                else // � ������ ������ ���� ���� � ����������� �����������������
                {
                    // nsn ������� ��� ���������, ����� ������ ������������ ���� ������� �� ������� ������. 
                    // ���������������� �� ���������� Y �� ����������, ��� ��� �� ������ ������ ������ ������������ ���� ����� 0.
                    var pxH = this._popupElement.clientHeight;
                    var pxW = this._popupElement.clientWidth;

                    if (this._xCoordinate + pxW > clientWidth)
                        this._xCoordinate = clientWidth - pxW - 20;

                    var dY = clientHeight - (this._yCoordinate + pxH);
                    if (dY < 0)
                        this._yCoordinate = clientHeight - dY - 20;
                }

            }
        }
        else {
        }

        var el = this.get_element(); // ������ ���� �� �������, ����� ����������
        if (IsEnabled(el)) {
            this.show();
            e.preventDefault();
            return false;
        }
    },


    _onLayout: function(e) {
        if (this._angl) {
            if (this._angl.style.visibility != "hidden")
                this._angl.style.visibility = "hidden";
        }
        /// <summary>
        /// Handler for scrolling and resizing events that would require a repositioning of the modal dialog
        /// </summary>
        this._layout();
    },

    _onCommonCancel: function(e) {
        this.hide();
        e.preventDefault();
        if (this._OnCancelScript) {
            window.setTimeout(this._OnCancelScript, 0);
        }
        return false;
    },

    // ��������� ������� ������ �� ��� ���� 
    // ��� � ������� ������
    _onCommonPst: function(e) {
        this.hide();
        return true;
    },


    show: function() {
        StopTimer();

        var oBody = document.body;
        oBody.scroll = "no";
        if (document.recalc)     // For Mozilla Firefox compat.   
            document.recalc(true);

        OnLineExt.ModalPopup.MPCloserBehavior.callBaseMethod(this, 'show');
    },

    hide: function() {

        OnLineExt.ModalPopup.MPCloserBehavior.callBaseMethod(this, 'hide');

        StartTimer();

        var oBody = document.body;
        oBody.scroll = "auto";
        if (this._angl) {
            if (this._angl.style.visibility != "visible")
                this._angl.style.visibility = "visible";
        }

    },

    get_TTByClick: function() {
        return this._TTByClick;
    },

    set_TTByClick: function(value) {
        this._TTByClick = value;
    },

    set_OffsetX: function(value) {
        this._OffsetX = value;
    },

    get_OffsetX: function(value) {
        return this._OffsetX;
    },

    set_OffsetY: function(value) {
        this._OffsetY = value;
    },

    get_OffsetY: function(value) {
        return this._OffsetY;
    },

    get_UserPostBack: function() {
        return this._UserPostBack;
    },

    set_UserPostBack: function(value) {
        this._UserPostBack = value;
    },

    dispose: function() {
        this.detachContentHandles();

        if (this._backgroundElement && this._onCommonCancelHandler && CheckHandler(this._backgroundElement, 'click', this._onCommonCancelHandler))
            $removeHandler(this._backgroundElement, 'click', this._onCommonCancelHandler);

        if (this._onCommonCancelHandler != null)
            this._onCommonCancelHandler = null;

        if (this._onCommonPstHandler != null)
            this._onCommonPstHandler = null;

        var oBody = document.body;
        oBody.scroll = "auto";
        if (this._angl) {
            if (this._angl.style.visibility != "visible")
                this._angl.style.visibility = "visible";
        }


        OnLineExt.ModalPopup.MPCloserBehavior.callBaseMethod(this, 'dispose');
    }

}

OnLineExt.ModalPopup.MPCloserBehavior.registerClass('OnLineExt.ModalPopup.MPCloserBehavior', OnLineExt.ModalPopup.ModalPopupBehavior);


