$(function() {
    function checkLength(o,n,min,max) {
    	if ( o.val().length > max || o.val().length < min ) {
    		o.addClass('ui-state-error');
    		$("#validateTips").text("Length of " + n + " must be between "+min+" and "+max+".").effect("highlight",{},1500);
    		return false;
    	} else {
    		return true;
    	}
    }
    
    function checkRegexp(o,regexp,n) {
    	if ( !( regexp.test( o.val() ) ) ) {
    		o.addClass('ui-state-error');
    		$("#validateTips").text(n).effect("highlight",{},1500);
    		return false;
    	} else {
    		return true;
    	}
    }
    
    $("#new_login").dialog({
        autoOpen: false,
        draggable: false,
        modal: true,
        width: 300,
        buttons: {
            'Set nickname': function() {
                var bValid = true;
                
                $("#nickname").removeClass('ui-state-error');
                
                bValid = bValid && checkLength($("#nickname"), 'nickname', 3, 16);
                
                bValid = bValid && checkRegexp($("#nickname"),/^[a-z]([0-9a-z_])+$/i,"Nickname may consist of a-z, 0-9, underscores, begin with a letter.");

                if(bValid) {
                    var nicknameData = { nickname: $("#nickname").val() };
                    $.ajax({
                        type: "POST",
                        url: "/login.json",
                        data: nicknameData,
                        dataType: "json",
                        success: function(data) {
                            if (data['errors'] !== undefined && data['errors']['nickname'] !== undefined && data['errors']['nickname']['free'] !== undefined && data['errors']['nickname']['free'] === false) {
                                $("#nickname").addClass('ui-state-error');
                                $("#validateTips").text("Nickname already in use, please try another.").effect("highlight", {}, 1500);
                                return false;
                            } else {
                                location.reload(true);
                            }
                        }
                    });
                }
            },
            Cancel: function() {
                $(this).dialog('close');
            }
        }
    });
    
    $("#please-wait").dialog({
       autoOpen: false,
       draggable: false,
       modal: true,
       width: 300 
    });
    
    $("#openid-button").click(function() {
        $("#openid").dialog('open');
    });
    
    $("#openid").dialog({
       autoOpen: false,
       draggable: false,
       modal: true,
       width: 300,
       buttons: {
           'Login': function() {
               $("#openid-form").submit();
           },
           Cancel: function() {
                $(this).dialog('close');
           }
       }
    });
    
    $("#openid-next").val(location.href);
});

function login() {
    $("#please-wait").dialog('open');
    $.getJSON("/login.json",function(data){
        if (data['login'] !== undefined) {
            if (data['login']['new'] !== undefined && data['login']['new'] === true) {
                $("#please-wait").dialog('close');
                $("#new_login").dialog('open');
            }
            if (data['login']['refresh'] !== undefined && data['login']['refresh'] === true) {
                location.reload(true);
            }
        } else {
            $("#please-wait").dialog('close');
        }
    });
}

function logout() {
    $.getJSON("/logout.json",function(data){
        if (data['success'] !== undefined) {
            if (data['success'] === true) {
                location.reload(true);
            }
        }
    });
}