Shadowbox.init({
	displayNav : false,
	showOverlay : true,
	overlayColor : "#272727",
	players:  ['html', 'iframe'] 
});

var redirectLogin = false;

function showLogin(divId, frmId, serviceId) {
	// PTF code
	if (nick != null) {
		var lc = '<div id="' + divId + 'Data"></div><input type="button" value="Surt" onclick="prepareForLogout();doLogoutRu(\''
				+ frmId
				+ 'Out\',\''
				+ frmId
				+ 'OutService\')" />'
				+ '<form name="' + frmId + 'Out" id="' + frmId + 'Out" action="' + URL_CAS_SECURE_LOGOUT + '" method="get">'
				+ '<input type="hidden" name="gateway" value="true" />'
				+ '<input type="hidden" id="' + frmId + 'OutService" name="service" value="" /></form>';

		var ctrl = document.getElementById(divId);
		if (ctrl != null) {
			ctrl.innerHTML = lc;
		}
		doGetUserData(divId+'Data',"actualitzaAltresDades()");
		//doGetUserData(divId+'Data');
		doAfterLogin();
		
	}
	
	else{

		//if(document.location.href.match('http://www.324.cat/pnoticies/1024ParticipacioRU.jsp')){
		if(document.location.href.match('http://www.324.cat/envia')){
			showElement("e_42_upload");
			hideElement("e_42_upload_logged");
			showElement("menu_deixans_b");
			showElement("e_42_upload_publicitat");
		}
		/*** INICI elmeu3cat24 + commenting ***/
		hideElement("cgu_commenting_logged");
		showElement("cgu_commenting");
		/*** FI elmeu3cat24 + commenting ***/	
		
		/*** INICI porra oscars 2012 ***/
		hideElement("participa_oscars12_logged");
		showElement("participa_oscars12");
		setValueElement("action_porraoscars12", "2122");
		/*** FI porra oscars 2012 ***/	
		
	}
	
	showLoginCommenting();
}

function actualitzaAltresDades(){
	rewriteElement('enviaLoggedNick', ccrtvi_nom +' '+ ccrtvi_cognom);
}


function enviaLoginUploadRU(){
	if (nick == null) {
		Shadowbox.open({player:"iframe",content:'http://www.324.cat/su/ru/common_login.jsp?hiRegServiceIdParam=CITN_REG',width:696,height:551});
	}
} 

// ------ START LOGOUT
function doLogoutRu(frmId, serviceId) {
	var frmLogout = document.getElementById(frmId);
	var ctrlService = document.getElementById(serviceId);
	if (ctrlService!=null) {
		ctrlService.value = escape(window.location);
	}
	if (frmLogout!=null) {
		var ssoIframe = document.getElementById("logoutFrame");
		bIframeWaitingResult = false;
		ssoIframe.src= URL_CAS_SECURE_LOGOUT;
	}
}
// ------ END LOGOUT

// ------ START CLEAN LOGOUT FRAME
function cleanLogoutIFrame() {
	var logoutFrame = document.getElementById("logoutFrame");
	if(logoutFrame.src==URL_CAS_SECURE_LOGOUT) {
		var ifrId = document.getElementById("ifrId");
		doValidate('divLogin','frmLogin','ctrlServiceLogin','frmPostIframe','ifrId');
		rewriteElement("divDadesUsuari",'');
		rewriteElement("divLogin",'<a rel="shadowbox[maqueta];width=696;height=551" href="http://www.324.cat/su/ru/common_login.jsp?hiRegServiceIdParam=CITN_REG" class="pngFix">Inicia sessi\u00f3</a>');
		showElement("registerLi");

		//if(document.location.href.match('http://www.324.cat/pnoticies/1024ParticipacioRU.jsp')){
		if(document.location.href.match('http://www.324.cat/envia')){
			showElement("e_42_upload");
			hideElement("e_42_upload_logged");
		}	

		/*** INICI amaga els botons denuncia comentari ***/
		jQuery(".thickbox").addClass("none");
		/*** FI amaga els botons denuncia comentari ***/
		
		logoutFrame.src="";
		resetShadowbox();
	}
}
// ------ END CLEAN LOGOUT FRAME

