(function($) {
	$.uniprogy.uFacebook = function(settings)
	{
		$('<div />').attr({id: "fb-root"}).appendTo('body');
		var e = document.createElement('script');
		e.src = document.location.protocol + '//connect.facebook.net/'+settings.language+'/all.js';
		e.async = true;
		
		document.getElementById('fb-root').appendChild(e);	
		
		window.fbAsyncInit = function() {
			FB.init({
				appId   : settings.appId,
				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 act
			FB.Event.subscribe('auth.statusChange', function(response) {
				if (response.status === 'connected')
					setTimeout('$.uniprogy.uFacebookLoggedIn("'+settings.loginUrl+'",'+settings.flag+')',0);
			});
		};
	};
	
	$.uniprogy.uFacebookLoggedIn = function(loginUrl,flag)
	{
		$.ajax({
			url: loginUrl,
			type: 'post',
			data: 'flag='+flag,
			success: function(data)
			{
				if(data)
					if(flag == 1)
						$('.facebookLoginButton').html(data);
					else
						$('#wlt-UserMenu').uWorklet().process({
							content: {
								replace: data
							}
						});
			}
		});
	}
})(jQuery);
