function update_dom() {
	/* parse the dom again, if some fbml changed within the site */
	FB.XFBML.Host.parseDomTree();
}

function init(key, url) {
	/* init facebook framework check if user is logged in, callback */
	FB.init(key,
			url,
			{"ifUserConnected" : update_login_status});
}

function logout() {
	FB.Connect.logout(function() {
		$(".facebook-user").html("<div class=\"login-button\"><fb:login-button></fb:login-button></div>");
		update_dom();
        $('#nav-facebook a.connect').removeClass('blank');
	});
}

function update_profile_form() { //fill in info when the user connects to facebook (only if form is here)
	if($("form:profile-form").length > 0) {
		$.getJSON('/js_user_info/', function(data) {
    		$('#id_first_name').val(data['first_name']);
    		$('#id_last_name').val(data['last_name']);
    	});
    }
}

function update_login_status() {
	/* 	update the user box after login
		try to ask the user for the rights we need if not already granted
		dummy get from the server, to get auth setup right in middleware */
        $(".facebook-user").html("<div style='position:relative;left:10px;top:1px;'><div style='float:left;max-width:50px;margin-top:7px;'><fb:profile-pic size='square' uid='loggedinuser' facebook-logo='true'></fb:profile-pic></div><div style='float:left;padding:9px 0 0 5px;'><img src='../media/images/fb_welcome.png' /><br /><a style='padding:1px 0 0 12px;' href='#' onclick='javascript:logout();'><img src='../media/images/logout.png' /></a></div><div class='clear'></div></div>");
	$('#nav-facebook a.connect').addClass('blank');

    //check if the user has permission
	//ask for permission
	/*FB.Facebook.apiClient.users_hasAppPermission("publish_stream",
     function(result) {
        if (result == 0) { //we need the permissions!
            FB.Connect.showPermissionDialog("publish_stream");
        }
        //get from server, to get an auth update
    	//$.get('../dummy_auth_get/', null);
    	update_dom();
    });*/
    //temp
    update_dom();
    $.get('../dummy_auth_get/', function(data) {
    	update_profile_form();
    });


}

function send_profile_form() {
	//alert("form send!");
	$("form:profile-form").submit();
}

