var as_app_name = "flashcontent";
var apiKey = "";
var fb_page_id = "";
var fb_session = null;

var pg_fb_fan = false;
var pg_fb_email = false;
var pg_fb_stream = false;
var pg_fb_book = false;
var isCanvas = false;

function fbInit(pAsSwfName,pApi_key,pPageId,pCanvas, pLang){
	as_app_name = pAsSwfName;
	apiKey = pApi_key;
	fb_page_id = pPageId;
	isCanvas = pCanvas;
	window.fbAsyncInit = function() {
		FB.init({appId: apiKey, status: true, cookie: true,
				 xfbml: true});
		if(isCanvas){
			checkSession();
		}
	};
	(function() {
	var e = document.createElement('script'); e.async = true;
	e.src = document.location.protocol +
	  '//connect.facebook.net/'+ pLang +'/all.js';
	document.getElementById('fb-root').appendChild(e);
	}());
}

function checkFbApi(){
	if(FB != null){
		if(FB.getLoginStatus != null){
			return "ready";
		}
	}
	return "notready";
}

function fb_js_login(){
	FB.login(function(response) {
		if(response.session){
			fb_session = response;
		}
		flashCallBack('onLogInCallBack', response );
	}, {perms:'email'});
}

function fb_js_getSession(){
	FB.getLoginStatus(function(response) {
		if(response.session){
			fb_session = response;
		}
		flashCallBack('onLogInCallBack', response );
	});
}

function fb_js_me(){
	FB.api('/me',function(response) {
		flashCallBack('onUserInfo', response );
	});
}

function fb_js_friends(){
	FB.api('/me/friends',function(response) {
		flashCallBack('onUserFriends', response );
	});
}

function fb_js_book(){
	FB.api(
	  {
		method: 'fql.query',
		query: 'SELECT bookmarked, email, publish_stream FROM permissions WHERE uid=' + fb_session.session.uid
	  },function(response) {
		flashCallBack('onUserBook', response );
	});
}

function fb_js_friendsplus(){
	FB.api(
	  {
		method: 'fql.query',
		query: 'SELECT uid,first_name,last_name,pic_square,pic_square_with_logo,is_app_user,online_presence FROM user WHERE uid IN ( SELECT uid2 FROM friend WHERE uid1=' + fb_session.session.uid + ' )'
	  },function(response) {
		flashCallBack('onUserFriendsPlus', response );
	});

		
}

function publishFeedAuto(pMessage,pAttachment,pActionLink){
	var publish = {
	  method: 'stream.publish',
	  message: pMessage,
	  attachment: pAttachment,
	  action_links:pActionLink
	};
	FB.api(publish,function(response) { });
}

function publishFeed(pMessage,pAttachment,pActionLink){
	var publish = {
	  method: 'stream.publish',
	  display: 'popup', // force popup mode
	  message: pMessage,
	  attachment: pAttachment,
	  action_links:pActionLink
	};
	FB.ui(publish,function(response) { });
}

function flashCallBack ( cb ) {
	try{
		if( arguments.length > 1 ){
			document[as_app_name]["fbCallback"]( cb, Array.prototype.slice.call(arguments).slice(1)[0]);
		}else{
			document[as_app_name]["fbCallback"]( cb );
		}
	}
	catch(e){
	}
}

function checkSession(){
	FB.getLoginStatus(function(response) {
		if(response.session){
			fb_session = response;
			//checkFB_All();
		}
	});
}

function checkFB_All(){
	FB.api(
	  {
		method: 'fql.query',
		query:'SELECT email, publish_stream, bookmarked FROM permissions WHERE uid=' + fb_session.session.uid
	  }, function(result){
			if(result != null){
				if(result[0] != undefined){
					var row = result[0];
					if(row.email == 1){
						pg_fb_email = true;
					}else{
						pg_fb_email = false;
					}
					if(row.publish_stream == 1){
						pg_fb_stream = true;
					}else{
						pg_fb_stream = false;
					}
					if(row.bookmarked == 1){
						pg_fb_book = true;
					}else{
						pg_fb_book = false;
					}
				}
			}
			FB.api(
			  {
				method: 'fql.query',
				query:'SELECT type FROM page_fan WHERE page_id=' + fb_page_id + ' AND uid=' + fb_session.session.uid
			  }, function(result){
					if(result != null){
						if(result[0] != undefined){
							pg_fb_fan = true;
						}else{
							pg_fb_fan = false;
						}
					}else{					
						pg_fb_fan = false;
					}
					var total = 1;
					if(pg_fb_fan){
						total++;
					}
					if(pg_fb_stream){
						total++;
					}
					if(pg_fb_email){
						total++;
					}
					if(pg_fb_book){
						total++;
					}
					pg_generateBar(total);
			});
	});
}

function fb_open_book(){	
	FB.ui({ 
		method: 'bookmark.add'//,display: 'popup'
	},function(result) {
		checkFB_All();
	});
}

function fb_open_mail(){
	FB.login(function(response) {
		if(response.session){
			checkFB_All();
		}
	}, {perms:'email'});
}

function fb_open_stream(){
	FB.login(function(response) {
		if(response.session){
			checkFB_All();
		}
	}, {perms:'publish_stream'});
}