facebook_user_cookie_name = facebook_key + '_user';
facebook_fbsetting_cookie_name = 'fbsetting_' + facebook_key;

function facebook_cookie_exists() {
	return (document.cookie.indexOf(facebook_user_cookie_name) != -1);
	// && (document.cookie.indexOf(facebook_fbsetting_cookie_name) != -1)
}

function facebook_on_user_not_connected() {
	if (facebook_cookie_exists()) {
		document.location.reload();
	}
}

function update_user_box() {
	try {
		var user_box = document.getElementById("fb_user"), api = FB.Facebook.apiClient, txt = [];
		if (api.get_session().uid != '') {

			//If we are on Safari, it doesn't handle facebook cookies in iframes well.
			//We have to go to specially prepared page (safari_iframe_site) in the same domain as iframe, so
			//we can put facebook cookie and then return back to site hosting iframe (iframe_hosting_site).
			if (is_safari){
				if (facebook_cookie_exists()) {
					if (top.location.href == safari_iframe_site) {
						top.location.href = iframe_hosting_site;
					}
				} else if (top.location.href != safari_iframe_site) {
					top.location.href = safari_iframe_site;
				}
			}

			//If we are connected to facebook application, but cookie doesn't exist, we have to reload to get this cookie.
			if (!facebook_cookie_exists()) {
				document.location.reload();
			}
			
			if (user_box) {
				$.get("/ajaxfacebook/?id=" + api.get_session().uid, null,
					function(data) {
						if (data == 1) {
							document.location.href = facebook_redirect;
						}
					}
				);
			}
			   
			// add in some XFBML. note that we set useyou=false so it doesn't display "you"
			txt.push('<div class="fb_logged">');
			txt.push('<fb:profile-pic uid="loggedinuser" facebook-logo="no" size="square" ></fb:profile-pic>');
			txt.push('<div id="fbtext">Welcome, <fb:name uid="loggedinuser" useyou="no"></fb:name><br />');
			// logout button
			txt.push('<a class="fb_logout" href="#"');
			txt.push("onclick=\"FB.Connect.logoutAndRedirect('')\">Logout</a></div>");
			txt.push('</div>');
			user_box.innerHTML = txt.join('');
			//// because this is XFBML, we need to tell Facebook to re-process the document
			FB.XFBML.Host.parseDomTree();
		}
	} catch (ex) {}
}

function facebook_post_trivia_score(score) {
	var message = "I've just " + score + " in Heineken UCL Trivia game! Want to try to beat my score?";
	facebook_post_trivia_to_wall(message);
}

function facebook_post_penalty_score(score) {
	var message = "I've just scored " + score +
					" points in the Heineken UCL Penalty Shootout game!" +
					" Want to try to beat my score?";
	facebook_post_penalty_to_wall(message);
}

function facebook_post_trivia_to_wall(message) {
	var hrefbase = 'http://' + window.location.hostname;
	var attachment =
	{
		name : 'Heineken UCL Trivia',
		description : '',
		href : "http://www.heineken.com/ie/UCL Trivia.aspx",
		media: [
		{
			type : 'image',
			src : hrefbase + '/img/inline/heineken_champions_league.jpg',
			href : "http://www.heineken.com/ie/UCL Trivia.aspx"
		}
		],
		auto_publish: true
	};

	FB.Connect.streamPublish(message, attachment, null, null, "Share score on your wall");
}

function facebook_post_penalty_to_wall(message) {
	var hrefbase = 'http://' + window.location.hostname;
	var attachment =
	{
		name : 'Heineken UCL Penalty Shootout game',
		description : '',
		href : "http://www.heineken.com/ie/UCL-Penalty.aspx",
		media: [
		{
			type : 'image',
			src : hrefbase + '/img/inline/heineken_champions_league.jpg',
			href : "http://www.heineken.com/ie/UCL-Penalty.aspx"
		}
		],
		auto_publish: true
	};

	FB.Connect.streamPublish(message, attachment, null, null, "Share score on your wall");
}


