function facebook_onlogin(){

	check_fb_user();
	window.location=window.location;
}

function facebook_onlogout(){
	alert('logout ok');
}


// -----------------------
function facebook_onload(already_logged_into_facebook) {
  // user state is either: has a session, or does not.
  // if the state has changed, detect that and reload.
  FB.ensureInit(function() {
      FB.Facebook.get_sessionState().waitUntilReady(function(session) {
          var is_now_logged_into_facebook = session ? true : false;

          // if the new state is the same as the old (i.e., nothing changed)
          // then do nothing
          if (is_now_logged_into_facebook == already_logged_into_facebook) {
            return;
          }

          // otherwise, refresh to pick up the state change
          //refresh_page();
          //window.location = window.location;
        });
    });
}

function fb_logged_in(){
	check_fb_user();
	var user_box = document.getElementById('fb_user');
	user_box.innerHTML = '';
	//user_box.innerHTML = '<span><!--<fb:name uid="loggedinuser" useyou="false"></fb:name><br /><fb:login-button autologoutlink="true" length="long" size="small" onlogout="facebook_onlogout();"></fb:login-button>--></span>';
	FB.XFBML.Host.parseDomTree();

	
}
function fb_logged_out(){
	var user_box = document.getElementById('fb_user');	
	user_box.innerHTML = '<fb:login-button onlogin="facebook_onlogin();" size="medium"></fb:login-button>';
	FB.XFBML.Host.parseDomTree();
}
function check_fb_user(){
	var XHR = new XHRConnection();
	XHR.setRefreshArea('fbd');
	XHR.appendData("go", '1');
	XHR.sendAndLoad("../../facebook/ajax/ajax_fb_check_user.php", "POST");
	return true;
	
}

/**
 * Fenetre modal pour la recup du mail
 */
var fbMailReturn = function (obj){
	if(obj.responseText == 'ok'){
		ModalPopups.Close("idIndicator1");
	}
	else{
		document.getElementById('div_retour_fbmail').innerHTML = obj.responseText;
	}
}
function ModalPopupsIndicator(path,lang){
	var titleAffich=textAffich="";
	if (lang=="fr")
	{
		titleAffich="";
		textAffich="<div id='titlePrint' style=\"margin-bottom:20px; border:solid;\">Pour finaliser votre inscription, veuillez choisir votre pseudo <br />"+
		"et renseigner l\’adresse email sur laquelle vous souhaitez \être inform\é <br /> "+
		"des actualit\és des projets que vous soutiendrez.</div><div id=\"div_fb_pseudo\" style=\"margin-left:20px;overflow:auto;\">" + 
		"<input type=\"text\" name=\"fbpseudo\" id=\"fbpseudo\" value=\"Pseudo\" size=\"50px;\" height=\"150px;\"/><br /></div>"+
		"<div id=\"div_fb_mail\" style=\"margin-left:20px;overflow:auto;\">Adresse mail :<br />" + 
		"<input type=\"text\" name=\"fbmail\" id=\"fbmail\" value=\"\" size=\"50px;\" /><br />"+
		"<input type=\"submit\" name=\"bt_fb_mail\" value=\"Valider\" style=\"margin-left:40%;margin-top:5%;overflow:auto;\" onclick=\"ajax_save_mail('"+path+"');\"/></div>"+
		"<div id=\"div_retour_fbmail\" style=\"margin-left:40px;\"></div>";
	}else if (lang=="en")
	{
		titleAffich="In order to complete your registration, please choose a username <br />"+
		"and provide us with the email address we will use to keep you informed.";
		textAffich="<div id=\"div_fb_pseudo\" style=\"margin-left:20px;overflow:auto;\">Pseudo :<br />" + 
		"<input type=\"text\" name=\"fbpseudo\" id=\"fbpseudo\" value=\"\" size=\"25\" /><br /></div>"+
		"<div id=\"div_fb_mail\" style=\"margin-left:20px;overflow:auto;\">E-mail :<br />" + 
		"<input type=\"text\" name=\"fbmail\" id=\"fbmail\" value=\"\" size=\"25\" /><br />"+
		"<input type=\"submit\" name=\"bt_fb_mail\" value=\"Valider\" onclick=\"ajax_save_mail('"+path+"'); \"/></div>";
	}
	ModalPopups.Indicator("idIndicator1","",textAffich
		, 
		{
			width: 380,
			height: 350,
			backgroundColor: "black",
            shadowSize: 15,
            titleBackColor: "white",
            titleFontColor: "Black",
            popupBackColor: "white",
            popupFontColor: "black",
            footerBackColor: "white",
            footerFontColor: "black",
			fontStyle:"arial"
		}
	);
}

function ajax_save_mail(path){
	var XHR = new XHRConnection();
	XHR.setRefreshArea('div_retour_fbmail');
	XHR.appendData("pseudo", document.getElementById('fbpseudo').value);
	XHR.appendData("mail", document.getElementById('fbmail').value);
	XHR.sendAndLoad(path+"facebook/ajax/ajax_save_mail.php", "POST",fbMailReturn);
	return true;
}
// --------------------------------------------------
/**
 * Deconnexion
 */
