if (Affinitive === undefined) {
	var Affinitive = {};
}

var dontReload = false;

Affinitive.facebookReloadPage = function(){
	if (!dontReload) {
		window.location.reload();
	}
}

Affinitive.facebookAuthorize = function()
{
	jQuery.ajax({
		type: "POST",
		dataType: "json",
		//url: fpglobals.baseurl + "social-network/authorize/",
		url: fpglobals.baseurl + "json/social-network/facebook-process-login/",
		success: function(data, textStatus) {
			if (data.success) {
				window.location.reload();
			} else if (data.redirect) {
				window.location = data.redirect;
			} else {
				// let's not bother the user with an abrasive popup.
				//alert(data.message);
			}
		},
		async: false
	});
}

Affinitive.facebookConnect = function(extraPerms) {
	dontReload=true;
	if (typeof Affinitive.FBExtraPerms != 'undefined' && typeof extraPerms == 'undefined') {
		extraPerms = Affinitive.FBExtraPerms;
	}
	FB.login(function(response) {
		if (response.session) {
			// user logged in
			if (response.perms) {
				// user granted SOME (not all) perms
			}
			else {
				// user granted NO perms
			}
			Affinitive.facebookAuthorize();
			//window.location = fpglobals.baseurl + "social-network/facebook-process-login/";
		}
		else {
			// user not logged in 
		}
	}, {"perms": extraPerms});
}

Affinitive.logout = function(url)
{	
	var sess = FB.getSession();
	if (sess == null) {
		return true;
	}
	FB.logout(function(response) {
		window.location = url;
	});
	return false;
}

Affinitive.fbUsePhoto = function(checkbox)
{
	if (checkbox.checked) {
		urlHit = fpglobals.baseurl + "social-network/use-photo/y/";
	}
	else {
		urlHit = fpglobals.baseurl + "social-network/use-photo/n/";
	}
	jQuery.get(urlHit);
	jQuery('#fbUsePhotoReply').html("Preferences Saved<br />");
}

