function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

var mbcookie = {
    games: new Array(),
    load: function ()
    {
        var the_cookie = $.cookie('mbgames');
        if (the_cookie) {
            this.games = JSON.parse(unescape(the_cookie));
        }
        return this.games;
    },
    save: function (expires, path)
    {
        var d = expires || new Date(2020, 02, 02);
        var p = path || '/';

        document.cookie = 'mbgames='+escape(JSON.stringify(this.games))
                          + ';path=' + p
                          + ';expires=' + d.toUTCString();
    }
}

function mbcookieSet(newGameUnique, newGameTitle)
{
	// Chack if newGame is in array
	var loaded = mbcookie.load();
	var inArray = false;
	var gameKey = 0;

	for(var i = 0; i < mbcookie.games.length; i++)
	{
		if(mbcookie.games[i][newGameUnique] == undefined)
		{
			if(inArray != true)
			{
				inArray = false;
			}
		}
		else
		{
			inArray = true;
			gameKey = i;
		}
	}

	// Bring game to front
	if(inArray == false)
	{
		// Add to front of array
		// And remove last
		var gameToAdd = new Object();
		gameToAdd[newGameUnique] = newGameTitle;	
		mbcookie.games.unshift(gameToAdd);
	}
	else
	{
		// Add to front
		// And remove dupliacte
		mbcookie.games.splice(gameKey, 1);
		var gameToAdd = new Object();
		gameToAdd[newGameUnique] = newGameTitle;	
		mbcookie.games.unshift(gameToAdd);
	}

	// remove last if array is greater then 5 items
	if(mbcookie.games.length > 5)
	{
		// drop last
		mbcookie.games.pop();
	}

	// ready to store? Yes!
	mbcookie.save();
}