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

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

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

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

function publish (userPath, srcImagen, perfilDescription,mediaPath,caption,message) {
	
	FB.ui(
	{
		method: 'stream.publish',
		display: 'popup',
		message: message,
		attachment: {
			name: perfilDescription,
			caption: caption,
			description: "Este mes del niño recupera tu sueño y conviértete en Rockstar, Superhéroe, Futbolista, Top model, Actriz o Princesa.",
			media: [ {
				type: 'image', 
				src: srcImagen,
				href: userPath
			} ],
			href: mediaPath
		},
		user_prompt_message: 'Recupera tu sueño'
		},
		onPublish
	);
}

function onPublish (response) {
	try { console.log(response); } catch (e) {};
	try { document.flashcontent.onPublish(response); } catch(e) {};
}

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

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

function handleSessionResponse(response) {
	try { console.log(response); } catch (e) {};
	try { document.flashcontent.handleSession(response); } catch(e) {};
}

function getUserData ()
{
	var user = FB.Data.query("SELECT uid, name, username,pic_square,first_name,last_name,sex, email FROM user WHERE uid = {0}", FB.getSession().uid);;
	var albums = FB.Data.query("SELECT aid, name, created FROM album WHERE owner = {0} AND size > 0 ORDER BY created DESC", FB.getSession().uid);
	var photos = FB.Data.query("SELECT aid, src, src_big FROM photo WHERE aid IN (SELECT aid FROM album WHERE owner = {0}) LIMIT 10", FB.getSession().uid);
	
	FB.Data.waitOn([user, albums, photos], function(response) {
		var data = {
			user: response[0][0],
			albums: response[1],
			photos: response[2]
		};
		
		try { console.log(data); } catch (e) {};
		
		document.flashcontent.onUserData(data);
	});
}

