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

function login () {
	try { console.log('login'); } catch (e) {};
	FB.login(onLogin, {perms:'user_groups,user_likes,friends_photo_video_tags,user_photo_video_tags,user_status'});
};

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

function publish (message, picture, link, name, caption, description)
{
	try { console.log("inicial publishFriend con el id: " + facebookId); } catch (e) {};
	
	FB.ui(
	{
		method: 'stream.publish',
		display: 'popup',
		message: message,
		attachment: {
			name: name,
			caption: caption,
			description: (
				description
			),
			media: [ {
				type: 'image', 
				src: picture,
				href: link
			} ],
			href: link
		},
		user_prompt_message: 'Frescura Multiplicada'
		},
		function(response) {
			if (!response || response.error)
			{
				try { console.log("error"); console.log(response); } catch (e) {};
				try { document.flashcontent.onPublish(response); } catch(e) {};
			} 
			else
			{
				try { console.log("published!"); console.log(response); } catch (e) {};
				try { document.flashcontent.onPublish(response); } catch(e) {};
			}
		}
	);
}

function getUserData ()
{
	try { console.log('get user data'); } catch (e) {};
	
	var user = FB.Data.query("SELECT uid,name FROM user WHERE uid = {0}", 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 5;", FB.getSession().uid);
	var status = FB.Data.query("SELECT message FROM status WHERE uid = {0} ORDER BY time DESC LIMIT 1", FB.getSession().uid);
	var photos = FB.Data.query("SELECT src_big FROM photo WHERE pid IN ( SELECT pid FROM photo_tag WHERE subject={0} ) ORDER BY rand() LIMIT 5", 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 3;", FB.getSession().uid);
	var pages = FB.Data.query("SELECT name FROM page WHERE page_id IN (SELECT target_id FROM connection WHERE source_id={0} and target_type='Page') ORDER BY rand() LIMIT 3", FB.getSession().uid);
	
	
	
	FB.Data.waitOn([user,friends,status,photos,groups,pages], function(response) {
		var data = {
			user: response[0][0],
			friends: response[1],
			status: response[2],
			photos: response[3],
			groups: response[4],
			pages: response[5]
		};

		try { console.log(data); } catch (e) {};
		
		document.flashcontent.onUserData(data);
	});
}
function logout () {
	FB.logout(null);
};

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

function showLike (id) {
	document.getElementById(id).style.visibility  = 'visible';
}

function hideLike (id) {
	document.getElementById(id).style.visibility  = 'hidden';
}

function moveLike (id, x, y) {
	document.getElementById(id).style.left  = (x - 25) + 'px';
	document.getElementById(id).style.top  = (y - 10) + 'px';
}
