var src;
var url;

var firstTime = true;

var fbApiInitialized = false;



function fbEnsureInit(callback) {
    if (!window.fbApiInitialized) {
        setTimeout(function() { fbEnsureInit(callback); }, 50);
    } else {
        if (callback) { callback(); }
    }
}

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();
    
	fbEnsureInit(function() {
		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
	fbEnsureInit(function() {
        FB.ui({method:'stream.publish',
        	message:'Check out my customized meal on the Red Robin Customizer',
        	attachment:{
        		name:'Red Robin Customizer',
        		href:url,
        		caption:'{*actor*} customized a meal on the Red Robin Customizer',
        		media:[{ 
					    "type": "image", 
					    "src": src, 
					    "href": url
				}]
        	},
        	action_links:[{text:'Customize your meal', href:url}], 
        	user_message_prompt:'What do you think?'})
    },
	function(response) {
		if (response && response.post_id) {
			//alert('Post was published.');
    	} else {
			//alert('Post was not published.');
		}
	}); 
}
