var fbuser=null;
var debug={};
jQuery(document).ready(function(){
	window.fbAsyncInit = function() {
		FB.init({
			appId  : API_KEY,
			status : true, // check login status
			cookie : true, // enable cookies to allow the server to access the session
			xfbml  : true  // parse XFBML
		});
		FB.Event.subscribe('auth.login', function(response) {fb_login();});
		fb_login();
	}
	jQuery('body').append('<div id="fb-root"></div>'+'<scr'+'ipt src="http://connect.facebook.net/da_DK/all.js"></scr'+'ipt>');	
});

function fb_logout(){
	FB.logout(function(response) {
		update_userbox(false);
	});	
	return false;
}

function fb_login(){
	FB.getLoginStatus(function(response) {
	  if (response.session) {
		FB.api('/me', function(response) {
		  update_userbox(response.name,"http://graph.facebook.com/"+response.id+"/picture",response.link);
		});
	  } else {
		update_userbox(false);
	  }
	});	
}

function fb_permissions(func){
	if ( fbuser.permissions != null ) { func();return true; }
	FB.login(function(response) {
	  if (response.session) {
		if (response.perms) {
			fbuser.permissions=response.perms;
			func();
			return true;
		} else {
			return false;
		}
	  } else {
		return false;
	  }
	}, {perms:'publish_stream'});	
}

function tellUser(what,response){
	//todo make nicer :P
	if (typeof(response)!='undefined'){
		what+=' '+response.error.message;
	}
	alert(what);
}

function fb_like(post,e){
	fb_permissions(function(){	
		FB.api('/'+post+'/likes','post',{ 'url':'https://graph.facebook.com/'+post+'/likes'} ,function(response){
			if(!response|| response.error){
				tellUser('likeError',response);
				debug.likeError=response;
			}else{
				jQuery(e).fadeOut();
			}
		});
	});
	return false;//do not follow link
}

function fb_comment(post,e,msg){
	fb_permissions(function(){	
		FB.api('/'+post+'/comments','post',{ 'msg':msg} ,function(response){
			if(!response|| response.error){
				tellUser('commentError',response);
				debug.commentError=response;
			}else{
				jQuery(e).fadeOut();
			}
		});
	});
	return false;//do not follow link	
}

function update_userbox(name,image,profile){
	if ( name == false ){
		fbuser=null;
		jQuery(".fblogout").hide();
		jQuery(".fbconnect_login_button").fadeIn("slow");
		if(typeof(this.window["user_FBlogin"])=="function"){
			user_FBlogin(fbuser);
		}
		return;
	}
	fbuser={"name":name,"image":image,"profile":profile,"permissions":null};
	jQuery(".fbconnect_login_button").hide();
	if(typeof(this.window["user_FBlogin"])=="function"){
		jQuery(".fblogout").fadeIn("slow",function(){
			user_FBlogin(fbuser)
		});
	}else{
		jQuery(".fblogout").fadeIn("slow");
	}
}

function showVideo(video){
	video = video.replace('/v/','/embed/').replace('&autoplay=','?autoplay=');
	
	jQuery('body').append('<div id="video"><iframe class="youtube-player" type="text/html" width="480" height="385" src="'+video+'" frameborder="0"></iframe></div>');
	jQuery('#video')
		.css({
			'position'	:'absolute',
			'left'		: (jQuery('.fbwall').offset().left + ((jQuery('.fbwall').width()-480)/2))+'px' ,
			'top'		: (jQuery(document).scrollTop()+ ((jQuery(window).height()-385)/2))+'px'
			})
		.expose({
			onBeforeClose: function(){
							jQuery('#video').remove();
						}
			});
	
}


function fb_popupdialog(message,callback){	 
	FB.ui(
		{
			method:'fbml.dialog',
			display: 'dialog',
			fbml: (message),
			width: '300px',
			height: '200px'
		},
		function(response) {
			callback(response);
			//fbDialogDismiss();
		}
	);
}

/*
  FB.ui(
   {
     method: 'stream.publish',
     message: 'getting educated about Facebook Connect',
     attachment: {
       name: 'Connect',
       caption: 'The Facebook Connect JavaScript SDK',
       description: (
         'A small JavaScript library that allows you to harness ' +
         'the power of Facebook, bringing the user\'s identity, ' +
         'social graph and distribution power to your site.'
       ),
       href: 'http://github.com/facebook/connect-js'
     },
     action_links: [
       { text: 'Code', href: 'http://github.com/facebook/connect-js' }
     ],
     user_message_prompt: 'Share your thoughts about Connect'
   },
   function(response) {
     if (response && response.post_id) {
       alert('Post was published.');
     } else {
       alert('Post was not published.');
     }
   }
 );
 * 
function out(c){
    console.log(c);
}

fb_popupdialog('<fb:comments xid="444543446976" canpost="true" candelete="false" > <fb:title>Kommenter indlxg</fb:title</fb:comments>',out);

 
 */

