  var authToken ='';
  jQuery.noConflict();
  //Initializing the FACEBOOK API
  window.fbAsyncInit = function() {
    FB.init({appId: FB_APP_ID, status: true, cookie: true,
             xfbml: true});
      		
  };
  
  jQuery(document).ready(function() {
	  

	  jQuery("body #global").show();

	  
	   if (!globalLogin){
	   	
	    var e = document.createElement('script'); e.async = true;
	    e.src = document.location.protocol +
	      '//connect.facebook.net/fr_CA/all.js';
	    document.getElementById('fb-root').appendChild(e);
  	  }
  });
  
  //If users connected load the scriopt that checks the email
  function userConnected(response){
	  jQuery(".FBsigninBox").hide();
	  //check in db if he is existing user
	  fbID = response.session.uid;
	 
	  url = "/ajax/fblogin/";
	  authToken = response.session.access_token;
	  //get information about the user by extended permissions
	  var urlAccses = 'https://graph.facebook.com/' +  fbID +'/?access_token=' + authToken +  '&callback=checkEmail';
	
	  var script = document.createElement("script");
	  script.src = urlAccses;
	  document.getElementsByTagName("head")[0].appendChild(script);

  }



  function checkEmail(graph)
  {
	  //console.log(graph);
	  fbID = graph.id;
	  email = graph.email;
	  jQuery.ajax({
		  type: "POST",
			  url: url,
			  data: "fbid=" + fbID + "&token="  + authToken + '&email=' +email ,
			  dataType: "json",
			  context: document.body,
			  success: function(response){
			  		if (response.hasvisa){
						//TODO CHANGE THE LOGIN INFO	
			  			
			  			window.location = response.backUrl;

				  	}else{
			  			window.location = "http://courriel.cyberpresse.ca/membre/profile.fb.php?mode=register&nextUrl=http://moncinema.cyberpresse.ca/";
				  	}
					
			  }
			});	

  }
  

  //after login detecting the facebook status
  function afterInit(){
		 // window.FB.Connect.ifUserConnected(userConnected,userNotConnected);
		  FB.getLoginStatus(fbUserStatus,true);
		 
		
  }

  function fbUserStatus(response){
	 
		 if (response.status=="connected"){
			 
			 userConnected(response);
		 }
		 if(response.status=="notConnected"){
			 
		 }
		 if(response.status=="unknown"){
		 }	 	 
	  }
