

//Facebook Connect
function fb_gfc_fbc_disconnect(){
	fb_gfc_empty_details();
	FB.Connect.logout(function($){
		jQuery('.fb-gfc-userbox').hide();
		jQuery('.fb_gfc_hide_on_login,.fb_gfc_hide_on_login_button').show();
	});
				
	return false;

}

function fb_gfc_auth_using_fb() {
  //get the users data from FB
  var viewer  = FB.Facebook.apiClient.fql_query(
  
      'SELECT uid, name, pic_square_with_logo,profile_url FROM user WHERE uid='+FB.Facebook.apiClient.get_session().uid,
      
      function(results) {
	
		// if results null, get information data from php
		if(results == null){
			var ajax_url = FC_GFC_PLUGIN_URL + '/fbc/fblogin.php';
			var data = {};

			jQuery.post(ajax_url, data, function(response) {
				if(response != ''){
					// use eval to parse JSON
					var user = eval("("+response +")");
					if(user != null){
						// if not null update userbo
						fb_gfc_update_userbox( user.uid,
											   user.name,
										       user.uid+'@facebook.com',
				                               user.pic_square_with_logo,
				                               user.profile_url,
				                        	   'fb_gfc_fbc_disconnect()');
					}
				}else{
					//alert('No response from server');
				}
			});
		}else{ //if results exists, update userbox
			
      		fb_gfc_update_userbox( results[0].uid,
						results[0].name,
						results[0].uid+'@facebook.com',
                        results[0].pic_square_with_logo,
                        results[0].profile_url,
                        'fb_gfc_fbc_disconnect()');

		}
	});

}


//Google Friend Connect
function fb_gfc_gfc_disconnect(){
	fb_gfc_empty_details();
	google.friendconnect.requestSignOut();
 	jQuery('.fb-gfc-userbox').hide();
	jQuery('.fb_gfc_hide_on_login,.fb_gfc_hide_on_login_button').show();
}

function fb_gfc_auth_using_gfc() {
  //Request GFC to send extra profile data
  var params = {};
      params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] =
      [opensocial.Person.Field.URLS];
      
  
  // Create a request to grab the current viewer.
  var req = opensocial.newDataRequest();
  req.add(req.newFetchPersonRequest('VIEWER', params), 'viewer_data');
  // Sent the request
  req.send(function(data) {
  
    // If the view_data had an error, then user is not signed in
    if (!data.get('viewer_data').hadError()) {
      //get the users data from GFC
      var viewer = data.get('viewer_data').getData();
      
	  var profile_url;
	  var email;
	//if undefined field urls
	try{
		profile_url = viewer.getField(opensocial.Person.Field.URLS)[0].getField('address');
	}catch(err){
	    profile_url	= viewer.getField(opensocial.Person.Field.PROFILE_URL);
		if(profile_url == undefined){
			profile_url = '';
		}
	}
	
	try{
		email = viewer.getField(opensocial.Person.Field.EMAILS)[0].getField('address');
	}catch(err){
		email = viewer.getField(opensocial.Person.Field.ID)+'@friendconnect.google.com';
	}


      fb_gfc_update_userbox( viewer.getField(opensocial.Person.Field.ID),
					  viewer.getDisplayName(),
					  email, 
                      viewer.getField(opensocial.Person.Field.THUMBNAIL_URL),
                      profile_url,
                      'fb_gfc_gfc_disconnect()');
    }else{
	  //alert(data.get('viewer_data').hadError());
	}
  
  });
}


function fb_gfc_empty_details(){
  jQuery('#author').val('');
  jQuery('#email').val('');
  jQuery('#url').val('');
}

//Generic updates #userbox with info retrieved
//from services
function fb_gfc_update_userbox(uid, name, email,image, url, logout) {

	jQuery(document).ready(function(){
		

  	//hide name input and service
  	//login buttons
  	jQuery('.fb_gfc_hide_on_login,.fb_gfc_hide_on_login_button').hide();
 	// Refresh the DOM
    	//FB.XFBML.Host.parseDomTree();
    	getImage(email,image);

  	//populate the data in #userbox and show it
  	jQuery('.fb_gfc_hide_on_login').after( "<div class='fb-gfc-userbox'></div>");
  	jQuery('.fb-gfc-userbox').html("").html("<img alt='"+name+"' src='"+image+"' /><p>"
                    			 + "Logged in as <a href='"+url+"'>" + name + "</a> "
                    			 + "(<a href='#author' onclick='" + logout + "'>logout</a>)</p>" ).load();
	
	

  //populate the values of the inputs
  //using data from serivce
  		jQuery('#author').val(name);
  		jQuery('#email').val(email);
  		jQuery('#url').val(url);
		
		});
	
}

function getImage(userid,image_url) {

		var ajax_url = FC_GFC_PLUGIN_URL + '/servercode.php';
		var data = {
			userid				: userid,
			image_url			: image_url
		};

		jQuery.post(ajax_url, data, function(response) {
			/*if(response != ''){
				alert(response);
			}else{
				alert('No response from server');
			}*/
		});

}