/*document.observe("dom:loaded", function() {
	FB.init("9ef284cd57a32ee6fc888421f2ffa891", "/codebase/js/facebook/xd_receiver.htm", {"forceBrowserPopupForLogin":false, "ifUserConnected":connected, "ifUserNotConnected":notConnected});
	
});*/
document.observe("dom:loaded", function() {
	FB.init({
		appId  : '285300057623', //app id
		status : true, // check login status
		cookie : true, // enable cookies to allow the server to access the session
		xfbml  : true  // parse XFBML
	});
	FB.getLoginStatus(function(response) {
	  if (response.session) {
		// logged in and connected user, someone you know
		//console.debug('logged in and connected user, someone you know');
		$('btn_logout').show();
		$('btn_login').hide();
		$('btn_postToWall').show();
		
		loadDetails(false);
	  } else {
		// no user session available, someone you dont know
		//console.debug('no user session available, someone you dont know');
		//$('btn_login').show();
		//$('btn_logout').hide();
		//$('btn_postToWall').hide();
	  }
	});
});
function loadDetails(autopost){
	FB.api('/me', function(response) {
		//console.debug(response);
		//insert user into database
		updateUser(response);
		if(autopost==true){
			postToWall();
		}
	});	
}
function updateUser(response_obj){
	//console.debug(response_obj);
	ajax_call = new Ajax.Request('/content/campaigns/lm2010/aj_fbookfunc.php?tmp='+Math.random(),{
		method:'post',
		parameters: {'doWhat':'updateUser', 'response':Object.toJSON(response_obj)},
		onSuccess:function(transport){
			if(transport.getHeader('Content-Type') == 'application/json; charset=UTF-8'){
				json_transport = transport.responseJSON;

				if(json_transport['succes']!=undefined){
					//console.debug(json_transport['sql']);
					//$('valentijncontent').update(json_transport['succes']);
				}else{
					//alert(json_transport['error']);
				}
			}
		}
	});		
}
function postToWall(){
	FB.ui({
		method: 'stream.publish',
		message: 'maakt nu kans op een Belvilla vakantiecheque voor een droomvakantie!',
		attachment: {
			name: 'Belvilla Last minute vakantiehuizen',
			caption: '',
			description: (
				'Wil je ook kans maken op een Belvilla vakantiecheque? ' +
				'Klik dan op bovenstaande link, ' +
				'bezoek de Belvilla Last minute actiepagina en volg de instructies onderaan de pagina!'
			),
			href: 'http://www.belvilla.nl/dv/fbnllm/vakantiehuis-lastminute'
		},
		action_links: [
			{ text: 'Belvilla Fan Page', href: 'http://www.facebook.com/belvilla.vakantiehuizen' }
		],
		user_message_prompt: 'Stel je vrienden op de hoogte van de Belvilla last minutes'
	},
	function(response) {
		if (response && response.post_id) {
			//console.debug(response);
			registerPost(response);
		} else {
			//alert('Post was not published.');
		}
	});
}
function registerPost(response_obj){
	ajax_call = new Ajax.Request('/content/campaigns/lm2010/aj_fbookfunc.php?tmp='+Math.random(),{
		method:'post',
		parameters: {'doWhat':'registerPost', 'response':Object.toJSON(response_obj)},
		onSuccess:function(transport){
			if(transport.getHeader('Content-Type') == 'application/json; charset=UTF-8'){
				json_transport = transport.responseJSON;

				if(json_transport['succes']!=undefined){
					//console.debug(json_transport['sql']);
					//$('valentijncontent').update(json_transport['succes']);
				}else{
					//alert(json_transport['error']);
				}
			}
		}
	});		
}
function login(){
	FB.login(function(response) {
		if (response.session) {
			if (response.perms) {
				// user is logged in and granted some permissions.
				// perms is a comma separated list of granted permissions
				//console.debug('user is logged in and granted some permissions.');
				//console.debug(response.perms);
				
				$('btn_postToWall').show();
				$('btn_logout').show();
				$('btn_login').hide();
				
				loadDetails(true);
			} else {
				// user is logged in, but did not grant any permissions
				//console.debug('user is logged in, but did not grant any permissions');
				$('btn_login').show();
				$('btn_logout').hide();
				$('btn_postToWall').hide();
			}
		} else {
			// user is not logged in
			//console.debug('user is not logged in');
			$('btn_login').show();
			$('btn_logout').hide();
			$('btn_postToWall').hide();
		}
	}, {perms:'email,user_birthday,read_stream,publish_stream,offline_access'});//read_friendlists,
}
function logout(){
	FB.logout(function(response) {
		// user is now logged out
		//console.debug(response);
		$('btn_login').show();
		$('btn_logout').hide();
		$('btn_postToWall').hide();
	});	
}

