/* gigya.js */

var GG_conf =
{
	/* APIKey per dbergueda.cat */
APIKey: '2_d85x1rlGaMvwbwe3ZegOpmrTlCGUJuVNlljwZMRrgjeIEWpVPAdgNxcC8momYBYy',
enabledProviders: 'facebook,twitter',
lang: 'es'
};

var GG_dom = window.location.hostname;

// Comprovem domini
if ((GG_dom.indexOf("radioberga.cat") == 0) || (GG_dom.indexOf("radioberga.cat") == 4)) {
	/* APIKey per radioberga.cat */
	GG_conf.APIKey = "2_mBPGtMsDD_rHg9S9iRDE9dQ04uvP4IUuHncxyvTkkIdkJpXyOkNcQSJP_EXUL90O";
}

var GG_params =
{
	height:20,width:54,showTermsLink:false, // remove 'Terms' link
	hideGigyaLink:true, // remove 'Gigya' link
	useHTML:true,  // Use the HTML implementation of the Plugin (rather then Flash implementation)
	containerID: "divConnect", // The component will embed itself inside the divConnect Div 
	UIConfig: '<config><body><controls><snbuttons buttonsize="20"></snbuttons></controls></body></config>'
};

function GG_Inicialitza() {
	// Obtenim info d'usuari
	gigya.services.socialize.getUserInfo(GG_conf, { callback: GG_renderUI });

	// Endollem funcions a executar quan hi hagi canvis en l'estat de connexió
	gigya.services.socialize.addEventHandlers(GG_conf,{ onConnectionAdded: GG_renderUI, onConnectionRemoved: GG_renderUI });
	gigya.services.socialize.addEventHandlers(GG_conf,{ onConnectionAdded: DB_mostra_form_comenta, onConnectionRemoved: DB_mostra_form_comenta });

	// Mostrar frase perquè s'identifiqui
	document.getElementById("p_identifica_t").style.display = "block";
	document.getElementById("divConnect").style.float = "left";
}

function GG_renderUI(res) {
	if (res.user != null && res.user.isConnected) {
		document.getElementById("p_identifica_t").style.display = "none";
		document.getElementById("divConnect").style.display = "none";
		document.getElementById("usuari_nom").innerHTML = res.user.nickname;
		if (res.user.thumbnailURL.length > 0) {
			document.getElementById("usuari_foto").src = res.user.thumbnailURL;
		}
		else {
			document.getElementById("usuari_foto").src = "http://cdn.gigya.com/site/images/bsAPI/Placeholder.gif";
		}
		document.getElementById("profile").style.display = "block";
	}
	else {
		document.getElementById("profile").style.display = "none";
		// Tornem a mostrar llista de xarxes
		document.getElementById("divConnect").style.display = "block";
		gigya.services.socialize.showAddConnectionsUI(GG_conf,GG_params);
		document.getElementById("p_identifica_t").style.display = "block";
	}
}	

function DB_comenta() {
	gigya.services.socialize.getUserInfo(GG_conf, { callback: DB_mostra_form_comenta });
}

function DB_mostra_form_comenta(res) {

	if (res.user != null && res.user.isConnected) {
		document.getElementById("noticia_form_comenta").style.display = "block";
		document.getElementById("noticia_form_comenta_uid").value = res.user.identities[res.user.providers[0]].providerUID;
		document.getElementById("noticia_form_comenta_usr").value = res.user.nickname;
		document.getElementById("noticia_form_comenta_img").value = res.user.thumbnailURL;
		document.getElementById("noticia_form_comenta_xrx").value = res.user.providers[0];
		document.getElementById("noticia_form_corregeix").style.display = "none";
		document.getElementById("noticia_form_comparteix").style.display = "none";
	}
	else {
		if (document.getElementById('noticia_form_comenta_uid').value == "uid") {
			alert("Has d'identificar-te per poder comentar");
		}
		document.getElementById('noticia_form_comenta').style.display = "none";
		document.getElementById('noticia_form_comenta_uid').value = "uid";
		document.getElementById('noticia_form_comenta_usr').value = "usr";
		document.getElementById("noticia_form_comenta_img").value = "img";
		document.getElementById("noticia_form_comenta_xrx").value = "xrx";
	}

}

function DB_comparteix() {
	document.getElementById("noticia_form_comparteix").style.display = "block";
	document.getElementById("noticia_form_corregeix").style.display = "none";
	document.getElementById("noticia_form_comenta").style.display = "none";
}

function DB_corregeix() {
	document.getElementById("noticia_form_corregeix").style.display = "block";
	document.getElementById("noticia_form_comparteix").style.display = "none";
	document.getElementById("noticia_form_comenta").style.display = "none";
}