function resetShadowbox() {
	Shadowbox.clearCache(); 
	Shadowbox.setup();
}

function doAfterLogin() {
	hideElement("registerLi");

	if(document.location.href.match('http://www.324.cat/envia')){
		usuariTotesDades();
	}

	if(redirectLogin){
		redirectLogin = false;
		window.location="http://www.324.cat/envia";
	}
	
	/*** INICI elmeu3cat24 + commenting ***/
	showElement("cgu_commenting_logged");
	hideElement("cgu_commenting");
	/*** FI elmeu3cat24 + commenting ***/
	
	
	/*** INICI elmeu3cat24 votacions ***/
	hideElement("noLogat"); 
	/*** FI elmeu3cat24 votacions ***/
	
	
	/*** INICI mostra els botons denuncia comentari ***/
	jQuery(".thickbox.none").removeClass("thickbox none").addClass("thickbox");
	/*** FI mostra els botons denuncia comentari ***/
	
	/*** INICI porra oscars 2012 ***/
	showElement("participa_oscars12_logged");
	hideElement("participa_oscars12");
	setValueElement("action_porraoscars12", "123");
	/*** FI porra oscars 2012 ***/	
	
}

function prepareForLogout() {
	rewriteElement("divDadesUsuari", "Desconnectant ...");
}
	
function showElement( elementId ) {
  if(document.getElementById(elementId))
	document.getElementById(elementId).style.display="block";
}

function hideElement( elementId ) {
  if(document.getElementById(elementId))
	document.getElementById(elementId).style.display="none";
}

function rewriteElement( elementId, newContent ) {
  if(document.getElementById(elementId))
	document.getElementById(elementId).innerHTML = newContent;
}

function setValueElement( elementId, newValue ) {
	  if(document.getElementById(elementId))
		document.getElementById(elementId).value = newValue;
}

function setValueElementIframe( elementId, newValue ) {
	if (document.getElementById('iframeCommentLogin')){
	  if(iframeCommentLogin.document.getElementById(elementId))
		  iframeCommentLogin.document.getElementById(elementId).value = newValue;
	}
}



function showNameIframe( elementId ) {
	if (document.getElementById('iframeCommentLogin')){
	  if(iframeCommentLogin.document.getElementById(elementId))
		  iframeCommentLogin.document.getElementById(elementId).innerHTML = ccrtvi_nom +' '+ ccrtvi_cognom;
	}
}

function showNameIframeDenuncia( elementId ) {
	if (document.getElementById('TB_iframeContent')){
		var iframe = document.getElementById('TB_iframeContent');
		if(iframe.contentWindow.document.getElementById(elementId))
		  iframe.contentWindow.document.getElementById(elementId).innerHTML = ccrtvi_nom +' '+ ccrtvi_cognom;
	}
}



function showLoginCommenting() {}

function showLoginElMeu3catRU() {
	if (nick != null) {
		usuariTotesDades();
	}
	else{
		Shadowbox.open({player:"iframe",content:'http://www.324.cat/su/ru/common_login.jsp?hiRegServiceIdParam=CITN_REG',width:696,height:551});
		redirectLogin = true;
	}
}

