
sfFacebookConnect = function(api_key, signin_url)
{
  this.xd_receiver_path = "http://www.walldress.com/xd_receiver.htm";
  this.api_key = api_key;
  this.signin_url = signin_url;
  this.callback = '';
  this.forward = '';
  
  this.init();
};
sfFacebookConnect.prototype.init = function()
{
		var url_signin = this.signin_url;
		var api_key= this.api_key;
		window.fbAsyncInit = function() {
		  FB.init({appId: api_key, xfbml: true, cookie: true, oauth: true});		


	}
}

sfFacebookConnect.prototype.TryMe = function()
{
	var url_signin = this.signin_url; // User click FB Connect et success
	FB.getLoginStatus(function(response) { if (response.status === 'connected') {
		document.location.href= url_signin;
	}
	
	});
	
}

sfFacebookConnect.prototype.onStatus = function (response) {
	console.info('onStatus', response);
	if (response.session) {
		console.info('User logged in');
		if (response.perms) {
			console.info('User granted permissions');
		}else{
			console.info('User has not granted permissions');
		}
		alert('ok');
		showAccountInfo();
	} else {
		console.info('User is logged out');
		showLoginButton();
	}
}

sfFacebookConnect.prototype.getSigninUrl = function()
{
  t_signin_url = this.signin_url;
  if(this.forward != undefined && this.forward != '')
  {
    t_signin_url += '?forward=' + this.forward; 
  }
  
  return t_signin_url;
}
sfFacebookConnect.prototype.reload = function(object){
FB.XFBML.parse(object);
//FB.XFBML.parse();
//FB.XFBML.Host.parseDomTree(object);
}
sfFacebookConnect.prototype.gotoLoginPage = function()
{
  //console.log(this.getSigninUrl());
  document.location.href= this.getSigninUrl();
};
sfFacebookConnect.prototype.requireSession = function(forward, callback)
{
  this.forward = forward;
  if (callback==undefined)
  {
	var current_obj = this;
	callback = function(){current_obj.gotoLoginPage()};
  }
  FB.ensureInit(function(){FB.Connect.requireSession(callback)});
};

/*
 * 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.
 *
 */
sfFacebookConnect.prototype.publishFeedStory = function(form_bundle_id, template_data)
{
  // Load the feed form
  FB.ensureInit(
    function()
    {
      FB.Connect.showFeedDialog(form_bundle_id, template_data);
      //FB.Connect.showFeedDialog(form_bundle_id, template_data, null, null, FB.FeedStorySize.shortStory, FB.RequireConnect.promptConnect);
      
      // hide the "Loading feed story ..." div
      //ge('feed_loading').style.visibility = "hidden";
    }
  );
};


/* Feed Publish */
sfFacebookConnect.prototype.streamPublish = function(attachment, action_links)
{
	FB_RequireFeatures(["Connect"], function() {
		FB.ensureInit(function() {
			FB.Connect.streamPublish('', attachment, action_links);
		});
	});
};

sfFacebookConnect.prototype.stream_callback = function (attachment, action_links){
	FB.Connect.streamPublish("", attachment, action_links, null, "", function(){}, true, null);
};


sfFacebookConnect.prototype.facebook_prompt_permission = function()
{
   FB_RequireFeatures(["Connect"], function() {
    
     FB.ensureInit(function() {

		FB.Facebook.apiClient.users_hasAppPermission("email",
		 function(result) {
			if (result == 0) {
				FB.Connect.showPermissionDialog("email",function(){});
			} 
		});

     });
    
   });
};
/* Auto Feed Publish */
sfFacebookConnect.prototype.autostreamPublish = function(attachment, action_links)
{
   FB_RequireFeatures(["Connect"], function() {
  
   
     FB.ensureInit(function() {

	//FB.Connect.showPermissionDialog('publish_stream', stream_callback);

    //check is user already granted for this permission or not
    FB.Facebook.apiClient.users_hasAppPermission('publish_stream',
     function(result) {
        // prompt offline permission
        if (result == 0) {
            // render the permission dialog

            FB.Connect.showPermissionDialog('publish_stream', FB.Connect.streamPublish("", attachment, action_links, null, "", function(){}, true, null));
        } else {
            // permission already granted.

			FB.Connect.streamPublish("", attachment, action_links, null, "", function(){}, true, null)


        }
    });


    //FB.Connect.streamPublish("sympathique", attachment, action_links, null, "", stream_callback, true, null);
        // FB.Connect.streamPublish('', attachment, action_links, null, null, "", stream_callback, true, null);
              // sf_fb.autostreamPublish();

     });
    
   });
};

