// Load On Document Ready;
$(document).ready(function(){
	
	/* Global Functions */
	$("a[href=#top]").click(function(){
		$('html, body').animate({scrollTop:0}, 'slow');
		return false;
	});
	
	/* Register */
	var register_options = {
			title     : "REGISTER",
		    buttons   : {"Cancel" : function(){$(this).dialog("close");}, "REGISTER" : registerAjax},
		    // buttons   : {"Cancel" : function(){$(this).dialog("close");}, "REGISTER" : function(){$("#register-form").submit()}},
			minHeight : 420,
			width  	  : 550,
			modal     : true,
			autoOpen  : false
	};
	
	$("#register").dialog(register_options);
	
	$(".button2").click(function(e){
		e.preventDefault()
		$("#register").dialog('open');
	});
	
	// hide dropdown list of businesses until user selects "I Am A: Business Owner"
	$("#iUserType1, #iUserType2").focus(function(){
		if($(this).val() == '1'){
			$("#business").show();
		}else{
			$("#business").hide();
		}
	});
	
	function registerAjax(){
		$.post("user/register", $("#register-form").serialize(),registerCallback);
	}
	
	function registerCallback(data){
		if(data == "success"){
			window.location.href = "business-directory/index";
		}else{
			var result = $.parseJSON(data);
			$("#errors").html("");
			for(var i in result){
				$("#errors").append(result[i] + "<br />");
			}
		}
	}
	
	/* Login */
	var login_options = {
			title     : "LOGIN",
			buttons   : {"Cancel" : function(){$(this).dialog("close");}, "LOGIN" : loginAjax},
		    // buttons   : {"Cancel" : function(){$(this).dialog("close");}, "LOGIN" : function(){$("#login-form").submit();}},
			height 	  : 200,
			width  	  : 250,
			modal     : true,
			autoOpen  : false
	};
	
	$("#sign-in").dialog(login_options);
	
	$(".button1").click(function(e){
		e.preventDefault()
		$("#sign-in").dialog('open');
	});
	
	function loginAjax(){
		$.post("user/login", $("#login-form").serialize(),loginCallback);
	}
	
	function loginCallback(data){
		if(data == 'valid'){
			$(window.location).attr('href', 'business-directory/index');
		}else{
			$("#login-error").html(data);
		}
	}
	
	/* Update */
	var update_options = {
			title     : "UPDATE YOUR PROFILE",
			buttons   : {"Cancel" : function(){$(this).dialog("close");}, "Update" : updateAjax},
		    // buttons   : {"Cancel" : function(){$(this).dialog("close");}, "LOGIN" : function(){$("#login-form").submit();}},
			height 	  : 420,
			width  	  : 550,
			modal     : true,
			autoOpen  : false
	};
	
	$("#update").dialog(update_options);
	
	$("#update-link").click(function(e){
		e.preventDefault();
		$("#update").dialog('open');
	});
	
	
	$("#update-iUserType1, #update-iUserType2").focus(function(){
		if($(this).val() == '1'){
			$("#update-business").show();
		}else{
			$("#update-business").hide();
		}
	});
	
	
	function updateAjax(){
		$.post("user/update", $("#update-form").serialize(),updateCallback);
	}
	
	function updateCallback(data){
		if(data == 'success'){
			$("#update").dialog('close');
			alert("Your account has been updated.");
			location.reload();
		}else{
			var result = $.parseJSON(data);
			$("#update-errors").html("");
			for(var i in result){
				$("#update-errors").append(result[i]);
			}
		}
	}
	
});
