if (typeof window.Laruch == "undefined") {
    var Laruch = {};
}
Laruch.login = {
     url: '',
     furl: '',
     pos: '',
    type: 'get',
    data: null,
    init: function(E){
        this.url = E.url;
        this.furl = E.furl;
        this.pos = E.pos;
        this.getData(this.url, this.pos);

        $("#signin").click(function(e) {
            e.preventDefault();
            $("fieldset#signin_menu").toggle();
            $("#signin").toggleClass("menu-open");
        });
        
        $("fieldset#signin_menu").mouseup(function() {
            return false
        });
        $(document).mouseup(function(e) {
            if($(e.target).parent("a.menu").length==0) {
                $(".menu").removeClass("menu-open");
                $("fieldset#signin_menu").hide();
            }
        });

        $("#signinForm input").keypress(function (e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			$('#button_connexion').click();
			return false;
		} else {
			return true;
		}
        });
    },

    setListener: function(){
        var AA = this;
        $("#user_forgot_password").click(function(){
           Laruch.login.forgot(AA.furl, AA.pos);
           return false;
        });
    },

    getData: function(url, pos, data, type){
            var A = this;
            if(!data){
                data = this.data;
            }
            if(!type){
                type = this.type;
            }
            $.ajax({
                cache: false,
                type: type,
                url : url,
                data: data,
                success: function(data){
                    $(pos).html(data);
                    A.setListener();
                }
            });
    },

    forgot: function(url, pos){
        this.getData(url, pos);
    },

    submit: function(element, action){
        var url = action;
        var data = $(element).serialize();
        this.getData(url, this.pos, data, 'post');
    },

    newsletter: function(link){
        var loader = $('#newsLetter img');
        $.ajax({
            url: link,
            type : "post",
            data: $("#newsLetterForm").serialize(),
            dataType: "json",
            beforeSend: function(){
                loader.show();
            },
            success: function(data){
                loader.hide();
                $("#" + data.update).html(data.content);
            }
        });
        return false;
    }
}
