(function($) {
	$.uniprogy.uFacebook = function(appId,appSession,loginUrl,mobile)
	{
		$('<div />').attr({id: "fb-root"}).appendTo('body');
		var e = document.createElement('script');
		e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
		e.async = true;
		document.getElementById('fb-root').appendChild(e);	
		
		$('.facebookShare').click(function(){
			var link = $(this).attr('href').replace(/http:\/\/([w|\.]+)?facebook\.com\/share.php\?u=/,'');
			$.uniprogy.facebook.share(link);
			return false;
		});
		
		window.fbAsyncInit = function() {
			FB.init({
				appId   : appId,
				session : appSession, // don't refetch the session when PHP already has it
				status  : true, // check login status
				cookie  : true, // enable cookies to allow the server to access the session
				xfbml   : true // parse XFBML
			});
			
			// whenever the user logs in, we refresh the page
			FB.Event.subscribe('auth.login', function() {
				$.uniprogy.uFacebookLoggedIn(loginUrl,mobile);
			});
		};
	};
	
	$.uniprogy.facebook = {
		share: function(url)
		{
			FB.ui({
		  	    method: 'stream.share',
		  	    u: url
		  	});
		}
	};
	
	$.uniprogy.uFacebookLoggedIn = function(loginUrl,mobile)
	{
		var worklet = mobile ? 'wlt-FacebookLoginButton' : 'wlt-UserMenu';
		$.ajax({
			url: loginUrl,
			success: function(data)
			{
				if(data && !mobile)
					$('#'+worklet).uWorklet().process({
						content: {
							replace: data
						}
					});
			}
		});
	}
})(jQuery);
