//Login window
function loginWindow(){
    this.init = function(){
        document.getElementById('login').focus();

        $('#login_form').bind('submit', function(){
            windows.setLoading();

            $.ajax({
                url: '/mail.php',
                type: "POST",
                data: {
                    email: $('#login').val(),
                    about: $('#about').val()
                },
                success: function(data){
                    windows.unsetLoading();
                    windows.unsetError();

                    if(data == '1'){
                        $('#login_form').html('Спасибо за участие в акцции, мы с вами свяжемся!');
                    }else{
                        windows.setError('Неправильный адрес электронной почты');
                    };
                },
                error: function(){
                    windows.unsetLoading();
                    windows.unsetError();
                    windows.unsetOk();
                    windows.setError('Ошибка передачи данных');
                }
            });

            return false;
        })
    }
}

//Windows class
function Windows(){
    this.width = 500;
    this.f = null;

    this.createOverlay = function(){
        var that = this;
        var overlay_HTML = '<div id="windows_overlay"></div>';

        $('body').prepend(overlay_HTML);
        $('#windows_overlay').css({
            opacity: 0.3
        }).bind('click', function(){
            that.closeWindow();
        });
    }

    this.resizeWindow = function(){
        var w = $('#windows_window');

        w.css({
            marginLeft: 0,
            marginTop: -w.height()/2
        });
    }

    this.closeWindow = function(){
        var w = $('#windows_window');
        var o = $('#windows_overlay');

        w.fadeOut(200, function(){
            $(this).remove();
        });

        o.fadeOut(200, function(){
            $(this).remove();
        });

        this.f = null;

        $('body').unbind('keyup');
    }

    this.setError = function(message){
        $('#windows_window .windows_error').text(message).show();
    };

    this.setOk = function(message){
        $('#windows_window .windows_ok').text(message).show();
    };

    this.unsetError = function(){
        $('#windows_window .windows_error').text('').hide();
    };

    this.unsetOk = function(){
        $('#windows_window .windows_ok').text('').hide();
    };

    this.setLoading = function(){
        $('#windows_window .windows_header').addClass('windows_loading');
    }

    this.unsetLoading = function(){
        $('#windows_window .windows_header').removeClass('windows_loading');
    }

    this.showWindow = function(header, container_id, func){
        var that = this;

        var content = $('#'+container_id).html();

        if(content == null){
            content = ''
        };

        var window_HTML =
        '<div id="windows_window">' +
            '<div class="windows_header">' +
                '<a class="close" href="javascript:void(0)" title="Закрыть"></a>' +
                '<h1>' + header + '</h1>' +
            '</div><div class="windows_error"></div>' +
            '<div class="windows_ok"></div>' +
            '<div class="windows_content">' +
                content +
            '</div>' +
        '</div>';

        $('body').prepend(window_HTML);

        $('#windows_window a.close').bind('click', function(){
            that.closeWindow();
        });

        this.f = new func();
        this.f.init();

        $('#windows_window').css({
            width: this.width
        });

        $('body').bind('keyup', function(event) {
            if(event.keyCode == '27'){
                that.closeWindow();
            };
        });

        this.createOverlay();
        this.resizeWindow();
    }
}

var windows = new Windows();
