var SHARE_MY_PEPPER_BUNDLE_ID = 110329813741;
var SHARE_GALLERY_PEPPER_BUNDLE_ID = 110329813741;
var SHARE_STORY_BUNDLE_ID = 110332358741;


var keepHash = false;

function reload_window(){
	if(keepHash){
		window.location.reload();
	} 
	else {
		window.location = window.location.href.replace(/#.*$/, '');
	}
}

function doFbSharePepper(PepperId, bundle_id) {
	FB.FBDebug.dump("Entering doFbSharePepper");
	
	pageTracker._trackPageview('/outbound/fbconnect/logged-in/share-pepper');
	
	var image_src = "http://" + window.location.host + "/images/peppers/" + PepperId + "_g.jpg";
	
	var attachment = {
						'name': 'Create-A-Pepper to fight childhood cancer',
						'caption' : "{*actor*} just created a pepper at CreateAPepper.com.",
						'description' : "  Create your own pepper with Chili's Grill & Bar&reg; and help raise awareness for St. Jude Children's Research Hospital. Chili's will donate 100% of all profits on September 27 to the hospital.", 
						'media': [{'type':'image',
                             'src':image_src,
                             'href':"http://www.createapepper.com/ViewPepper.aspx?p="+PepperId}]
					 };
					 
	
	var callback = cbPepperShared;
	
	FB.Connect.streamPublish('', attachment, null, null,
                         'Add a comment to share on your wall',
                         callback );

	FB.FBDebug.dump("Leaving doFbSharePepper");
}

function cbPepperShared(post_id, exception) {
	FB.FBDebug.dump("Entering cbPepperShared");
	
	//alert("in cbPepperShared");
	
	FB.FBDebug.dump("Leaving cbPepperShared");
}


function fbShareStoryDetermineLogin(event, target_id) {
	var first_time_through = true;
	FB.Connect.ifUserConnected(function(){if(first_time_through){fbShareStory(event, target_id)}}, 
		function(event)
		{
			first_time_through = false;
			$('#fbInterstitial').addClass('story');
			
			
			Shadowbox.open({
							player: 'inline',
							content: '#loginInterstitial',
							height: 395,
							width: 725,
							displayNav: "false"
						});
			
			keepHash = true;
			window.location = window.location.href.replace(/#.*$/, '') + '#' + target_id;
			
				
			
		}
	);
}

function fbShareStory(event, target_id) {
	FB.FBDebug.dump("Entering fbShareStory");
	
	var storyHolder = $('.shareFacebook.'+target_id).parents('.childStory')[0];
	var headingText = $(storyHolder).find('.formHeader strong').html()
						+ $(storyHolder).find('.formHeader span').html() + "<br /><br />";
	var bodyText = "";
	$(storyHolder).find('.storyBodyWrapper').clone().find('img').remove().end().find('p').append('<br /><br />').each(function(){bodyText += $(this).html()});
	var photoSrc = $(storyHolder).find('.formBody p img')[0].src;
	
	var image_src = photoSrc;
	var template_data = {"images":[{"src":photoSrc, "href":"http://www.createapepper.com/"}], "title":headingText, "body":bodyText};
	
	var bundle_id = SHARE_STORY_BUNDLE_ID;
	var callback = 'cbPepperShared';
	
	FB.Connect.requireSession(
		function()
		{
			alert("showFeedDialog is deprecated.  Replace with streamPublish to use this.");
			Shadowbox.close();
			FB.Connect.showFeedDialog(bundle_id, template_data, [], "", null, FB.RequireConnect.require, function(){reload_window();});
		}
	);
	
	
	FB.FBDebug.dump("Leaving fbShareStory");
}


function doFbInviteFriends() {
	FB.FBDebug.dump("Entering doFbInviteFriends");
	pageTracker._trackPageview('/outbound/fbconnect/logged-in/invite-friend');
	
	FB.ensureInit(function() {
        var dialog = new FB.UI.FBMLPopupDialog('Invite your friends', '');
        var fbml = 
			"<fb:fbml>" 
				+"<fb:request-form action=\"" + document.location.href 
					+ "\"\tmethod=\"POST\" invite=\"false\" type=\"Create a Pepper\" "
					+"content=\"Check out CreateAPepper.com. "
					+"<fb:req-choice url='" + document.location.href + "' label='Connect' />\">"
					+"<fb:multi-friend-selector\tshowborder=\"false\" exclude_ids=\"\" max=\"20\"  "
						+"actiontext=\"Invite your friends\" rows=\"5\" email_invite=\"false\" bypass=\"cancel\"\tshowborder=\"false\" />"
				+"</fb:request-form>"
			+"</fb:fbml>";
		
        dialog.setFBMLContent(fbml);
        dialog.setContentWidth(750); 
        dialog.setContentHeight(540);
		dialog.add_closing(cbInviteFriendsClosing);
        dialog.show();
    });
	
	FB.FBDebug.dump("Leaving doFbInviteFriends");
}

function cbInviteFriendsClosing(bool_result) {
	FB.FBDebug.dump("Entering cbInviteFriendsClosing");
	
	FB.FBDebug.dump("Leaving cbInviteFriendsClosing");
}


function ga_fb_login_click_track() {
	pageTracker._trackPageview('/outbound/fbconnect/not-logged-in/click-to-fb');
}
