
/*
 * The facebook_onload statement is printed out in the PHP. If the user's logged in
 * status has changed since the last page load, then refresh the page to pick up
 * the change.
 *
 * This helps enforce the concept of "single sign on", so that if a user is signed into
 * Facebook when they visit your site, they will be automatically logged in -
 * without any need to click the login button.
 *
 * @param already_logged_into_facebook  reports whether the server thinks the user
 *                                      is logged in, based on their cookies
 *
 */
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.
  return;
  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();
        });
    });
}
function explodeUrl(url) {
	var dati = new Array();
	var coppie = url.split("&");
	var keyvalue;
	for (i=0; i<coppie.length; i++) {
		keyvalue = coppie[i].split('=');
		dati[i] = unescape(keyvalue[1]);
	}
	return dati;
}
/*
 * Our <fb:login-button> specifies this function in its onlogin attribute,
 * which is triggered after the user authenticates the app in the Connect
 * dialog and the Facebook session has been set in the cookies.
 */
 function facebook_onlogin_ready() {
  // In this app, we redirect the user back to index.php. The server will read
  // the cookie and see that the user is logged in, and will deliver a new page
  // with content appropriate for a logged-in user.
  //
  // However, a more complex app could use this function to do AJAX calls
  // and/or in-place replacement of page contents to avoid a full page refresh.
	var liveReq = false;

	return;
	var cmp = document.getElementById('pnickname');
	if (cmp) {
		startWaiting("connessione in corso",cmp);
		if (window.XMLHttpRequest)
			liveReq = new XMLHttpRequest();
		else if (window.ActiveXObject)
			liveReq = new ActiveXObject("Microsoft.XMLHTTP");
		if (liveReq && liveReq.readyState < 4)
			liveReq.abort();
		liveReq.onreadystatechange = function () {
			if (liveReq.readyState == 4) {
				stopWaiting();
				var dati  = explodeUrl(liveReq.responseText);
				cmp.innerHTML = 'Il tuo nickname: <input maxlength="30" type="text" name="nickname" size="24" value="'+dati[1]+'"/>'; 
				cmp = document.getElementById('publish_fb_checkbox');
				if (cmp)  {
					cmp.innerHTML = "<img src=\"http://static.ak.fbcdn.net/images/icons/favicon.gif\" />"+
									"<input type=\"checkbox\" name=\"publish_to_facebook\" id=\"publish_to_facebook\" checked=\"checked\" />"+
									"<label for=\"publish_to_facebook\">Pubblica questo commento su facebook</label>";
				}
			}
		}
		var url = "getname.php?campo=first_name";
		//alert(url);
		liveReq.open("GET",url);
		liveReq.send(null);
	}
	else  {
		var cmp = document.getElementById('account');
		if (cmp) {
			startWaiting("connessione in corso",cmp);
			if (window.XMLHttpRequest)
				liveReq = new XMLHttpRequest();
			else if (window.ActiveXObject)
				liveReq = new ActiveXObject("Microsoft.XMLHTTP");
			if (liveReq && liveReq.readyState < 4)
				liveReq.abort();
			liveReq.onreadystatechange = function () {
				if (liveReq.readyState == 4) {
					stopWaiting();
					//alert(liveReq.responseText);
					var dati  = explodeUrl(liveReq.responseText);
					cmp.innerHTML = '<div id="header-profilepic">'+
						'<fb:profile-pic class=" fb_profile_pic_rendered FB_ElementReady" uid="'+dati[0]+'" size="square" facebook-logo="true">'+
						'<a class="FB_Link" href="http://www.facebook.com/profile.php?id='+dati[0]+'">'+
						'<img src="http://external.ak.fbcdn.net/safe_image.php?logo&url='+dati[2]+'" alt="'+dati[1]+'" title="'+dati[1]+'" style="" class=" fb_profile_pic_rendered">'+
						'</a>'+
						'</fb:profile-pic>'+
						'</div>'+
						'<div id="header-account"><b>Benvenuto, '+dati[1]+'</b>'+
						'<br>'+
						'<div class="account_links">'+
						'<a href="#" onclick="FB.Connect.logout(function() { facebook_onlogout_ready(); }); return false;">Logout da Facebook</a>'+
						'<br></div></div>';
				}
			}
			var url = "getname.php?campo[]=name&campo[]=pic_square";
			//alert(url);
			liveReq.open("GET",url);
			liveReq.send(null);
		}
		else {
			var cmp = document.getElementById('account2');
			if (cmp) {
				var user_id = FB.Facebook.apiClient.get_session().uid;
				FB.Facebook.apiClient.users_getInfo(user_id,["name","pic_square"],getInfo);
			}
		}
	}
	/*
	else
		refresh_page();
	*/
	return false;
}
function facebook_onlogout_ready() {
	return;
	var cmp = document.getElementById('account');
	if (cmp) {
		cmp.innerHTML = 'Benvenuto su GJM | <a href="#" onclick="FB.Connect.requireSession(function() {facebook_onlogin_ready();}); return false;" >'+
						'<img id="fb_login_image" src="http://static.ak.fbcdn.net/images/fbconnect/login-buttons/connect_light_medium_long.gif" alt="Connect"/>'+
						'</a>';
	}
	else {
		showInfoNotLoggedIn();
	}
}
var strWaiting;
var cmpWaiting;
var counterWating;
var timerWaiting;
function startWaiting(str,cmp) {
	strWaiting = str;
	cmpWaiting = cmp;
	counterWating = 0;
	cmp.innerHTML = str;
	continueWaiting();
}
function continueWaiting() {
	var i;
	cmpWaiting.innerHTML = '<strong>'+strWaiting+' ';
	for (i=0; i<counterWating; i++)
		cmpWaiting.innerHTML+='.';
	cmpWaiting.innerHTML+='</strong>';
	counterWating++;
	if (counterWating == 4)
		counterWating = 0;
	timerWaiting  = window.setTimeout(continueWaiting, 500);
}
function stopWaiting() {
	clearTimeout(timerWaiting);
}

