
$(document).ready(function() {
//on initialise la connection a l'appli facebook
// FB.init({appId: '195470180499951', status: true, cookie: true, xfbml: true}); 
FB.init({appId: '202901166429920', status: true, cookie: true, xfbml: true}); 


// verificiation du statut the status on load
FB.getLoginStatus(handleSessionResponse);
	$('.btnfacebookimg').bind('click', function() {
		//var $id=$(this).find('input').val();	
		//
	
	postOnFacebook();
     
		/*$nb_actuel=$(this).find('span').html();
		$new_nb=parseInt($nb_actuel)+1;
		$(this).find('span').html($new_nb);
		
		$.ajax({
		  url: 'ajax_facebook.php',
		  data: 'num_fiche='+$id+"&action=count&count="+$new_nb,
		  success: function(data){
	  		}
		});*/
		return false;
	});

});

/**
 * Permet de poster un message sur facebook
 */
function postOnFacebook(){	
	var params = {};
	params['link'] = document.location.href; /* un lien partager */
	params['name'] = '';
	$img=$('img').attr('src');
	params['picture'] = 'http://drinkolor.com/v3/'+$img; 
	params['name'] = $('h1 cufon').attr('alt');
	



var $desc;
		  $('article p cufontext').each(function(index) {
		$new_desc=$(this).html();
		$desc=$desc+$new_desc;
		
     });
		$desc=$desc.replace('undefined','');
				params['description'] =  $desc;
	params['url'] = 'http://url.pour.le.j.aime'; /* like !*/													
	var callback = function(response){
		//on se logue si pas deja fait
		if (!response.session){
			console.info('not logged in');
			return;
		}
		//on fait une reque pour recuperer les informations de l'utilisateur
		FB.api(
			{
				method: 'fql.query',
				query: 'SELECT id, name, username, pic, pic_square, url, type FROM profile WHERE id=' + FB.getSession().uid
			},
			 function(response) {
				var user = response[0];
				//nameUser = user.name;
				 params['message'] = "Viens decouvrir le portfolio de Niels Saint-Viteux.";
				//getFirstName(nameUser)+
				//+typemetier[idfiche-1]
				//getFirstName(nameUser)+params_message1+params_message2+params_message3+typemetier[idfiche-1]+". "+params_message4;
				//on se connecte a l'api pour poster
				
				FB.api('/me/feed', 'post', params, function(response) {
					//si erreur
					if (!response || response.error) {
						var msg = response ? response.error.type+' - '+response.error.message : '';
						console.log('Error occured: '+msg);
					} 
					//sinon
					else {
						console.log('Post ID: ' + response.id);
					}
				});
			}
		);

	}

	FB.login(callback, {perms:'publish_stream'});

};

/**
 * Fonction qui recupere le prenom de la personne
 */
function getFirstName(stringname) {
	var positionspace		= stringname.indexOf(" ",0);
	if(positionspace!=-1)
		return stringname.substring(0, positionspace);
	else
		return stringname;
}
	  
/**
Fonction habituel de test equivalent print_r php
*/
function print_r(obj) {
  win_print_r = window.open('about:blank', 'win_print_r');
  win_print_r.document.write('<html><body>');
  r_print_r(obj, win_print_r);
  win_print_r.document.write('</body></html>');
}

/**
Fonction habituel de test equivalent print_r php suite
*/
function r_print_r(theObj, win_print_r) {
	if(theObj.constructor == Array ||
		theObj.constructor == Object){
	if (win_print_r == null)
		win_print_r = window.open('about:blank', 'win_print_r');
	}
	for(var p in theObj){
		if(theObj[p].constructor == Array||
			theObj[p].constructor == Object){
			win_print_r.document.write("<li>["+p+"] =>"+typeof(theObj)+"</li>");
			win_print_r.document.write("<ul>")
			r_print_r(theObj[p], win_print_r);
			win_print_r.document.write("</ul>")
		} else {
			win_print_r.document.write("<li>["+p+"] =>"+theObj[p]+"</li>");
		}
	}
	  win_print_r.document.write("</ul>")
}

// handle a session response from any of the auth related calls
function handleSessionResponse(response) {
        // if we dont have a session, just hide the user info
    if (!response.session) {
		return;
	}
// if we have a session, query for the user's profile picture and name      
}
