var _swfname = "flash";
function openPopup(link){
	newwindow = window.open(link,'popup','resizable=0,height=490,width=770,toolbar=no,scrollbars=yes,location=no,status=no');
	if(window.focus) newwindow.focus();
}
//
function fbinit(){
	FB.init({apiKey:'d195304ad83744b1f43c5eee3a19a76b'});
}
//
function fblogin(){
	FB.getLoginStatus(getLoginStatusHandler);
}
//
function fblogout(){
	FB.logout(handleSessionResponse);
}
//
function fbdisconnect(){
	FB.api({method:'Auth.revokeAuthorization'}, function(response) {clearDisplay();});
}
//
function fbGetUserFriends(){
	FB.api('me/friends?fields=id,name,picture', function(response) {
		flashCallBack('fbGetUserFriendsResult', response);
	});
}
function fbpublish(image,description){
	FB.api('/me/feed','post',{message:'De Standaard gaat 10 dagen op vakantie',picture:image,caption:description,link:'http://www.destandaard.be',description:'',name:'De Standaard Reisspel'},function(response){
		flashCallBack('fbPublished', response);
	});
}
//
function getLoginStatusHandler(response){
	//check of er een sessie is
	if (!response.session || !response.perms || !response.perms.contains('publish_stream')) {
		FB.login(handleSessionResponse,{perms:'publish_stream'});
	}else{
		handleSessionResponse(response);
	}
}
//
//sessie response afhandelen
function handleSessionResponse(response){
	//check of er een sessie is
	if (!response.session) {
		flashCallBack('logout');
		return;
	}
	//user info
	FB.api({
			method: 'fql.query',
			query: 'SELECT id, name, pic_square FROM profile WHERE id=' + FB.getSession().uid
		},
		function(response){
			flashCallBack('loginSucces', response[0]);
		}
	);
}
//alles hiden
function clearDisplay(){
	$('#user-info').hide('fast');
	$('#user-friends').hide('fast');
}
//
function flashCallBack (func){
	if( arguments.length > 1 ){
		document[_swfname][func](Array.prototype.slice.call(arguments).slice(1)[0]);
	}else{
		document[_swfname][func]();
	}
}