/*
 * Do a page refresh after login state changes.
 * This is the easiest but not the only way to pick up changes.
 * If you have a small amount of Facebook-specific content on a large page,
 * then you could change it in Javascript without refresh.
 */
function refresh_page() {
	return;
  window.location = 'index.php';
}

/*
 * Prompts the user to grant a permission to the application.
 */
function facebook_prompt_permission(permission) {
  FB.ensureInit(function() {
    FB.Connect.showPermissionDialog(permission);
  });
}

/*
 * Show the feed form. This would be typically called in response to the
 * onclick handler of a "Publish" button, or in the onload event after
 * the user submits a form with info that should be published.
 *
 */
function facebook_publish_feed_story(attachment,action_link) {
	FB.ensureInit(function() {
		FB.Connect.streamPublish(
			'',
			attachment,
			action_link,
			null,
			"Vuoi aggiungere qualcosa?",
			function (post_id,exception) {
				if (post_id != "null") {
					if (attachment.idrisorsacommento || attachment.idrisorsa) {
						if (window.XMLHttpRequest)
							liveReq = new XMLHttpRequest();
						else if (window.ActiveXObject)
							liveReq = new ActiveXObject("Microsoft.XMLHTTP");
						if (liveReq && liveReq.readyState < 4)
							liveReq.abort();
						liveReq.onreadystatechange = function () {
							if (liveReq.readyState == 4) {
								//alert(liveReq.responseText);
							}
						}
						if (attachment.idrisorsacommento)
							var url = "setrcfbuid.php?idrc="+attachment.idrisorsacommento;
						else
							var url = "setrcfbuid.php?idr="+attachment.idrisorsa;
						//alert(url);
						liveReq.open("GET",url);
						liveReq.send(null);
	
					}
				}
			},
			true);
	});
}
function facebook_publish_feed_story2(attachment,action_link) {
	FB.ensureInit(function() {
		/*
		FB.Connect.showPermissionDialog(
			"publish_stream",
			function () {
				FB.Connect.streamPublish(
					'',
					attachment,
					action_link,
					null,//'135753750103',
					"Vuoi aggiungere qualcosa?",
					function (post_id,exception) {
						if (post_id != "null") {
							if (attachment.idrisorsacommento || attachment.idrisorsa) {
								if (window.XMLHttpRequest)
									liveReq = new XMLHttpRequest();
								else if (window.ActiveXObject)
									liveReq = new ActiveXObject("Microsoft.XMLHTTP");
								if (liveReq && liveReq.readyState < 4)
									liveReq.abort();
								liveReq.onreadystatechange = function () {
									if (liveReq.readyState == 4) {
										//alert(liveReq.responseText);
									}
								}
								if (attachment.idrisorsacommento)
									var url = "setrcfbuid.php?idrc="+attachment.idrisorsacommento;
								else
									var url = "setrcfbuid.php?idr="+attachment.idrisorsa;
								//alert(url);
								liveReq.open("GET",url);
								liveReq.send(null);
			
							}
						}
					},
					true,'135753750103');
			},true,[135753750103]);
		*/
		FB.Connect.streamPublish(
					'',
					attachment,
					action_link,
					null,
					"Vuoi aggiungere qualcosa?",
					function (post_id,exception) {
						if (post_id != "null") {
							if (attachment.idrisorsacommento || attachment.idrisorsa) {
								if (window.XMLHttpRequest)
									liveReq = new XMLHttpRequest();
								else if (window.ActiveXObject)
									liveReq = new ActiveXObject("Microsoft.XMLHTTP");
								if (liveReq && liveReq.readyState < 4)
									liveReq.abort();
								liveReq.onreadystatechange = function () {
									if (liveReq.readyState == 4) {
										//alert(liveReq.responseText);
									}
								}
								if (attachment.idrisorsacommento)
									var url = "setrcfbuid.php?idrc="+attachment.idrisorsacommento;
								else
									var url = "setrcfbuid.php?idr="+attachment.idrisorsa;
								//alert(url);
								liveReq.open("GET",url);
								liveReq.send(null);
			
							}
						}
					},
					true,'135753750103');

	});
}

/*
 * If a user is not connected, then the checkbox that says "Publish To Facebook"
 * is hidden in the "add run" form.
 *
 * This function detects whether the user is logged into facebook but just
 * not connected, and shows the checkbox if that's true.
 */
function facebook_show_feed_checkbox() {
	return;
  FB.ensureInit(function() {
      FB.Connect.get_status().waitUntilReady(function(status) {
          if (status != FB.ConnectState.userNotLoggedIn) {
            // If the user is currently logged into Facebook, but has not
            // authorized the app, then go ahead and show them the feed dialog + upsell
            checkbox = ge('publish_fb_checkbox');
            if (checkbox) {
              checkbox.style.visibility = "visible";
            }
          }
        });
    });
}
