var displayLogin;

$(function(){
    // btnExpand, boxLoginForm
    $("#btnExpand").click(function(){
        $("#boxLoginForm").slideToggle();
    });

    //txtSubscribe
    //formSubscribe

    noclick = function(e){
        alert('System is in progress, please wait...');
        return false;
    }

    subscribe = function(e){
        e.stopPropagation();
        if($('#txtSubscribe').val() != ""){
            filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
            if(filter.test($('#txtSubscribe').val())){
                $('#btnSubscribe').unbind('click').bind('click', noclick);
                $('#txtSubscribe').unbind('keypress').bind('keypress', noclick);
                
                $.getJSON('remote.php?action=subscribe&email='+$('#txtSubscribe').val(), function(json){
                    if(json.success){
                        alert(json.message);
                    }else{
                        alert(json.message);
                    }
                    $('#txtSubscribe').val('').focus();
                    $('#btnSubscribe').unbind('click').bind('click', subscribe);
                    $('#btnSubscribe').unbind('click').bind('click', subscribe);
                    $('#txtSubscribe').unbind('keypress').bind('keypress', function(e){
                        if(e.keyCode == 13){
                            subscribe(e);
                            return false;
                        }
                    });
                });
            } else{
                alert('You E-Mail format false.');
            }
        }else{
            alert('Please enter you E-Mail.');
        }
        return false;
    };

    $('#btnSubscribe').unbind('click').bind('click', subscribe);
    $('#txtSubscribe').unbind('keypress').bind('keypress', function(e){
        if(e.keyCode == 13){
            subscribe(e);
            return false;
        }
    });

    login = function(e){
        e.stopPropagation();

        err = '';
        if($('#username').val() == "")
            err += '\n - Username';
        if($('#password').val() == "")
            err += '\n - Password';

        if(err != ''){
            alert('The following error(s) occurred:' + err);
        }else{
            $('#btnLogin').unbind('click').bind('click', noclick);
            $.getJSON('remote.php?action=login&username='+$('#username').val()+'&password='+$('#password').val(), function(json){
                if(json.success){
                    alert(json.message);
                    window.location.href = 'asccClub.php';
                }else{
                    alert(json.message);
                }
                $('#username').val('').focus();
                $('#password').val('');
                    
                $('#btnLogin').unbind('click').bind('click', login);
            });
        }

        return false;
    }

    $('#btnLogin').unbind('click').bind('click', login);
    $('#username').keypress(function(e){
        if(e.keyCode == 13){
            $('#password').focus();
            return false;
        }
    });
    $('#password').keypress(function(e){
        if(e.keyCode == 13){
            login(e);
            return false;
        }
    });

    logout = function(e){
        e.stopPropagation();
        
        $('#btnLogOut').unbind('click').bind('click', noclick);
        $.getJSON('remote.php?action=logout', function(json){
            if(json.success){
                alert(json.message);
                window.location.href = 'index.php';
            }else{
                alert(json.message);
            }

            $('#btnLogOut').unbind('click').bind('click', logout);
        });
            
        return false;
    }

    $('#btnLogOut').unbind('click').bind('click', logout);

    editProfile = function(e){
        e.stopPropagation();

        err = '';
        if($('#nw_password').val() == "")
            err += '\n - New password';

        if($('#cnw_password').val() == "")
            err += '\n - Confirm new password';

        if($('#nw_password').val() != "" && $('#cnw_password').val() != "") {
            if($('#nw_password').val() !== $('#cnw_password').val())
                err += '\n - Password miss math';
        }

        if(err != ''){
            alert('The following error(s) occurred:' + err);
        }else{
            $('#btnUpdate').unbind('click').bind('click', noclick);
            $.getJSON('remote.php?action=editProfile&password='+$('#nw_password').val(), function(json){
                if(json.success){
                    alert(json.message);
                    window.location.href = window.location;
                }else{
                    alert(json.message);
                }
                $('#nw_password').val('').focus();
                $('#cnw_password').val('');

                $('#btnUpdate').unbind('click').bind('click', editProfile);
            });
        }

        return false;
    }

    $('#btnUpdate').unbind('click').bind('click', editProfile);
    
    displayLogin = function(){
        frmWidth = 346;
        frmHeight = 128;
        
        $('<div />').attr('id', 'loginContainer').appendTo('body');
        $.get('remote.php?action=lodeLoginForm', function(html){
            $('#loginContainer').html(html);
        }, 'html');
        
        var position = function(){
            $('#loginContainer').css({
                'left': ($(window).width() - frmWidth) / 2,
                'top': ($(window).height() - frmHeight) / 2
            });
        };

        position();

        $(window).resize(position).scroll(position);
   
    }
});

