/**
 * 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
 * @param (optional) width
 * @param (optional) height
 * @param (optional) show faces
 * @param (optional) css styles to be overriden
 * @param (optional) string Facebook App ID (not the key)
 */
function facebookLikeSetButton(divId, likeUrl, width, height, showFaces, overrideStyles, appId) 
{
	likeUrl = encodeURIComponent(likeUrl);

	/* obsolete
	var iframeSrc = 'http://www.facebook.com/plugins/like.php?href=' + likeUrl + '&amp;action=like&amp;channel_url=http%3A%2F%2Fstatic.ak.fbcdn.net%2Fconnect%2Fxd_proxy.php%23cb%3Df48d111c28ce4%26origin%3D' + likeUrl + 
									'%26relation%3Dparent.parent%26transport%3Dpostmessage&amp;colorscheme=light&amp;layout=standard&amp;locale=en_US&amp;node_type=link&amp;sdk=joey';
	*/

	var iframeSrc = 'http://www.facebook.com/plugins/like.php?href=' + likeUrl + '&amp;layout=standard&amp;action=like&amp;colorscheme=light';

	var iframeStyle = 'border:none; overflow:hidden;';

	// Width is 450px by default
	if (width !== undefined) {
		iframeSrc 	+= '&amp;width=' + width;
		iframeStyle += 'width: ' + width + 'px;';
	} else {
		iframeSrc 	+= '&amp;width=450';
		iframeStyle += 'width: 450px;';
	}

	// Height is 80px by default
	if (height !== undefined) {
		iframeSrc 	+= '&amp;height=' + height;
		iframeStyle	+= 'height: ' + height + 'px;';
	} else {
		iframeSrc 	+= '&amp;height=80';
		iframeStyle += 'height: 80px;';
	}

	// Show Faces is true by default
	if (showFaces !== undefined) {
		iframeSrc += '&amp;show_faces=' + showFaces;
	} else {
	 	iframeSrc += '&amp;show_faces=true';
	}
	
	// Add the API key if specified
	if (appId !== undefined) {
		iframeSrc += '&amp;api_key=' + appId;
	}
	
	if (overrideStyles !== undefined) {
		iframeStyle += overrideStyles;
	}
	
	// Construct the final IFrame HTML
	var iframeHTML = '<iframe scrolling="no"' +
												   'frameborder="0"' +
												   'allowTransparency="true"' +
												   'src="' + iframeSrc + '"' +
												   'style="' + iframeStyle + '">' +
									 '</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);
		  	}
		  }
	});
}
