// Functions to vars
var aFBIDtoMBID = -1;
var aMBIDtoFBID = -1;
var checkMBCookies	= null;
var userDataStorage = new Object();
var mbid			= null;

//-----------------------------------------------------------
checkMBCookies = function(uid, callback)
{
	// if you're not logged in, clear out your MBID and MBFRIENDS cookies
	if (uid==false){
		$.cookie('MBID', null);
		$.cookie('MBID_SIG', null);
		$.cookie('MBFRIENDS', null);
		$.cookie('MBFRIENDS_SIG', null);
		$.cookie('MBFRIENDS_FBS', null);
		$.cookie('MBFRIENDS_FBS_SIG', null);
		//console.log('cleared');
		return;
	}

	// if you're logged in, and you have MBID and MBFRIENDS cookies, you're fine.
	if ($.cookie('MBID') &&
	    $.cookie('MBID_SIG') &&
	    $.cookie('MBFRIENDS') &&
	    $.cookie('MBFRIENDS_SIG') &&
	    $.cookie('MBFRIENDS_FBS') &&
	    $.cookie('MBFRIENDS_FBS_SIG'))
        {
			// now if set load the callback funcation
			//console.log('here');
			//console.log($.cookie("MBID"));
			finishBakingCookies(callback);
			return;
	}

	// else if you're logged in but DON'T have cookies, then let's get some
	api.friends_getAppUsers(function(friends){
			//console.log('there');
			//console.log($.cookie("MBID"));

            $.post('/user/login',{friends:JSON.stringify(friends)}, function(){finishBakingCookies(callback)}, 'JSON');
        });
 }

function finishBakingCookies(callback)
{
	//console.log('callback');

	mbid = $.cookie("MBID");

	//console.log(mbid);

	// now if set load the callback funcation
	if(callback != null){
		callback();
	}
}

// Used on the profile pages to slide the hidden names of your friends scores up and down.
function slidit(id){
    $('#' + id + '-game_more').slideToggle('fast');
}

// Used to store user data for the profile page
function getUserData(data){
    for(var i = 0; i < data.length; i++)
    {
		userDataStorage[data[i]['uid']] = data[i];
    }
    return;
}

function MBIDtoFBID(mbid){
	// populate the global MBIFtoFBID and FBIDtoMBID hashes if
	// they're not already setup
	if (aFBIDtoMBID ==-1 || aMBIDtoFBID==-1){

		aMBIDtoFBID = new Object();
		aFBIDtoMBID = eval('('+$.cookie('MBFRIENDS')+')');

		for (var thisfbid in aFBIDtoMBID ){
			thismbid = aFBIDtoMBID[thisfbid];
			aMBIDtoFBID[thismbid] = thisfbid;
		}
	}
	return aMBIDtoFBID[mbid];
}

function postScoreToWall(gameUniuqName, gameTitle, score)
{
	api = FB.Facebook.apiClient;
	var attachment = {'name':gameTitle,
					  'href':'http://www.mousebreaker.com/games/'+gameUniuqName,
					  'description':'My best ever score on '+gameTitle+' is '+score+'! Reckon you can beat that?',
					  'media':
						[{'type':'image',
						  'src':'http://www.mousebreaker.com/games/screenshots/'+gameUniuqName+'.gif',
						  'href':'http://www.mousebreaker.com/games/'+gameUniuqName}]
					 };

	var actionLinks = [{ 'text': 'Play '+gameTitle, 'href':'http://www.mousebreaker.com/games/'+gameUniuqName}];

	FB.Connect.streamPublish('', attachment, actionLinks, null, 'Brag about your score on Facebook...', null, true);
}

function postMedalToWall(gameUniuqName, gameTitle, score, medal)
{
	$('#flashcontent').css("visibility","hidden")

	api = FB.Facebook.apiClient;
	var attachment = {'name':gameTitle,
					  'href':'http://www.mousebreaker.com/games/'+gameUniuqName,
					  'description':'I just got a '+medal+' medal for a score of '+score+' on '+gameTitle+' on Mousebreaker. Think you can beat me?',
					  'media':
						[{'type':'image',
						  'src':'http://www.mousebreaker.com/layout/award_'+medal+'.png',
						  'href':'http://www.mousebreaker.com/games/'+gameUniuqName}]
					 };

	var actionLinks = [{ 'text': 'Play '+gameTitle, 'href':'http://www.mousebreaker.com/games/'+gameUniuqName}];

	FB.Connect.streamPublish('', attachment, actionLinks, null, 'Brag about your medal on Facebook...', function(){$('#flashcontent').css("visibility","visible");}, true);
}