//inicio
function init () {
	FB.init({appId: flashvars.apiKey, status: true, cookie: true, xfbml: true, status: true, logging: true});
}

function checkLogin () {
	return FB.getSession();
}


function login () {
	FB.login(onLogin, {perms:'user_birthday,user_photos,user_status,user_groups,user_photo_video_tags,friends_photos,email'});
};

function onLogin (response) {
	try {
		document.flashcontent.onLogin(response);
	} catch(e) {};
}

function logout () {
	FB.logout(null);
};

function disconnect () {
	if (FB.getSession()) {
		FB.api({ method: 'Auth.revokeAuthorization' });
	}
};

//----------------------------------------------------

function getUserData ()
{
	var user = FB.Data.query("SELECT uid, name, first_name, last_name, birthday_date, sex, email FROM user WHERE uid = {0}", FB.getSession().uid);
	var profile_pictures = FB.Data.query("SELECT src_big FROM photo WHERE aid in("
	+ "SELECT aid FROM album WHERE owner = {0} AND type = 'profile' LIMIT 3"
	+ ") ORDER BY rand() LIMIT 10;", FB.getSession().uid);
	var tagged_pictures = FB.Data.query("SELECT src_big FROM photo WHERE pid IN("
	+ "SELECT pid FROM photo_tag WHERE subject = {0} LIMIT 3"
	+ ") ORDER BY rand() LIMIT 10;", FB.getSession().uid);
	var friends = FB.Data.query("SELECT uid, name FROM user WHERE uid IN ("
	+ "SELECT uid2 FROM friend WHERE uid1 = {0}"
	+ ") ORDER BY rand() LIMIT 10;", FB.getSession().uid);
	var status = FB.Data.query("SELECT message FROM status WHERE uid = {0} AND message <> '' LIMIT 1", FB.getSession().uid);
	var groups = FB.Data.query("SELECT name FROM group WHERE gid IN ("
	+ "SELECT gid FROM group_member WHERE uid =  {0}"
	+ ") ORDER BY rand() LIMIT 10;", FB.getSession().uid);
	var pages = FB.Data.query("SELECT name FROM page WHERE page_id IN ("
	+ "SELECT page_id FROM page_fan WHERE uid =  {0}"
	+ ") ORDER BY rand() LIMIT 10;", FB.getSession().uid);
	
	FB.Data.waitOn([user, profile_pictures, tagged_pictures, friends, status, groups, pages], function(response) {
		var data = {
			user: response[0][0],
			friends: response[3],
			status: response[4][0],
			groups: response[5],
			pages: response[6]
		};
		
		data.pictures = response[1].concat(response[2]);
		data.pictures.splice(3);
		
		try {
			document.flashcontent.onUserData(data);
		} catch(e) {};
		});
}
