function publish (score, msg_id) {
	body = '';
	if(msg_id == '151177074975') {
		message = 'Scrivi qualcosa';
		return_url = 'http://cellulare.blinkogold.it/iqtestfb/?77tadunit=38d11eed3&zz=523';
		title = 'Ho totalizzato ' + score + ' al test di intelligenza';
		caption = '{*actor*} ha sfidato i suoi amici al test di intelligenza e il suo risultato è ' + score + '.';
		link_text = 'Prova a batterlo!';
		stripped_body = body.replace(/(<([^>]+)>)/ig," "); 
	}
	else {
		message = 'Scrivi qualcosa';
		return_url = 'http://cellulare.blinkogold.it/iqtestfb/?77tadunit=e25570d3&zz=516';
		title = 'Ho accettato la sfida al test di intelligenza';
		caption = '{*actor*} ha sfidato i suoi amici al test di intelligenza!';
		link_text = 'Sfidalo anche tu!';
		stripped_body = body.replace(/(<([^>]+)>)/ig," "); 
	}

	//creazione dell'attachment
	attachment = {
	'name': title,
	'href': return_url,
	'media': [{ 
	  'type': 'image',
	  'src': 'http://cellulare.blinkogold.it/iqtestfb/images/test_intelligenza.gif',
	  'href': return_url
	  }],
	'caption': caption,
	'description': stripped_body
	};

	//link che compaiono sulla notifica in basso di fianco a "mi piace" e "commenta"
	actionlinks = [{'text': link_text, 'href': return_url}];

	FB.ensureInit(function(){
		FB.Connect.streamPublish('', attachment, actionlinks, null, message);
	});
  
	//template_data = {
	//	"score": score,
	//	"images": [{"src":"http://cellulare.blinkogold.it/iqtestfb/images/test_intelligenza.gif","href":"http://cellulare.blinkogold.it/iqtestfb/?77tadunit=e25570d3&zz=516"}]
	//}
	//FB.ensureInit(function(){
	//	FB.Connect.showFeedDialog(msg_id, template_data, null, null,FB.FeedStorySize.full,FB.RequireConnect.promptConnect,null,"Scrivi qualcosa");
	//});
}

function get_unique_index_array(frnds) {
	var index = new Array();
	var gen_number = 0;
	var is_duplicate = false;
	var total_index = 10;
	var counter = 0;
	
	do {
		gen_number = Math.floor(Math.random()*(frnds.length - 2)) + 1;
		for(var i = 0; i < 10; i++)	{
			if(index[i] == gen_number) {
				is_duplicate = true;
			}
			else { is_duplicate = false; }
		}
		if(!is_duplicate) { 
			index[counter] = gen_number;
			counter++;
		}
	} 
	while (counter != total_index);
	
	return index;
}

function update_friends(frnds, ex) {
		
		var index = get_unique_index_array(frnds);
		
		var i = 0;
		if($('#Challanger-1 div.avatar').html() != null) {
			for(i = 0; i < 3; i++) {
				$('#Challanger-' + (i + 1) + ' div.avatar').html('<a href="#" onclick="invite(); return false;"><fb:profile-pic uid="' + frnds[index[i]] + '" linked="false" size="square" /></a>');
				$('#Challanger-' + (i + 1) + ' span.name').html('<a href="#" onclick="invite(); return false;"><fb:name uid="' + frnds[index[i]] + '" capitalize="true" linked="false" firstnameonly="true" /></a>');
				$('#Challanger-' + (i + 1) + ' span.score').html(Math.floor(Math.random()*90)+100);
			}
		}
		else { i = 3; }
		for(var j = 1; j < 8; j++) {
			$('#Bott_ch' + j  + ' div.avatar').html('<a href="#" onclick="invite(); return false;"><fb:profile-pic uid="' + frnds[index[i]] + '" linked="false" size="square" /></a>');
			$('#Bott_ch' + j + ' span.name').html('<a href="#" onclick="invite(); return false;"><fb:name uid="' + frnds[index[i]] + '" capitalize="true" linked="false" firstnameonly="true" /></a>');
			$('#Bott_ch' + j + ' span.score').html(Math.floor(Math.random()*90)+100);
			i++;
		}
		
		FB.XFBML.Host.parseDomTree();
}

function update_user_box() {
	$('#fb-connect-box').html("<div class='avatar'><fb:profile-pic uid='loggedinuser' facebook-logo='false' size='square' linked='false'></fb:profile-pic></div><div class='text'>Ciao, <fb:name uid='loggedinuser' useyou='false' linked='false'></fb:name></div>");
	FB.Facebook.apiClient.friends_get(null, update_friends);
 }

function invite() {
	FB.ensureInit(function() { 
	  var content = "<fb:request-form type='IQ Test' action='" + window.location + "' invite='true' content=\"Ciao, ti scrivo per segnalarti questo test divertente! Prova a battere il mio risultato! <fb:req-choice url='http://cellulare.blinkogold.it/iqtestfb/?77tadunit=904683e7&zz=524' label='Vai al test' />\"><fb:multi-friend-selector actiontext='Invita i tuoi amici' email_invite='false'> </fb:request-form>";
	  var popup = new FB.UI.FBMLPopupDialog('Invita i tuoi amici', content);
	  popup.setContentWidth(600);
	  popup.setContentHeight(550);
	  popup.set_placement(FB.UI.PopupPlacement.center)
	  popup.show();
	});
}

$(document).ready(function(){
	FB_RequireFeatures(["XFBML"], function() {
		FB.Facebook.init('5cbd2759f6572b26faa58725668afd2d', null, {"ifUserConnected" : update_user_box});
	});
});



