       


/**
Funzione che mostra il contenuto di un Elemento (div,span,..., ecc)
@parm id l'id dell'elemento da visualizzare
**/
function showElement(id){
    var elem = document.getElementById(id);
    if (elem != null) {
	    elem.style.display = "block";
	}
}

/**
Funzione che nasconde il contenuto di un Elemento (div,span,..., ecc)
@parm id l'id dell'elemento da nascondere
**/
function hideElement(id){
var elem = document.getElementById(id);
 if (elem != null) {
	elem.style.display = "none";
	}
}
   

/**
Funzione che provvede a nascondere il tasto di Login di Facebook
**/
function fb_hide_login_button(){
 hideElement("fbLogin");
}

/**
Funzione che provvede a mostrare il tasto di Login di Facebook
**/
function fb_show_login_button(){
 showElement("fbLogin");
}


/**
Funzione che provevde a nascondere il tasto di Logout di Facebook
**/
function fb_hide_logout_button() {
 hideElement("fbLogout");
}

/**
Funzione che provvade a mostrare il tasto di Logout di Facebook
**/
function fb_show_logout_button() {
 showElement("fbLogout");
}

/**
Funzione che cancella il contenuto degli input text della form 
**/
function fb_clear_fields(){

   if (document.getElementById('nomeCommento') != null )
	   document.getElementById('nomeCommento').value = "";
   if (document.getElementById('emailCommento') != null)
           document.getElementById('emailCommento').value = "";
   if (document.getElementById('textCommento') != null)
           document.getElementById('textCommento').value = "";


   if (document.getElementById("nomeCommento") != null)
       document.getElementById("nomeCommento").readOnly = false;

   if (document.getElementById("emailCommento") != null)
       document.getElementById("emailCommento").readOnly = false;
}

/**
Funzione che cancella il messggio della box dei commenti
**/
function fb_clear_comment_message(){
   if(document.getElementById("textCommento") != null){
        document.getElementById("textCommento").value = "";
    }
}


/**
Funzione che riempe i valori delle credenziali dell'utente recuperate dal profilo di Facebook e li inserisce nella form dei commenti
**/
function fillForm(nomeUtente,cognomeUtente,emailUtente){
              
  if (document.getElementById('nomeCommento') != null ) {
      document.getElementById('nomeCommento').value = nomeUtente+" "+ cognomeUtente;
      document.getElementById("nomeCommento").readOnly = true;
}

 if ( document.getElementById('emailCommento') != null) {
   document.getElementById('emailCommento').value = emailUtente;
   document.getElementById("emailCommento").readOnly = true;
  }

}


/**
Funzione che aggiorna il messaggio dello stato del commento inserito
@param msg il messaggio dello stato da visualizzare 
**/
function update_comment_status(msg) {

  var divStatusComemnt = document.getElementById("fbStatusComment");
  if (divStatusComemnt != null) {
     if (msg != null){
        divStatusComemnt.innerHTML= msg;
        
      } else {
 	 divStatusComemnt.innerHTML= "Il commento <font color=#808000> &egrave; stato inviato al moderatore </font>";
        }
   }
}



/**
Funzione di callback del Facebook Connect
**/
function auth_using_fb() {

 // nascondo il tasto di login
  fb_hide_login_button();

  // interrogazione dei dati dal profilo Facebook dell'utente loggato
  var viewer  =  FB.Data.query(
     		'SELECT name, pic_square_with_logo,profile_url, email,first_name,last_name  FROM user WHERE uid='+response.session.uid,
     			 function(results) {
        			update_userbox( results[0].name,
                                                results[0].pic_square_with_logo,
                                                results[0].profile_url,
                                                results[0].email,
                                                results[0].first_name,
                                                results[0].last_name)
                       
        		fillForm(results[0].first_name,results[0].last_name, results[0].email);
                       }
  
                 );
  
}


