verifyTOU = function(id) {
	if(typeof(id) === 'undefined') {
		id = 'accept-tou';
	}
	if(!$('#'+id).is(':checked')) {
		alert('You must accept the Terms of Use first.');
		return false;
	}
	return true;
};
signupLB = function(act) {
	act.stopImmediatePropagation();
	signupLBOpen();
	return false;
};
signupLBOpen = function() {	
	$('#signupLB').dialog({
		title: 'Get Your Free Account - No Credit Card Required!',
		modal: true,
		height: 265,
		width: 600
	});
};
fbPasswordLB = function(data) {
	$('#fbPasswordLB').dialog({
		title: 'Welcome '+data.name+', almost there! Just set a password.',
		modal: true,
		height: 200,
		width: 550,
		buttons: {
			'Set Password': function() {
				if(verifyTOU('accept-tou-pw')) {
					if($('#password').val() != $('#password_confirm').val()) {
						alert('Your passwords must match.');
					} else if($('#password').val().length < 6) {
						alert('Password must be at least 6 characters.');
					} else {
						$('#fbPasswordLB').dialog('close');
						var diag = $('<div>Creating Account...</div>');
						diag.dialog({
							title: 'Please Wait',
							modal: true,
							height: 125,
							width: 300,
							close: function() {
								diag.dialog('destroy');
							}
						});
						$.getJSON('/auth/fbcreate?pw='+$('#password').val(), function(data) {
							if(data.loggedin) {
								window.location = '/support/welcome';
							} else {
								alert('There was an error creating your account.');
								diag.dialog('close');
								fbPasswordLB(data);
							}
						});
					}
				}
			}
		}
	});
};
fbLogin = function(diag) {
	$.getJSON('/auth/fblogin', function(data) {
		diag.dialog('close');
		if(data.loggedin) {
			window.location = '/dashboard';
		} else {
			if(data.connected) {
				fbPasswordLB(data);
			} else {
				alert('An error occured pulling your facebook profile.');
			}
		}
	});	
};
fbconnect = function(act) {
	act.stopImmediatePropagation();
	$('#signupLB').dialog('close');
	var diag = $('<div>Connecting with Facebook...</div>');
	diag.dialog({
		title: 'Please Wait',
		modal: true,
		height: 125,
		width: 300,
		close: function() {
			diag.dialog('destroy');
		}
	});
	FB.getLoginStatus(function(response) {
		if (response.status === 'connected') {
			fbLogin(diag);
		} else {
			FB.login(function(response) {
				if(response.authResponse) {
					fbLogin(diag);
				} else {
					diag.dialog('close');
					signupLBOpen();
				}
			}, {scope: 'email'});
		}
	});
	
	return false;
};
function onLinkedInAuth() {
	IN.API.Profile("me").result(function(me) {
		var id = me.values[0].id;
		console.debug(me);
	});
}
$(document).ready(function() {
	$('.signupLBBind').live('click', signupLB);
	$('.fbconnect').live('click', fbconnect);
});

