/**
 *  This file is part of Exafm.
 *
 *  Exafm is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Exafm is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Exafm.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package public
 * @subpackage js
 * @author Óscar Palacios <oscarpalacious@gmail.com>
 * @copyright Copyright (c) 2010, Óscar Palacios
 * @license http://www.gnu.org/licenses/gpl.html GNU Public License
 *
 */
var Fbook = {
	config : null,
	sesion : null,

	/**
	 * Inicializador del API de Facebook.
	 */
	init : function(config) {
		Fbook.config = config;
		FB.init({
			appId  : Fbook.config.apikey,
			status : true,
			cookie : true,
			xfbml  : true
		});
		Fbook.switchSesion();
	},

	/**
	 * Cambia el despliegue entre un botón de
	 * login y la foto y el nombre del usuario.
	 */
	switchSesion : function() {
		if(jq(Fbook.config.contenedorLogin).length > 0) {
			var facebookHTML = '';
			FB.getLoginStatus(function(respuesta) {
				Fbook.sesion = respuesta.session;
				var titulo = document.title;
				var color = 'white';
				if(titulo.substr(0,9) == 'EXA fm, e') {
					color = '#FF9900';
				}
				if(respuesta.status == 'connected') {
					var facebookHTML;
					facebookHTML  = '<fb:profile-pic uid="loggedinuser" facebook-logo="true" width="20" height="20" style="float: left;">';
					facebookHTML += '</fb:profile-pic><div style="float:left; padding-left:10px; margin-top:5px;"><font color="'+color+'">Bienvenido, </font>';
					facebookHTML += '<fb:name uid="loggedinuser" useyou="false"></fb:name>';
					facebookHTML += '<a href="javascript:Fbook.logout();">';
					facebookHTML += '<font color="'+color+'">&nbsp;&#124;&nbsp;Cerrar sesi&oacute;n</a></font><br /></div>';
					jq('#facebookLogin').html(facebookHTML);
					jq('#facebookConnect').html('');
					jq('#ingresaFB').html('');
					if(titulo.substr(0,9) == 'EXA fm, e') {
						jqscripts.exactoCargarPlayList(Fbook.sesion.uid);
					}
					if(jq(Fbook.config.contenedorComentarios).length > 0) {
						jq('#comentario_contenido').removeAttr('disabled');
						jq('#comentario_contenido').html('');
						jq('#postear').removeAttr('disabled');
						jq('#cancelar').removeAttr('disabled');
						jq('#publicar_wall').removeAttr('disabled');
						jq('#comentario_connect').html('');
					}
				} else {
					if(jq('#facebookLogin').length > 0) jq('#facebookLogin').html('<fb:login-button onlogin="Fbook.switchSesion();" perms="publish_stream,offline_access" width="180"></fb:login-button>');
					if(jq('#facebookConnect').length > 0) jq('#facebookConnect').html('<fb:login-button onlogin="Fbook.switchSesion();" perms="publish_stream,offline_access" width="180"></fb:login-button>');
					if(jq('#ingresaFB').length > 0) jq('#ingresaFB').html('Ingresa usando tu cuenta de Facebook');
					if(jq('#comentario_connect').length > 0) jq('#comentario_connect').html('<fb:login-button width="180" onlogin="Fbook.switchSesion();" perms="publish_stream,offline_access"></fb:login-button><span style="margin: 5px 0 0 10px;" class="gris">Utiliza tu cuenta de Facebook para dejar comentarios.</span>');
				}
				FB.XFBML.parse(null, function() {
					jq('.fb_link').css('color', color);
				});
			});
		}
	},

	comentarioEnviar : function() {
		var comentario = jq('#comentario_contenido').val();
		var enviarWall = jq('input[id=publicar_wall]:checked').length;
		if(comentario.length > 0) {
			FB.getLoginStatus(function(respuesta) {
				Fbook.sesion = respuesta.session;
				if(respuesta.status == 'connected') {
					if(enviarWall > 0) {
						FB.api('/'+Fbook.sesion.uid+'/feed', 'post', { message: comentario }, function(respuesta) {
							if(!respuesta || respuesta.error) {
								alert('Hubo un error al intentar enviar tu comentario a Facebook.');
							} else {
								alert('Se envi\363 tu comentario a tu muro.');
							}
						});
					}
					jq.ajax({
						url      : conf.url_scripts+'comentarioEnviar.php',
						type     : 'post',
						data     : {
							comentario  : comentario,
							contexto    : jq('#contexto').attr('name'),
							contexto_id : jq('#contexto').val(),
							facebook_id : Fbook.sesion.uid
						},
						dataType : 'html',
						success  : function(response) {
							jq('#comentarios_lista').prepend(response);
							jq('#comentario_contenido').val('');
							FB.XFBML.parse();
						}
					});
				} else {
					//
				}
			});
		} else {
			alert('Nos interesa su comentario. De veras.');
		}
	},

	logout : function() {
		FB.logout(function(response) {
			Fbook.switchSesion();
		})
	}

};

