var facebookLogin = false;

function fb_user() {
		facebookLogin = true;
		var me = null;
		var friends = null;
		
		FB.api('/me', function(data) {
			me = data;
			if (friends != null) {
				getFlash().fb_login(me, friends);
			}
		});

		FB.api('/me/friends', function(data) {
			friends = data;
			if (me != null) {
				getFlash().fb_login(me, friends);
			}
		});

}

function page_fblogin() {
	FB.init({
	     appId  : '157700507614340',
	     status : true, 
	     cookie : true, 
	     xfbml  : true  
	   });

	FB.getLoginStatus(function(response) {
	if (response.status === 'connected') { // the user is logged in and connected to your application
		fb_user();
	} else { //the user isn't logged in to Facebook or isn't connected to the application
	FB.login(fb_user, 
	{scope:'read_stream,publish_stream,email,offline_access'});
	}});

}

function page_fbpost(body) {
    FB.api('/me/feed', 'post', {message: body}, function(response) {
        // nothing yet
    });
}

function page_fbpostfriend(id, body) {
    FB.api('/' + id + '/feed', 'post', {message: body}, function(response) {
        // nothing yet
    });
}


function page_fbloginpost(body) {
	if (facebookLogin) {
		page_fbpost(body);		
	} else {
		FB.init({
		     appId  : '157700507614340',
		     status : true, 
		     cookie : true, 
		     xfbml  : true  
		   });
	

	FB.getLoginStatus(function(response) {
	if (response.status === 'connected') { // the user is logged in and connected to your application
			page_fbpost(body);		
	} else { //the user isn't logged in to Facebook or isn't connected to the application
		FB.login(function() {
			page_fbpost(body);		
		}, 
	{scope:'read_stream,publish_stream,email,offline_access'});
	}});
	}
}


function page_fbfriend(friend) {
    FB.api(friend, function(response) {
		getFlash().fb_friend(response);
    });
}


function getFlash() {
    if (navigator.appName.indexOf("Microsoft") != -1){
        return window.wee;
    } else {
        return document.wee;
    }
}


