/*
 * FacebookComments
 * Class JS to post comments on Facebook
 * It has been totally rewritten to adapt to the new SDK : https://developers.facebook.com/docs/reference/javascript/FB.ui/
 * @author Valentin Clavreul <valentin.clavreul@smile.fr>
 * @version 2.0
 **/
var FacebookComments = {

	// ==== PARAMETERS ====
	fb_init : false,
	app_id : false,
	status : true,
	cookie : true,
	oauth : true,
	defaults : {
		method : 'feed',
		name : Liferay.Language.get('tribe-of-artists'),
		description : Liferay.Language.get('common-facebook'),
		link : window.location.href,
		picture : window.location.protocol + "//" + window.location.host
				+ '/community-theme/images/logolatribu.jpg'
	},
	default_locale : 'fr_FR',
	init_options : {},

	// ===== FUNCTIONS =====
	init : function(app_id) {
		this.app_id = app_id;
		this.init_options = {
			appId : this.app_id,
			status : this.status,
			cookie : this.cookie,
			oauth : this.oauth
		};
		this._loadSDK();
		this._initFacebook();
	},
	_loadSDK : function(locale) {
		if (locale === undefined) {
			locale = this.default_locale;
		}
		var js, id = 'facebook-jssdk';
		if (document.getElementById(id)) {
			return;
		}
		js = document.createElement('script');
		js.id = id;
		js.src = "//connect.facebook.net/" + locale + "/all.js";
		document.getElementsByTagName('head')[0].appendChild(js);
	},
	_initFacebook : function() {
		if (this.fb_init) {
			return true;
		} else {
			if (this.app_id) {
				window.fbAsyncInit = function() {
					FB.init(FacebookComments.init_options);
					this.fb_init = true;
				}
				return true;
			} else {
				this.fb_init = false;
				return false;
			}
		}
	},
	publishComment : function(settings) {
		if (settings == null || typeof settings != 'object') {
			settings = {};
		}
		var options = settings;
		for (param in this.defaults) {
			if (options[param] === undefined) {
				options[param] = this.defaults[param];
			}
		}

//		if (options.commentHeaderText !== undefined
//				|| options.image !== undefined) {
//			console.log('Button deprecated !');
//		}

		if (this._initFacebook()) {
			FB.ui(options);
		}
	}
};
