/**
 * Facebook Social Plugins functions
 *
 * These are a set of Javascript functions to implement Social Plugins.
 * Original docs: http://developers.facebook.com/plugins.
 *
 * Usage:
 * 	facebookInit(<your application id>) // wrapper to FB.init()
 * 	<plugin's functions> 
 *
 * @copyright Globalmind S.A.
 * @available since first release
 */

/**
 * Initialize Facebook's Javascript SDK
 *
 * @param string Application Id (not the same as Application key or Secret)
 */
function facebookInit(appId)
{
	FB.init({appId: appId, 
		 		   status: true, 
		 			 cookie: true,
         	 xfbml: true});  
}

/**
 * Sets a Facebook Like button given an URL from where Facebook will pull all data
 * and a div Id to place it.
 *
 * @param string div id attribute
 * @param string URL with Facebook metas
 */
function facebookLikeSetButton(divId, likeUrl) 
{
	likeUrl = encodeURIComponent(likeUrl);
	var iframeHTML = '<iframe src="http://www.facebook.com/plugins/like.php?href=' + likeUrl + '&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>';
	jQuery('#' + divId).html(iframeHTML);
}

/**
 * Sets a Facebook Login button (similar to the old Connect) inside our
 * div with the id's attribute specified in divId.
 *
 * @param string div id's attribute where we're setting the Login button
 * @param bool show friends' faces or not
 * @param string width
 * @param string max rows
 */
function facebookLoginSetButton(divId, showFaces, width, maxRows)
{
	// Check if the user is logged in (if so, we won't show the login button)
	FB.getLoginStatus(function(response) {
		if (response.session) {
				// User is already logged in, don't show the button
	  	} else {
	  		// User is not logged in, show the loggin button
				var strShowFaces;
			
				showFaces == true ? strShowFaces = 'true' : strShowFaces = 'false';
				
				var buttonCode = '<fb:login-button show-faces="' + strShowFaces + '" width="' + width + '" max-rows="' + maxRows + '"></fb:login-button>';
			
				jQuery('#' + divId).html(buttonCode);
				
				// This makes Facebook parse the code we've just added inside the
				// div with id=<divId>
				FB.XFBML.parse();
	  	}
	});
}

/**
 * Shows the Facebook Social Plugin 'Friendpile'
 *
 * @param div id where we are showing the plugin
 * @param string number of rows to show
 * @param string width
 */
function facebookFriendpile(divId, maxRows, width)
{
	var friendPile = '<fb:friendpile max-rows="' + maxRows + '" width="' + width + '"></fb:friendpile>';

	jQuery('#' + divId).html(friendPile);
	
	FB.XFBML.parse();
}


/**
 * Loads Facebook's comment plugin inside a div.
 *
 * @param string div id where we are loading the plugin
 * @param string unique identifier for the contents we are offering comments on
 * @param string number of posts to show
 * @param string width
 */
function facebookCommentsSet(divId, xid, posts, width)
{
	var comments = '<fb:comments xid="' + xid + '" numposts="' + posts + '" width="' + width + '"></fb:comments>';

	jQuery('#' + divId).html(comments);
	
	FB.XFBML.parse();
}


/**
 * Shows the Facebook Like Box. "The Like Box is a social plugin that enables 
 * Facebook Page owners to attract and gain Likes from their 
 * own website." (extracted from facebook.com)
 *
 * @param div id where we are loading the Like Box
 * @param string Fan Page Id
 * @param string width
 * @param string height
 * @param string number of users that already liked this page
 * @param string (optional) show stream, defaults to false
 * @param string (optional) show header, defaults to true
 */
function facebookLikeBoxSet(divId, fanpageId, width, height, 
													  connections, stream, header)
{
	var likeBox = '<iframe src="http://www.facebook.com/plugins/likebox.php?' + 
														'id=' + fanpageId + 
												    '&amp;width=' + width + 
												    '&amp;connections='+ connections;
												    '&amp;height=' + height; 
	
	stream === undefined ? likeBox += '&amp;stream=false' : likeBox += '&amp;stream=' + stream;
	 
	header === undefined ? likeBox += '&amp;header=true' : likeBox += '&amp;header=' + header;  

	likeBox += '" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:587px;" allowTransparency="true"></iframe>';

	jQuery('#' + divId).html(likeBox);
	
	FB.XFBML.parse();
}

/**
 * Pop up the Facebook Login window as the old fashioned Connect with Facebook
 * button used to do.
 *
 * @param (optional) callback with param = true for logged in or false otherwise
 */
function facebookLogin(callback)
{
	// Check if the user is logged in (if so, we won't show the login dialog again)
	FB.getLoginStatus(function(response) {
		if (response.session) {
			callback(true);
			return;
		}
	});
	
	// User is not logged in
	FB.login(function(response) {
		response.session ? true : false;
		  if (response.session) {
		  	if (callback !== undefined) {
		  		callback(true);
		  	}
		  } else {
		  	if (callback !== undefined) {
		  		callback(false);
		  	}
		  }
	});
}