/**
user_loggedIn
*/
function user_loggedIn(response){
  
     if (response.session) {
	// nascondo il tasto di login
	fb_hide_login_button();
	
        // interrogazione dei dati dal profilo Facebook dell'utente loggato
       var sql = 'SELECT name, pic_square_with_logo,profile_url, email,first_name,last_name  FROM user WHERE uid='+response.session.uid;
       var query = FB.Data.query(sql);
               query.wait(function(rows) {
       
                  update_userbox( rows[0].name,
                        rows[0].pic_square_with_logo,
                        rows[0].profile_url,
                        rows[0].email,
                        rows[0].first_name,
                        rows[0].last_name
                        )
                fillForm(rows[0].first_name,rows[0].last_name, rows[0].email)
             });
     }

}


/**
user_not_loggedIn
*/
function user_not_loggedIn(response){

}

/**
funzione che provvede ad popolare le informazioni reperite dal profilo di Facebook
@param name Nome utente composto da nome e cognome
@param image l'immagine del profilo dell'utente (url)
@param url il link al profilo dell'utente
@param email l'indirizzo e-mail dell'utente
**/
function update_userbox(name, image, url, email) {
	
var divInfoUser = document.getElementById("info_user");
var divLoggedUser = document.getElementById("logged_user");
var divFake = null;


if (divInfoUser != null) {

    name.replace('"','');
	
    divFake = document.getElementById("fakeDiv");
    
    // Controllo se il div che contiene le informazioni del profilo utente sia già presente all'interno del documento   
    if (divFake == null) {

                divFake = document.createElement("div");
                divFake.setAttribute("id","fakeDiv");
      

				// creo l'immagine                                      
				var img = document.createElement("img");
					// creo l'immagine                                      
				var img = document.createElement("img");
				if (image == null) {
                                        var url = document.getElementById("imgUtenteAnonimo").childNodes[0].nodeValue;
                                        
                                        img.src = url;
                                        img.width= "50";
				} else {
					img.src = image;
				}
				img.alt = name;
		
				// creo il link
				var link = document.createElement("a");
				link.href = url;
				link.innerHTML = name;
		
				// div 
				var divFromFacebook = document.createElement("div");
				divFromFacebook.innerHTML = "da Facebook";
		
				// div clear
				var divClear = document.createElement("div");
				divClear.className = "clear";
				
		
				// mostro il tasto di logout
				fb_show_logout_button();
		
				var divLogout = document.getElementById("fbLogout");
		
				// creo la struttura
				divFake.appendChild(img);
				divFake.appendChild(link);
				divFake.appendChild(divFromFacebook);
				divFake.appendChild(divClear);
		
				divFake.appendChild(divLogout);
				divInfoUser.appendChild(divFake); 
		
				//divLoggedUser.removeChild(divLogout);
				
				divInfoUser.style.display = "block";
		
         } else {
             // nop il div fake è già  presente
            }

	
} else {
  // alert("Errore: il div divInfoUser è nullo");
 // nop
 }
}


/**
Funzione che provevde alla pubblicazione del commento inserito dall'utente con alcune informazioni aggiuntive quali:
immagine dell'articolo commentato, il link all'articolo, il testo del titolo dell'articolo. Tali informazioni verranno pubblicate sulla bacheca dell'utente e saranno visibili agli amici dell'utente.
@param msg messaggio che l'utenteintende pubblicare.
@param imgSrc url dell'immagine dell'articolo
@param urlLink url dell'articolo.

**/
function callPublish(msg, imgSrc,urlLink, title, abstract,nome) {



FB.getLoginStatus(function(response) {
  if (response.session) {


 var att = { 
 	                'name': nome, 
 			'href': urlLink, 
 			'caption': title, 
 			'description': abstract, 	
 			'media': [{'type':'image',
                                   'src':imgSrc,
                                   'href':urlLink
                                 }]
                  };

var actionLinks = [{ "text": "Continua a leggere", "href": urlLink }];
	
FB.ui(
   {
     method: 'stream.publish',
     message:msg,
     attachment:att,
     action_links: actionLinks
   },
   function(response) {
     if (response && response.post_id) {
      update_comment_status();
     } else {
       update_comment_status("Errore durante la fase di pubblicazione del messaggio su facebook");
     }
   }
 );

  }
});       
}
