﻿Type.registerNamespace("Res.Commerce.Web.Composants");

Res.Commerce.Web.Composants.ModalPopup = function(element) {
    Res.Commerce.Web.Composants.ModalPopup.initializeBase(this, [element]);

    this._ouvertureControlId = null;
    this._fermetureControlId = null;
    this._focusControlId = null;
    this._popupId = null;
    this._formId = null;
    this._isVisible = null;

    this.form = null;
    this.masque = null;
    this.popup = null;
    this.ouvertureControl = null;
    this.fermetureControl = null;
    this.focusControl = null;
};

Res.Commerce.Web.Composants.ModalPopup.prototype =
{
    initialize: function () {
        Res.Commerce.Web.Composants.ModalPopup.callBaseMethod(this, "initialize");


        if (this._clientState != null) {
            this._isVisible = this._clientState.IsVisible;
        }

        this.ouvertureControl = $get(this._ouvertureControlId);
        this.fermetureControl = $get(this._fermetureControlId);
        this.focusControl = $get(this._focusControlId);
        this.popup = $get(this._popupId);
        this.form = $get(this._formId);

        //this.ouvertureControl = $("[id*='" + this._ouvertureControlId + "']");
        //this.fermetureControl = $("[id*='" + this._fermetureControlId + "']");
        //this.focusControl = $("[id*='" + this._focusControlId + "']");
        //this.popup = $("[id*='" + this._popupId + "']");
        //this.form = $("[id*='" + this._formId + "']");

        if (this._isVisible == true) {
            this.afficherPopup();
        }
        else {
            this._isVisible = false;
            this.cacherPopup();
        }

        if (this.ouvertureControl != null) {
            var afficherPopupHandler = Function.createDelegate(this, this.ouvertureClick);
            //$addHandler(this.ouvertureControl, "click", afficherPopupHandler);
            $("#" + this._ouvertureControlId).live("click", afficherPopupHandler);
        }

        if (this.fermetureControl != null) {
            var cacherPopupHandler = Function.createDelegate(this, this.fermetureClick);
            //$addHandler(this.fermetureControl, "click", cacherPopupHandler);
            $("#" + this._fermetureControlId).live("click", cacherPopupHandler);
        }

        var recentrerPopupHandler = Function.createDelegate(this, this.recentrerPopup);
        $(window).resize(recentrerPopupHandler);

    },
    dispose: function () {
        Res.Commerce.Web.Composants.ModalPopup.callBaseMethod(this, "dispose");
    },
    saveClientState: function () {
        this._clientStateField.value = "{ IsVisible:" + this._isVisible + " }";
        return null;
    },
    get_PopupId: function () {
        return this._popupId;
    },
    set_PopupId: function (value) {
        this._popupId = value;
    },
    get_FermetureControlId: function () {
        return this._fermetureControlId;
    },
    set_FermetureControlId: function (value) {
        this._fermetureControlId = value;
    },
    get_FocusControlId: function () {
        return this._focusControlId;
    },
    set_FocusControlId: function (value) {
        this._focusControlId = value;
    },
    get_FormId: function () {
        return this._formId;
    },
    set_FormId: function (value) {
        this._formId = value;
    },
    get_OuvertureControlId: function () {
        return this._ouvertureControlId;
    },
    set_OuvertureControlId: function (value) {
        this._ouvertureControlId = value;
    },
    get_IsVisible: function () {
        return this._isVisible;
    },
    set_IsVisible: function (value) {
        this._isVisible = value;
    },
    ouvertureClick: function (e) {
        // annuler l'evenement du trigger
        e.preventDefault();
        this.afficherPopup();
    },
    fermetureClick: function (e) {
        // annuler l'evenement du trigger
        e.preventDefault();
        this.cacherPopup();
    },
    recentrerPopup: function () {
        if ($(this.popup).is(':visible')) {
            this.cacherPopup();
            this.afficherPopup();
        }
    },
    f_filterResults:function (n_win, n_docel, n_body) {
	    var n_result = n_win ? n_win : 0;
	    if (n_docel && (!n_result || (n_result > n_docel)))
    		n_result = n_docel;
	    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
    },
    f_scrollLeft: function () {
            return this.f_filterResults(
		    window.pageXOffset ? window.pageXOffset : 0,
		    document.documentElement ? document.documentElement.scrollLeft : 0,
		    document.body ? document.body.scrollLeft : 0
	    );
    },
    f_scrollTop: function () {
            return this.f_filterResults(
		    window.pageYOffset ? window.pageYOffset : 0,
		    document.documentElement ? document.documentElement.scrollTop : 0,
		    document.body ? document.body.scrollTop : 0
	    );
    },

    afficherPopup: function () {
        this._isVisible = true;


        // détermnier la taille du masque (toute la page)
        var hauteurPage = $(document).height();
        var largeurPage = $(document).width();

        $(".memo_modalpopup_background").remove();

        // Création dynamique d'un masque, et ajout dans le body
        this.masque = $("<div></div>")[0];
        
        $(this.masque).addClass("memo_modalpopup_background");
        $(this.masque).css('height', hauteurPage);
        $(this.masque).css('width', largeurPage);
        $(this.masque).css('position', 'absolute');
        $(this.masque).css('left', 0);
        $(this.masque).css('top', 0);
        $(this.masque).css('z-index', 10000);
        $("body").append($(this.masque));
        // Afficher le masque
        $(this.masque).show();

        // déterminer la taille de la fenêtre
        var hauteurFenetre = $(window).height();
        var largeurFenetre = $(window).width();

        //On supprime du DOM les popup déjà activées avec le même ID
        $(this.form).children("[id*='" + this._popupId + "']").remove();

        // on place la popup à la racine de la page pour que le centrage soit OK
        $(this.form).append($(this.popup));


        // Positionner la popup au milieu de l'écran
        $(this.popup).css('position', 'absolute');
        $(this.popup).css('top', (hauteurFenetre / 2 - $(this.popup).height() / 2) + this.f_scrollTop());
        $(this.popup).css('left', (largeurFenetre / 2 - $(this.popup).width() / 2) + this.f_scrollLeft());
        $(this.popup).css('z-index', 10001);

        // Afficher la popup
        $(this.popup).show();
        $(this.focusControl).focus();
        var control = $find(this._popupId);
        if (control != null)
            control.initialize();
    },
    cacherPopup: function () {
        this._isVisible = false;
        if (this.masque != null) {
            $(this.masque).remove();
        }
        if (this.popup != null) {
            $(this.popup).hide();
        }
    }
};

Res.Commerce.Web.Composants.ModalPopup.registerClass("Res.Commerce.Web.Composants.ModalPopup", Res.Commerce.Web.Composants.ScriptControlBase);
if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded(); 