//facebook-specific functions

function PublishToFeed(headline, description, href, imageUrl)
{
	var attachment = {
		'name' : headline
		,'href' : href
		,'description' : description
		,'media' : [{
			'type' : 'image'
			,'src' : imageUrl
			,'href' : href
		}]
	};
	var action_links = [{
		'text' : 'Play now!'
		,'href': href
	}];

	if(Facebook)
	{
		Facebook.streamPublish('', attachment, action_links);
	}
	return false;
}

function PublishToFeedViaConnect(headline, description, href, imageUrl)
{
	var attachment = {
		'name' : headline
		,'href' : href
		,'description' : description
		,'media' : [{
			'type' : 'image'
			,'src' : imageUrl
			,'href' : href
		}]
	};
	var action_links = [{
		'text' : 'Play now!'
		,'href': href
	}];

	if(FB && FB.Connect)
	{
		FB.Connect.get_status().waitUntilReady(function(status) {
			FB.Connect.streamPublish('', attachment, action_links);
		});
	}
	return false;
}

function CheckExtendedPermissions(callbackFunction)
{
	//callbackFunction will get comma-separated list of permissions on success, '' on failure or dialog cancel
	if(Facebook)
	{
		Facebook.showPermissionDialog('publish_stream', callbackFunction, showProfileSelector, '');
	}
	return false;
}

function SetEmailPermission(playerId, redirectUrl)
{
	var ajax = new Ajax();
	ajax.responseType = Ajax.RAW;
	ajax.ondone = function(data)
	{
		document.setLocation(redirectUrl);
	}
	ajax.post(cWEB_ROOT + '/fb/permissions.php?player=' + playerId)
}

function BuildProfileSummary(currentElement, id)
{
	if('undefined' == typeof(Ajax))
	{
		//not on facebook, return alternate way
		return true;
	}
	
	var ajax = new Ajax();
	ajax.responseType = Ajax.FBML; //type can be Ajax.JSON, Ajax.FBML, or Ajax.RAW
	ajax.ondone = function(data)
		{
			var dialogTitle = 'Profile';
			var dialogContents = data;
			var dialogConfirmText = 'View full profile';
			var dialogCancelText = 'Close';

			var dialog = new Dialog(Dialog.DIALOG_CONTEXTUAL);
			dialog.setContext(currentElement);
			dialog.showChoice(dialogTitle, dialogContents, dialogConfirmText, dialogCancelText);
			dialog.onconfirm = function()
				{
					document.setLocation(currentElement.getHref());
				}
		}
	ajax.post(cWEB_ROOT + '/fb/expenttest/profile_summary.php?id=' + id);
	return false;
}


function navigateToURL(url)
{
	document.setLocation(url);
}

//possible ms gamehub specific stuff in the games, just make stub functions that don't do anything
function GameBreak(value) { return; }
function GameContinue(value) { return; }

function logClick(link) {
	$.ajax({
		url: "/ajax.php",
		type: "POST",
		data: {
			action: "log_click",
			url: $(link).attr("href")
		}
	}
	);
}

function onWin(gameID,contentID) {
	console.log(gameID+":"+contentID);
	$.ajax(
		{
			url: '/ajax.php',
			data: {
				action: 'victory_reward',
				game_id: gameID,
				content_id: contentID
			}
		}
	);
}
