// JavaScript Document



function sendFbAction(msg){
	swfobject.getObjectById("flashcontent").sendFbAction(msg);
}
function sendFbResponse(graphObjName,response){
	swfobject.getObjectById("flashcontent").sendFbResponse(graphObjName,response);
}



/*----------------------------------------------------------------------------------*/
//初始化FB
/*----------------------------------------------------------------------------------*/
function initFb(appId){
	window.fbAsyncInit = function() {
	FB.init({
	appId  : appId,
	status : true, // check login status
	cookie : true, // enable cookies to allow the server to access the session
	xfbml  : true  // parse XFBML
	});
	};
	
	(function() {
	var e = document.createElement('script');
	e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
	e.async = true;
	document.getElementById('fb-root').appendChild(e);
	getLoginStatus();
	}());
}


/*----------------------------------------------------------------------------------*/
//FB.login
/*----------------------------------------------------------------------------------*/
//FB登入
function fbLogin(permissions){
	FB.login(function(response) {
	  if (response.session) {
		sendFbAction("LOGGED");
		if (response.perms) {
		  // user is logged in and granted some permissions.
		  // perms is a comma separated list of granted permissions
		} else {
		  // user is logged in, but did not grant any permissions
		}
	  } else {
		// user is not logged in
		sendFbAction("CANCEL_LOGGED");
	  }
	}, {perms:permissions});
}
//取得FB登入狀態
function getLoginStatus(){
	FB.getLoginStatus(function(response) {
	  if (response.session) {
		// logged in and connected user, someone you know
		//sendFbAction("LOGGED");
		//sendFbResponse("LOGGED",response);
	  } else {
		// no user session available, someone you dont know
		//sendFbAction("NOT_LOGGED_IN");
		//sendFbResponse("NOT_LOGGED_IN",response);		
	  }
	});
}





/*----------------------------------------------------------------------------------*/
//Graph API 取資料
/*----------------------------------------------------------------------------------*/
function getUser(){
	FB.api('/me', function(response) {
		sendFbResponse("USER",response);
	});
};


function getFriends(){
	FB.api('/me/friends', function(response) {
		sendFbResponse("FRIENDS",response);
	});
};




function sendPost(msg,picPath,linkStr,nameStr,captionStr,descriptionStr,actText,actLink){
	FB.api('/me/feed', 'post', { message:msg,picture:picPath,link:linkStr,name:nameStr,caption:captionStr,description:descriptionStr,actions:{name:actText,link:actLink}}, function (response) {
		if (!response || response.error) {
			//alert('Post Error : ' + response.error.message);
			sendFbAction(response.error.message);
		} else {
			//alert('Post Success');
			sendFbAction("POST_SUCCESS");
		}
	});
};
