

function fb_login(permissions, target, deny_action)
{
	if(typeof(deny_action)	!= 'function')	deny_action = function(input){return true;};
	if(typeof(target) 		!= 'string' && typeof(target) != 'function' )	target = "/registration.php";
	if(typeof(permissions)	!= 'string')	permissions = "";
	if(typeof(target) 		== 'string')
		if(target.match(/\?/)) target = target+"&old_location="+escape(top.location.href);
	FB.login(function(response) {
		if (response.session) {
			if(typeof(target)=='string')
				top.location.href = target;
			elseif(typeof(target)=='function')
				target();
		} else {
			deny_action();
		}
	}, {perms:permissions});
}


/*
 * Inital loading of Facebook Javascript SDK
 * 			no need to create the fb-root element
 * ==================================================
 * app_ID #required is the app id of the facebook application
 * current_session #optional is to prefill the SDK with the current session 
 * 		could be <?php echo json_encode($facebook->getSession()); ?> if the $facebook is in php reachable
 * 		default null
 * on_load #optional a function witch one is called if the facebook framework is ready
 */
function fb_load(app_ID, current_session, on_load)
{
	if(typeof(current_session)	!= 'object') current_session = null;
	if(typeof(on_load)	!= 'function') on_load = function(){return true;};
	window.fbAsyncInit = function() {
		this.FB.init({
			appId	: app_ID,
			session : current_session, // 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
		});

		FB.Event.subscribe('edge.create', function(response) {
			if(typeof(fb_after_like) == "function") fb_after_like(true, response);
		});
		FB.Event.subscribe('edge.remove', function(response) {
			if(typeof(fb_after_like) == "function") fb_after_like(false, response);
		});
		on_load();
		
		FB.Canvas.setAutoResize();
	};

	(function() {
		var e = document.createElement('script');
		e.src = document.location.protocol + '//connect.facebook.net/de_DE/all.js';
		e.async = true;
		if(document.getElementById('fb-root') == null){
			var o = document.createElement('div');
			o.id = 'fb-root';
			document.getElementsByTagName('body')[0].appendChild(o);
		}
		document.getElementById('fb-root').appendChild(e);
	}());
}

function logingate(classname, permissions){
	jQuery('a.'+classname).click(function(){
		after_fb_login = this;
		if(FB.getSession()!==null)
			return true;
		else
			fb_login(permissions,function(){jQuery(this).click();},function(){alert('Sie müssen für diesen vorgang bei Facebook eingeloggt sein und der Application alle geforderten Rechte geben.');});
		return false;
	});
}

function fb_after_like(state, resp)
{
	if(state)
		jQuery('#likegate').fadeOut('fast');
	else
		jQuery('#likegate').fadeIn('fast');
}

if(typeof(jQuery) == 'function'){
	jQuery(function(){
		if(fbLikeGate)
			fb_after_like(false,false);
	});
}