var isDeco = function (obj) {
	if(obj.responseText == 'ok'){
		//if(FB.Connect.get_loggedInUser()!=null){
			//FB.Connect.logoutAndRedirect('http://www.touscoprod.com/');
		//}
		//else{
			window.location='http://www.touscoprod.com/';
			//jQuery.facebox({ 'Vous êtes bien déconnecté' })
		//}
	}
}
/*
function ajax_deco(){
	var XHR = new XHRConnection();
	XHR.setRefreshArea('compte');
	XHR.appendData("s_action", 'deconnexion');
	XHR.appendData("s_redirection", '<? echo $url_redirect_code;?>');
	XHR.sendAndLoad("../../pages/identification/deconnexion.php", "GET",isDeco);
	return true;
}*/
function ajax_deco(){
	var XHR = new XHRConnection();
	//XHR.setRefreshArea('fbd');
	XHR.appendData("s_action", 'deconnexion');
	XHR.appendData("s_redirection", '<? echo $url_redirect_code;?>');
	XHR.sendAndLoad("../../pages/identification/deconnexion.php", "GET", isDeco );
	return true;
}

/**
 * Permet de poster un commentaire (dans le forum) en ajax
 *
 */
function ajax_post_comment(id_topic,comment){
	var XHR = new XHRConnection();
	XHR.setRefreshArea('iframe_projet');
	XHR.appendData("id_topic", id_topic);
	XHR.appendData("comment", document.getElementById('s_commentaire').value);
	XHR.sendAndLoad("ajax_post_forum_projet.php", "POST",document.getElementById('s_commentaire').value='');
}

function ajax_load_comment(id_topic){
	var XHR = new XHRConnection();
	XHR.setRefreshArea('iframe_projet');
	XHR.appendData("id_topic", id_topic);
	XHR.appendData("comment", '0');
	XHR.sendAndLoad("ajax_post_forum_projet.php", "POST");
}

function ajax_delete_comment(id_topic,id_post){
	var XHR = new XHRConnection();
	XHR.setRefreshArea('iframe_projet');
	XHR.appendData("id_topic", id_topic);
	XHR.appendData("id_post_del", id_post);
	XHR.appendData("comment", '0');
	XHR.sendAndLoad("ajax_post_forum_projet.php", "POST");
}

function ajax_delete_comment_mon_compte(id_post,id_user){
	var XHR = new XHRConnection();
	XHR.setRefreshArea('iframe_compte');
	XHR.appendData("id_post_del", id_post);
	XHR.appendData("id_compte", id_user);
	XHR.sendAndLoad("Iframe/mes_post.php", "GET");
}

function ajax_load_comment_actu(id_topic){
	var XHR = new XHRConnection();
	XHR.setRefreshArea('forum_actu');
	XHR.appendData("id_topic", id_topic);
	XHR.appendData("comment", '');
	XHR.sendAndLoad("../projet/ajax_post_forum_actu.php", "POST");
}

function ajax_post_comment_actu(id_topic,comment){
	var XHR = new XHRConnection();
	XHR.setRefreshArea('forum_actu');
	XHR.appendData("id_topic", id_topic);
	XHR.appendData("comment", document.getElementById('s_commentaire').value);
	XHR.sendAndLoad("../projet/ajax_post_forum_actu.php", "POST",document.getElementById('s_commentaire').value='');
}

function ajax_delete_comment_actu(id_topic,id_post){
	var XHR = new XHRConnection();
	XHR.setRefreshArea('forum_actu');
	XHR.appendData("id_topic", id_topic);
	XHR.appendData("id_post_del", id_post);
	XHR.appendData("comment", '');
	XHR.sendAndLoad("../projet/ajax_post_forum_actu.php", "POST");
}

/**
 * Systeme de suivi des utilisateurs
 */
function ajout_ami(id_suivi,zone){
	var XHR = new XHRConnection();
	XHR.setRefreshArea(zone);
	XHR.appendData("id_suivi", id_suivi);
	XHR.appendData("s_id_ami", id_suivi);
	XHR.sendAndLoad("ajax_ajoute_ami.php", "POST");
	return true;
}

function supp_ami(id_suivi,zone){
	var XHR = new XHRConnection();
	XHR.setRefreshArea(zone);
	XHR.appendData("id_suivi", id_suivi);
	XHR.appendData("s_id_ami", id_suivi);
	XHR.sendAndLoad("ajax_supprime_ami.php", "POST");
	return true;
}

function liste_follow(path,id_user,type_follow){
	var XHR = new XHRConnection();
	if(type_follow=='follower'){
		XHR.setRefreshArea('aff_follower');
	}
	else if(type_follow=='following'){
		XHR.setRefreshArea('aff_following');
	}
	document.getElementById('aff_all_'+type_follow).innerHTML = '';
	XHR.appendData("type_follow", type_follow);
	XHR.appendData("id_user", id_user);
	XHR.sendAndLoad(path+"pages/moncompte/amis_liste.php", "GET");
	return true;
}

function myspace_share(T, C, U, L)
{
    var targetUrl = 'http://www.myspace.com/index.cfm?fuseaction=postto&' + 't=' + encodeURIComponent(T)
    + '&c=' + encodeURIComponent(C) + '&u=' + encodeURIComponent(U) + '&l=' + L;
    window.open(targetUrl);
}