/**
 * @author josh
 */
logged_in = false;
first_name = "";
last_name = "";
username = "";
incorrect_username_or_password_TEXT = "You have entered an invalid username and password combination. Please try again. If you don't have an account you can <a href=\"/user/register/\">Register</a> for one.<br /><br /><i>Tips: Check your spelling, the caps lock key, and that you're using the right password for your account.</i>";
already_logged_in_TEXT = "You are already logged in. This can happen when you have two tabs or windows open for BeardedGames and login on one and try to login on the other. Please refresh this page to see your logged in status."
function is_logged_in(){
    return logged_in;
}

login_listeners = Array();
function add_loggin_listener(listener){
    login_listeners.push(listener)
}

vote_get_listeners = Array();
function add_vote_get_listener(listener){
    vote_get_listeners.push(listener)
}
function call_login_listeners(){
    /*
     login_listeners.forEach(function(listener){
     listener.call();
     });*/
    //for (listener in login_listeners) {
    //listener();
    for (i = 0; i < login_listeners.length; i++) {
        login_listeners[i].call();
    }
    //}
}

function call_vote_get_listeners(){
    for (n = 0; n < vote_get_listeners.length; n++) {
        vote_get_listeners[n].call();
    }
    //}
}

function ajax_login(login_username, login_password, error_div){
    if (login_username == "username") {
        set_overlay("Login Error", "\"username\" is not an actual valid username. You probably didn't change the default username in the login box.", "Close and Try Again")
        toggle_overlay();
        return false;
    }
    new Ajax.Request('/ajax', {
        method: 'post',
        parameters: {
            'json': Array({
                'username': login_username,
                'password': login_password
            }).toJSON(),
            'method': 'login'
        },
        onSuccess: function(transport){
            var json = transport.responseText.evalJSON();
            if (json.success == "yes") {
                logged_in = true;
                username = json.username;
                first_name = json.first_name;
                last_name = json.last_name;
                call_login_listeners();
				call_vote_get_listeners();
            }
            else 
                if (json.success == "no") {
                    //$(error_div).innerHTML = "Error, not logged in: " + json.reason;
                    dialog_message = json.reason;
                    if (json.reason == "incorrect_username_or_password") {
                        dialog_message = incorrect_username_or_password_TEXT;
                    }
                    set_overlay("Login Error", dialog_message, "Close and Try Again")
                    toggle_overlay();
                }
        }
    });
}

function update_unread_messages(){
    new Ajax.Request('/ajax', {
        method: 'post',
        parameters: {
            'json': Array({}).toJSON(),
            'method': 'get_unread_count'
        },
        onSuccess: function(transport){
            var json = transport.responseText.evalJSON();
            if (json.success == "yes") {
                $('messaging-placeholder').innerHTML = '<a href="/messaging/inbox">Messaging (' + json.count + ')</a>';
            }
            else 
                if (json.success == "no") {
                //fail
                }
        }
    });
}

function ajax_register(register_username, register_password_1, register_password_2) {
	new Ajax.Request('/ajax', {
        method: 'post',
        parameters: {
            'json': Array({
                'username': register_username,
                'password_1': register_password_1,
				'password_2': register_password_2
            }).toJSON(),
            'method': 'register_user'
        },
        onSuccess: function(transport){
            var json = transport.responseText.evalJSON();
            if (json.success == "yes") {
                logged_in = true;
                username = json.username;
				if(json.username_changed) {
					alert("Your username has been changed to \""+ json.username +"\" for technical reasons.");
				}
                first_name = json.user_settings_first_name;
                last_name = json.user_settings_last_name;
                call_login_listeners();
            }
            else 
                if (json.success == "no") {
                    //$(error_div).innerHTML = "Error, not logged in: " + json.reason;
                    dialog_message = json.reason;
                    set_overlay("Register Error", dialog_message, "Close and Try Again")
                    toggle_overlay();
                }
        }
    });
}
var site_title=document.title;
function panic() {
	$('site_wrapper').style.display = "none";
	$('panic_wrapper').style.display= "block";
	document.title=panic_title;
}

function unpanic() {
	$('site_wrapper').style.display = "block";
	$('panic_wrapper').style.display = "none";
	document.title=site_title;
}

function update_game_vote(url, div_id){
    new Ajax.Request('/ajax', {
        method: 'post',
        parameters: {
            'json': Array({
                'url': url,
                'username': username
            }).toJSON(),
            'method': 'get_vote_on_game'
        },
        onSuccess: function(transport){
            var json = transport.responseText.evalJSON();
            if (json.success == "yes") {
                if (json.choice != "") {
                    $(div_id).innerHTML = " - <img src=\"/images/vote-" + json.choice + ".png\" class=\"vote\" />";
                }
            }
            else 
                if (json.success == "no") {
                //fail
                }
        }
    });
}

function load_game_votes(){
    new Ajax.Request('/ajax', {
        method: 'post',
        parameters: {
            'json': Array({}).toJSON(),
            'method': 'get_user_votes'
        },
        onSuccess: function(transport){
            var json = transport.responseText.evalJSON();
            if (json.success == "yes") {
                for (i = 0; i < json.game_count-1; i++) {
					alert(json.votes[i]);
                }
            }
            else {
                alert("fail");
            }
        },
        onFailure: function(){
            alert("error");
        }
    });
}

function cancel_settings(){
    if (confirm("Are you sure you want to cancel?")) {
        window.location = "/";
    }
}

