function facebook_onlogin_ready()
{
    var url = '/fb/mydir_login';
    // CHECK TO SEE IF THEIR FB ACCT IS LINKED
    new Ajax.Request(url, {
      method: 'get',
      onSuccess: function(transport) {
        var notice = $('notice');
        if (transport.responseText.match(/1/)){

          // alert('you are now logged in, w00t');          
          new Ajax.Updater('header_user_menu', '/users/user_header');
          
          if ($('comment_form_wrapper')!=undefined) {
              
              location.reload(true);
              
              // had to change this because comment form now needs to know various details: e.g.
              // modelname, current_path, current_id'
              //new Ajax.Updater('comment_form_wrapper', '/users/user_comment_form');
          };
          
        }else{
            myLightWindow.activateWindow({
                                            href: '/fb/connect_create_account/',
                                            width: 780,
                                            height: 500,
                                            resizeSpeed:20
                                        });
        }
      }
    });
}

function update_user_header(fb_api_key)
{
    new Ajax.Updater('header_user_menu', '/users/user_header', {evalScripts:true,onComplete:function(){FB_RequireFeatures(["XFBML"], function(){ FB.Facebook.init(fb_api_key, "/fb/connect_callback"); });}});
    /*
    new Ajax.Updater('header_user_menu', '/users/user_header', {evalScripts:true,onComplete:function(){FB_RequireFeatures(["XFBML"], function(){ FB.Facebook.init("1fc38576f4ee388baaabc03c41af4881", "/fb/connect_callback"); });}});
    */
}


function fb_connect_logout()
{
    // GET CURRENT PAGE TO SET AS post_logout_url
    var post_logout_url = document.location;
    createCookie('post_logout_url',post_logout_url,1);
    
    FB.ensureInit ( function () { 
		FB.Connect.logoutAndRedirect("/users/logout");
	});
    return false;
    
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
