// HEY! THAT'S MY PHISH!
// stolen by J()by from the board game "Hey! That's My Fish!"
// buy a real copy, it's a great game to have.

//home page function
function homeStart(){
	//bind the nav to click actions
	$("#mainPage a:eq(0)").click( registerAction );
	$("#mainPage a:eq(1)").click( loginAction );
	$("#mainPage a:eq(2)").click( aboutAction );
	
	$("#content").slideUp(500, function(){
		$("#content").load("content/home.cfm",{}, function(){
			$(this).slideDown(500);
		});
	});
	
	return(false);
}

//------------------ABOUT---------------------
//about action
function aboutAction(){
	$("#content").slideUp(500, function(){
		$("#content").load("content/about.cfm",{}, function(){$(this).slideDown(500);});
	});
}

//--------------------REGISTER---------------------
//register action
function registerAction(){
	//slide up
	$("#content").slideUp(500, function(){
		//load register form
		$("#content").load("content/register.cfm",{}, function(){
			//bind button click event
			$("#content .button").click( submitRegistration );
			$(this).slideDown(500);
		});
	});
	return false;
}

//submit registration actions
function submitRegistration(){
	//validate fields
	$("#content #name").validateEmpty("invalid");
	$("#content #email").validateEmpty("invalid");
	$("#content #password").validateEmpty("invalid");
	if( ! $("#content input").hasClass("invalid") )
	{
		$.ajax({
			url: 'action/users.cfm',
			type: 'POST',
			cache: false,
			data: {
				name:$("#content #name").val(), 
				email:$("#content #email").val(), 
				password:$("#content #password").val(), 
				action:"register"
				},
			dataType: 'html',
			timeout: 5000,
			error: function(){
				alert(':: error registering ::');
				},
			success: function(response){
				$("#content").slideUp(500, function(){$(this).html(response).slideDown(500);})
				}
		});
	}
}

//--------------------LOGIN---------------------
//login action
function loginAction(){
	//slide up
	$("#content").slideUp(500, function(){
		//load login form
		$("#content").load("content/login.cfm",{}, function(){
			//bind submit action
			$("#content .button").click( submitLogin );
			$(this).slideDown(500);
		});
	});
}

function submitLogin(){
	$("#content #email").validateEmpty("invalid");
	$("#content #password").validateEmpty("invalid");
	if( ! $("#content input").hasClass("invalid") )
	{
		$.ajax({
		    url: 'action/users.cfm',
		    type: 'POST',
			cache: false,
		    data: {
				email:$("#content #email").val(), 
				password:$("#content #password").val(), 
				action:"login"
				},
			dataType: 'html',
		    timeout: 5000,
		    error: function(){
		        alert(':: error logging in ::');
		    	},
		    success: function(response){
		    	$("#content").slideUp(500, function(){
					$(this).html(response).slideDown(500, function(){ 
						//login successful, show user controls
						if($("#loggedin").html()) 
						{
							loadGameRoom();
						}
					});
				});
		    	}
		});
	}
}

//------------------GAME ROOM---------------------
//user logged in, show controls
function loadGameRoom(){
	$("#mainPage").fadeOut(500, function(){
		$(this).html('').load("content/gameroom.cfm",{}, showGameRoom );
	});
}

function showGameRoom(){
	$("#content").slideUp(1);
	$("#stats").fadeOut(1);
	$("#username").hover(
		function(){ $("#stats").fadeIn(500); },
		function(){ $("#stats").fadeOut(500); }
	);
	$("#avatar").click(
		function(){
			if( !$("#avatar_form").html() ) $("#mainPage").prepend("<div id='avatar_form'></div>");
			$("#avatar_form").slideUp(1).load("content/avatar_upload.cfm", function(){$(this).slideDown(500);});
		}
	);
	$("#mainPage").fadeIn(500, function(){ $("#content").slideDown(500); });
}

//------------------GENERAL UTILS---------------------
//empty validator
jQuery.fn.validateEmpty = function( inv_class ){
	if(this.val().length == 0) 
	{
		this.addClass( inv_class ); 
	}
	else
	{ 
		this.removeClass( inv_class );
	}
	return this;
}