FB.init("857de749f3314d044557ef732ca098f1",
        "http://www.theinsidesource.com/xd_receiver.htm",
        {
            "ifUserConnected":fb_onConnected,
            "ifUserNotConnected":fb_onNotConnected,
            "doNotUseCachedConnectState":true
        }
    );
    
function fb_onConnected(user_id)
{
    $$(".facebook-button").each(function(el){
        el.empty();
        var logout = new Element("a", {"href":"#logout"});
        logout.set("html", "<img src='/static/images/layout/buttons/fb_logout_button.png' alt='Logout'/>");
        logout.addEvent("click", function(e){ e.stop(); FB.Connect.logoutAndRedirect("/"); })
        el.grab(logout);
    });
    
    auth_using_fb();
}

function fb_onNotConnected()
{
}
    
function auth_using_fb() {  
    //get the users data from FB  
   var viewer  = FB.Facebook.apiClient.fql_query(  

       'SELECT name, pic_square_with_logo,profile_url,current_location,proxied_email FROM user WHERE uid='+FB.Facebook.apiClient.get_session().uid,  

       function(results) {  
         update_userbox( results[0].name,  
                         results[0].pic_square_with_logo,  
                         results[0].profile_url,
                         results[0].current_location,
                         results[0].proxied_email,
                         'FB.Connect.logoutAndRedirect("/");return false;')  
       }  
  );  
}

function fb_post_to_feed(post_title, post_url, site_name, site_url, comment, callback)
{
    var user_message_prompt = "Is there anything else you'd like to share?";
    var user_message = {value: "Write your thoughts here"}; 
    
    FB.Connect.showFeedDialog("128591546371", {
        "post-title": post_title,
        "post-url": post_url,
        "site-url": site_url,
        "site-name": site_name,
        "comment": comment
        },
        null, null, null, FB.RequireConnect.require, callback,
        user_message_prompt, user_message);
}

function update_userbox(name, image, url, location, email, logout)
{
    var p = new Element("a", {"href":url});
    var n = new Element("span", {"class":"name"});
    n.set("html", name);
    p.grab(n);
    $("top_facebook_label").empty().set("html", "Welcome ").grab(p);
    //alert(name + image + url);
    
    var cn = $("comment_name_textbox");
    var ce = $("comment_email_textbox");
    var cl = $("comment_location_textbox");
    var cu = $("comment_url_textbox");
    
    var contact_name = $("name_textbox");
    var contact_email = $("email_textbox");
    var contact_subject = $("subject_select");
    var contact_message = $("message_textbox");
    
    var comment_submit_button = $("comment_submit_button");
    if($chk(comment_submit_button))
    {
        if(typeof comment_post == "function")
        {
            comment_submit_button.addEvent("click", function(e){
                e.stop();
                comment_post(function(){ $("comment_form").submit(); });
                });
        }
    }
    
    if($chk(contact_name))
    {
        contact_name.value = name;
    }
    
    /*
    if($chk(contact_email))
    {
        contact_email.value = email;
    }
    */
    
    if($chk(cn))
    {
        cn.value = name;
    }
    
    if($chk(ce))
    {
        ce.value = email;
    }
    
    if($chk(cl))
    {
        cl.value = location.city + ", " + location.state;
    }
    
    if($chk(cu))
    {
        cu.value = url;
    }
    
    var fields = $("comment_logged_out_fields");
    if($chk(fields))
    {
        fields.addClass("hidden");
        
        var status = $("comment_logged_in_status");
        if(!$chk(status))
        {
            status = new Element("div", {"id":"comment_logged_in_status"});
            status.wraps(fields);
        }
        
        var span = new Element("span");
        span.set("html", "Logged in as: ");
        var href = new Element("a", {"href":url});
        href.set("html", name);
        status.grab(span);
        status.grab(href);
    }
}