var PopupLogin = {

    show: function(nextLocation) {
        // nextLocation est soit une url-cible type "/user-plop", soit le nom d'une fonction
        var myRegExp = /\//i;

        new Ajax.Request(
            "/user-is_user_logged/",
            {
                method: "get",
                onComplete: function(t, xjson) {
                    if(xjson.userLogged) {
                        if(nextLocation.search(myRegExp) != -1) { // si on trouve la presence d'un slash "/" dans nextLocation, c'est une url-cible
                            window.location = nextLocation;
                        } else { // sinon c'est le nom d'une fonction
                            eval(nextLocation + "()");
                        }
                    } else {
                        if(!$("frmUserLogin")) {
                            PopupLogin.load(nextLocation);
                        } else {
                            window.alert("Vous devez vous identifier");
                        }
                    }
                }
            }
        );
        return false;
    },

    load: function(nextLocation) {
        var eMyDiv;

        // creation et affichage des div de la popup
        Tools.testSetDiv("popupOverlay");
        Tools.testSetDiv("popupLogin");
        $("popupLogin").setAttribute("class", "login");
        eMyDiv = document.createElement("div");
        eMyDiv.setAttribute("id", "loginandsubscriptionGradientBox");
        $("popupLogin").appendChild(eMyDiv);

        new Ajax.Updater(
            "loginandsubscriptionGradientBox",
            "/user-login_popup/",
            {
                method: "get",
                onComplete: function() {
                    popupDim = $("popupLogin").getDimensions();
                    viewportDim = document.viewport.getDimensions();
                    $("popupLogin").style.left = (viewportDim.width/2 - popupDim.width/2) + "px";
                    $("popupLogin").style.top = (viewportDim.height/2 - popupDim.height/2) + "px";

                    bodySize = $("global").getDimensions();
                    bodyHeight = (bodySize.height < viewportDim.height) ? viewportDim.height : bodySize.height;

                    $("popupOverlay").setStyle("height:"+bodyHeight+"px");
                    $("popupOverlay").setStyle("width:"+bodySize.width+"px");
                    $("popupOverlay").show();

                    $("popupLogin").show();

                    Actions.attach("popupFoot", "onclick", "PopupLogin.close");

                    Actions.attach("frmUserLogin", "onsubmit", "PopupLogin.submit", "'frmUserLogin', '" + nextLocation + "'");

                    Actions.attach("frmUserSubscribe", "onsubmit", "PopupLogin.submit", "'frmUserSubscribe', '" + nextLocation + "'");

                    $("frmUserLogin").focusFirstElement();
                }
            }
        );
    },

    close: function() {
        $("popupLogin").hide().update("");
        $("popupOverlay").hide();
        return false;
    },

    submit: function(formId, nextLocation) {
        // nextLocation est soit une url-cible type "/user-plop", soit le nom d'une fonction
        var myRegExp = /\//i;
        var nick = $F("nick");

        if(formId == "frmUserSubscribe" && !$("rules_ok").checked) { // test de l'email
            window.alert("Vous devez accepter le r\xE8glement pour continuer");
            return false;
        }

        $(formId).request({
            onComplete: function(t, xjson) {
                if(!xjson.isError) {

                    if(formId == "frmUserSubscribe") nick = $F("subscribeNick");

                    PopupLogin.close();

                    AjaxLogin.writeHeaderLogout(nick);

                    if(nextLocation.search(myRegExp) != -1) { // si on trouve la presence d'un slash "/" dans nextLocation, c'est une url-cible
                        window.location = nextLocation;
                    } else { // sinon c'est le nom d'une fonction
                        eval(nextLocation + "()");
                    }

                } else {
                    window.alert(xjson.errorMsg);
                }
            }
        });

        return false;
    }

};

