  
  // initialize the library with the API key
      FB.init( { appId: '132516636782515', status: true, cookie: true,
                 xfbml: true } );

      // fetch the status on load
      FB.getLoginStatus(handleSessionResponse);
     
      $('#logout').bind('click', function() {
        FB.logout(handleSessionResponse);
      } );

      $('#disconnect').bind('click', function() {
        FB.api( { method: 'Auth.revokeAuthorization' }, function(response) {
          clearDisplay();
        } );
      } );
      
	  function facebooksignin() {
	  FB.login(handleSessionResponse);
	  }
	  
	  function facebooksignout() {
	  FB.logout(handleSessionResponse);
	  		$('#facebookpanel').css('visibility','hidden');
			$('#facebookpanel').css('width','0px');
			$('#facebookpanel').css('height','0px');
			
			$('#signinpanel').css('visibility','visible');
			$('#signinpanel').css('width','400px');
			$('#signinpanel').css('height','auto');
		
		$.get("/signout/facebook");	
	  }
	  
      // no user, clear display
     function clearDisplay() {
       /*$('#facebookpanel').css('visibility','hidden');
			$('#facebookpanel').css('width','0px');
			$('#facebookpanel').css('height','0px');
			
			$('#signinpanel').css('visibility','visible');
			$('#signinpanel').css('width','400px');
			$('#signinpanel').css('height','auto');*/
      }
       
      // handle a session response from any of the auth related calls
      function handleSessionResponse(response) {
        // if we dont have a session, just hide the user info
        if (!response.session) {
          clearDisplay();
          return;
        }

        // if we have a session, query for the user's profile picture and name
        FB.api(
          {
            method: 'fql.query',
            query: 'SELECT id , name, username , url ,  pic_square FROM profile WHERE id=' + FB.getSession().uid
          },
          function(response) {
            var user = response[0];
           
			$('#facebookprofile').html(user.name);
			$('#facebookprofile').attr('href',user.url);
			$('#facebookpic').attr('src',user.pic_square);
			$('#facebookpic').attr('alt',user.name);
			
			
			$('#signinpanel').css('visibility','hidden');
			$('#signinpanel').css('width','0px');
			$('#signinpanel').css('height','0px');
			
			$('#facebookpanel').css('visibility','visible');
			$('#facebookpanel').css('width','auto');
			$('#facebookpanel').css('height','auto');
$.post("/signin/facebook", 
{ userid: user.id, name: user.name,username: user.username, icon: user.pic_square ,profile: user.url  } );			
			
          }
        );
      }