function usuariTotesDades(){
	jQuery.ajax({
        type: "GET",
        async: false,
        dataType:"xml",
        //url: "/pnoticies/1024Elmeu3cat24ComprovarDadesUsuari.jsp",
        url: "/su/Register?hiTarget=*.swf&hiPortal=not&hiRegServiceId=CCMA_REG&hiErrTarget=*.swf&hiAction=121&hiServiceId=CCMA_REG&hiRetrievalXsl=login.xsl&domainId=NOT&hiScope=ELMEU324&" + Math.random(),
      	success: function(xml) {	      	
			var code = jQuery(xml).find('code').text();
	      	if (code.toString()=="err.service.missing-values"){
	      		window.location="https://secure.ccrtvi.com/su/Register?hiAction=1024&hiPortal=ru&hiRegServiceId=CCMA_REG&hiServiceId=CCMA_REG&hiTarget=ruModifica.jsp&hiOrigin=www.324.com&hiErrTarget=error.jsp&hiScope=ELMEU324";
	      	}
	      	else{
	      		if(document.location.href == 'http://www.324.cat/elmeu324/' || document.location.href == 'http://www.324.cat/elmeu324'
	      			|| document.location.href == 'http://www.324.cat/' || document.location.href == 'http://www.324.cat'
	      			|| document.location.href.match('http://www.324.cat/elmeu324/text')
	      			|| document.location.href.match('http://www.324.cat/elmeu324/video')
	      			|| document.location.href.match('http://www.324.cat/elmeu324/audio')
	      			|| document.location.href.match('http://www.324.cat/elmeu324/foto')
	      			|| document.location.href.match('http://www.324.cat/pnoticies/1024ItemCommentCGU.jsp')
	      			){
	      			//window.location="http://www.324.cat/pnoticies/1024ParticipacioRU.jsp";
	      			window.location="http://www.324.cat/envia";
	      			
	      		}
	      		//else if(document.location.href.match('http://www.324.cat/pnoticies/1024ParticipacioRU.jsp')){
	      		else if(document.location.href.match('http://www.324.cat/envia')){
		      		hideElement("e_42_upload");
		    		showElement("e_42_upload_logged");
		    		showElement("menu_deixans_b");
		    		showElement("e_42_upload_publicitat");
	      		}
	      	}
      	}
	});    
}

function validacioScroon(action,dataType,params, funcionRetorno){

	var url = "/pnoticies/scroon.jsp";
	if (typeof(action) != "undefined" && action != ""){
		url += "?action=" + action; 
	}
		
	if (typeof(params) != "undefined" && params != ""){
		url+=params;
	}
	
	var dataTypeAux = "xml";
	if (typeof(dataType) != "undefined" &&  dataType != ""){
		dataTypeAux = dataType;
	}
	
	
	jQuery.ajax({
        type: "GET",
        async: false,
        dataType:dataTypeAux,
        url: url,
      	success: function(xml) {
      		if("vot" == action){
      			funcionRetorno(xml);
      		}
      		else if("comentari" == action){
      			funcionRetorno(xml);
      		}
      		else if("denuncia" == action){
      			funcionRetorno(xml);
      		}	
      		else{
	      		var user = jQuery(xml).find('user').text();
	      		var userid = jQuery(xml).find('userid').text();
	      		var key = jQuery(xml).find('key').text();
	      		setValueElement( 'userVideo', user );
	      		setValueElement( 'keyVideo', key );
	      		setValueElement( 'userAudio', user ); 
	      		setValueElement( 'keyAudio', key );
	      		setValueElement( 'userFoto', user );
	      		setValueElement( 'keyFoto', key );
	      		setValueElement( 'userText', user );
	      		setValueElement( 'keyText', key );
	      		/*** INICI elmeu3cat24 + commenting ***/
	      		setValueElementIframe( 'user.uid', userid );
	      		setValueElementIframe( 'user.username', userid );
	      		/*** FI elmeu3cat24 + commenting ***/
	      		validacioScroon = true;
      		}
      		
      	}
	});  

}

function nouVotRU(img, media, hit){
	if (nick) {
		var elemento = document.getElementById(img.id);
		var pos = getAbsoluteElementPosition(elemento);
		var capa = document.getElementById("NouVot");
		capa.className='';
		capa.style.top=(pos.top+15) + "px";
		capa.style.left=(pos.left-30) + "px";  
		capa.style.position='absolute';
		validacioScroon("vot","html","&item="+media+"&hit=" +hit +"&name="+ccrtvi_nom+"&lastname=" + ccrtvi_cognom ,votResultat);		
	}
}

function votResultat(xml){
	document.getElementById ('votMarc').innerHTML = xml;
}

function comentariResultat(xml){
	document.getElementById ('teu_comentari').innerHTML = xml;
	document.getElementById("tornarComment").style.display="";
	hideElement("avis");
}

function validaCommentScroon(media){
	var commentari = document.getElementById("missatge");
	if(!isRequiredFieldEmpty(commentari,'commentari')){		
	}else{ return false; }
	validacioScroon("comentari","html","&media="+media+"&comentari=" + commentari.value +"&name="+ccrtvi_nom+"&lastname=" + ccrtvi_cognom, comentariResultat);
}

