
/****** FACEBOOK ************/


/****** call back if we the user not having permissions so the publish stream box appears ************/

function grantExtendedPermissionsFB(msgid){
	    //alert(msgid);
		FB.Connect.showPermissionDialog("publish_stream", function(granted){
		   if(granted != ""){
			   var singleton=FB.Connect._singleton;
			   //////MOST IMPORTANT LINE GRANTS PERMISSIONS AND NOT ASKING AGAIN
		       singleton._userInfo.shortStorySetting = FB.FeedStorySetting.autoaccept;
		       
		   	   genericPrototype('/comment/index/ajaxpublishonfacebook/msgid/'+msgid,'genericJson', 'post', $('#formVar').serialize());

		       
		       
		   }else{

		   }
		   
		});
}


//signup
function update_signup(fid) {
	
	$('#account').html('');
	$('#FBsignin').html('<fb:profile-pic uid=\'loggedinuser\' facebook-logo=\'true\'></fb:profile-pic><br /><fb:name uid=\'loggedinuser\' firstnameonly=\'false\' useyou=\'false\'></fb:name>');

	//if($('#firstname')[0]){
	
	if (!fid){
	    var uid = FB.Facebook.apiClient.get_session().uid ;
	}else{
		uid = fid;
	}
	    var sql = "SELECT birthday_date,first_name,last_name,locale FROM user WHERE uid ="+uid;
	    try
	    {
	    	 FB.Facebook.apiClient.fql_query(sql, function(result, ex) {
	 	        var firstName= result[0]['first_name'];
	 	        var locale= result[0]['locale'];
	 	        var lastName= result[0]['last_name'];
	 	        //firstname

	 	        $('#firstname').val(firstName);
	 	        $('#firstname').attr('readonly','true');
	 	        //lastname
	 	        $('#lastname').val(lastName);
	 	        $('#firstname').attr('readonly','true');
	 	        // fuid
	 	        $('#facebook_user').val(uid);
	 	    });
	    }
	  catch(err)
	    {
		  //alert(err);
	    }
	    
	   
	//}
    if($('#FBsignin')[0]){
    FB.XFBML.Host.parseDomElement($('#FBsignin')[0]);
    }
    
    //remove unnesasery data from the form
    $("#fb_remove_1").remove();
    $("#fb_remove_2").remove();
    $("#fb_remove_3").remove();
    $("#fb_remove_4").remove();
    
    
    $('#facebook_user').val(uid);

    genericPrototype('/membres/ajaxloginuserfb/fbuid/'+uid,'genericJson', 'post', $('#formVar').serialize());
}


/****** whe login with facebook always update the login status all the elements are updated in the ajax call ************/
function update_signin() {
    //  
	$('#FBsignin').html('<fb:profile-pic uid=\'loggedinuser\' facebook-logo=\'true\'></fb:profile-pic> &nbsp;&nbsp;<fb:name uid=\'loggedinuser\' firstnameonly=\'false\' useyou=\'false\'></fb:name>');
	$('#photoOnFacebook').html('<fb:profile-pic uid=\'loggedinuser\' facebook-logo=\'true\'></fb:profile-pic>');
	$('#FBsigninBox').html('');
    
	//sign in
	if($('#FBsignin')[0]){
    	FB.XFBML.Host.parseDomElement($('#FBsignin')[0]);
    }
	// comment
	if($('#photoOnFacebook')[0]){
    	FB.XFBML.Host.parseDomElement($('#photoOnFacebook')[0]);
    }
	// FB box
	if($('#FBsigninBox')[0]){
    	FB.XFBML.Host.parseDomElement($('#FBsigninBox')[0]);
    }
	
	if(FB.Facebook.apiClient.get_session()){
		var uid = FB.Facebook.apiClient.get_session().uid ;
		genericPrototype('/membres/ajaxloginuserfb/fbuid/'+uid,'genericJson', 'post', $('#formVar').serialize());
	}
}


// logout
function fbLogout(){
	FB.Connect.logout(actionLogout);
}

function actionLogout(){
	//alert('sss');
	genericPrototype('/member/index/ajaxlogout','genericJson');
}


//get info ajax call for updating the login info

function getinfoUser (controller,name) {
	/****** try to get fbid safe ****/
	try
    {
    	var uid = FB.Facebook.apiClient.get_session().uid ;
    }catch(err){
		uid = false;
	}
	// FB box
	$('#FBsigninBox').html('');
	if($('#FBsigninBox')[0]){
    	FB.XFBML.Host.parseDomElement($('#FBsigninBox')[0]);
    }
    if (uid){
    	genericPrototype('/membres/ajaxloginuserfb/fbuid/'+uid,'genericJson', 'post', $('#formVar').serialize());
    }

}





/****** END FACEBOOK ************/
