var src;
var url;
var firstTime = true;

function postFacebook(imgSrc, foodItemID) {
    src = imgSrc;
    var str = window.location.href;
    if (str.indexOf('?') != -1)
        str = str.substring(0, str.indexOf('?'));

    url = str + '?u=' + foodItemID.toString();
    
    FB.ensureInit(function() {
        FB.Connect.get_status().waitUntilReady(function(status) {
            if (status == FB.ConnectState.connected) {
                doPost();
            }
            else {
                login();
            }
        });
    });
    
}

function login() {
    FB.Connect.requireSession(doPost);
}

function doPost() {
    // can be modified as a title for what you want it to say above the text box of the feed dialog
    var user_message_prompt = "";
    // can be modified to have default text already in the feed dialog text box
    var user_message = { value: "" };

    // fills in the template parameters defined in the feed template that was created on the facebook app page
    // basic html can be used, such as an anchor tag for a url
    var template_data = { "url": '<a href="http://www.redrobin.com">Red Robin</a>',
        "images": [{ 'src': src,
            'href': url}]

        };
        FB.ensureInit(function() {
            FB.Connect.showFeedDialog(106540859771, template_data, '', '', null, FB.RequireConnect.promptConnect, callback, user_message_prompt, user_message);
        });

        if (firstTime) {
            firstTime = false;
            setTimeout(doPost, 2000);
        }
        
    }
    function callback() {
        //alert('test');
    }
    FB_RequireFeatures(["Connect"], function() {
        FB.init("ba2286e861fae58a8da2d74ef3e108b6", "xd_receiver.htm");
        FB.Facebook.get_sessionState().waitUntilReady(function() {

        });
    });
