// Funcions Pragma Backoffice
// Isaac Roca - iroca at pragmapublicitat dot cat

carregant='<center><img style="float:none; margin-top:200px;" src="'+wwwroot+'/img/carregant_'+canal+'.gif"><br />'+carregant+'</center>';

var showCarregant = function() {
		jQuery('#contingut').html(carregant); 
		jQuery('#contingut').show();
	}
	

var redirect = function(o) {
	//alert('RESPONSETEXT: '+o);
	arr=o.split("|");
	if (arr[1]==undefined) { 
		alert('En construcción'); 
	} else {
		if (arr[1]!='') { alert(arr[1]); } 
	} 
	if(parseInt(arr[0])) {
		var str=""; for (i=2;i<arr.length;i++) str+=arr[i]+"|";
		jQuery('#contingut').html(str.substr(0,str.length-1));
		ajaxiza();
		jsload(sec);
	}
}


var redirectLogin = function() {
	//alert('RESPONSETEXT: '+o);
	document.location='/'+canal;
}

/* funcions admin */

function editar(ident) {
		//alert(ident);
		sec=$('sec').value;
		showCarregant();
		document.location="/"+canal+"/"+sec+"/?id="+ident;
		//jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: 'canal='+canal+'&sec='+sec+'&id='+ident, success: redirect });
	}

/*function afegir_poblacio(ident,poblacio) {
		sec=$('sec').value;
		showCarregant();
		document.location="/"+canal+"/"+sec+"/?id="+ident+'&poblacio='+poblacio+'&accio=afegir_poblacio';
	}

function esborra_poblacio(ident,poblacio) {
		sec=$('sec').value;
		showCarregant();
		document.location="/"+canal+"/"+sec+"/?id="+ident+'&poblacio='+poblacio+'&accio=esborrar_poblacio';
		//jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: 'canal='+canal+'&sec='+sec+'&id='+ident, success: redirect });
	}
*/
	
function afegir_poblacio() {
	
	pobName = jQuery('#poblacio :selected').text();
	pobID = jQuery('#poblacio').val();
	
	if (parseInt(pobID)&&jQuery('table.zona_poblacions').find('#pob'+pobID+'_data').length==0) {
	
		strAdd = '';
		strAdd += '<tr id="pob'+pobID+'_data"><td><p>'+pobName+'</p></td>';
		strAdd += '<td><input name="llistapob[]" type="hidden" value="'+pobID+'" />';
		strAdd += '<img style="cursor:pointer;" src="/img/delete.png" onclick="esborra_poblacio('+pobID+');" /></td></tr>';
		
		jQuery('table.zona_poblacions').prepend(strAdd);
	} else {
		alert('Municipi ja afegit');
	}

}	

function esborra_poblacio(poblacio) {
		jQuery('#pob'+poblacio+'_data').remove();
		
}

function esborrar_pdf(ident) {
		sec=$('sec').value;
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: 'canal='+canal+'&sec='+sec+'&id='+ident+'&accio=esborrar_pdf', success: redirect });
	}

function llistar(nom,camp,ordre,pagina) {
	sec=$('sec').value;
	jQuery('#'+nom).html("Carregant...");
	jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: 'accio=llistar&canal='+canal+'&sec='+sec+'&camp='+camp+'&ordre='+ordre+'&pagina='+pagina, success: function(o) {
		jQuery('#'+nom).html(o);
	} });

}

function logout() {
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: str+'&accio=logout&canal='+canal, success: redirectLogin });
		return false;
	}
	
function login() {
		var str = $('formulari').serialize();
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: str+'&accio=login&canal='+canal, success: redirectLogin });
		return false;
	}

function guardar() {
		var str = $('formulari').serialize();
		// alert(str);
		showCarregant();

		jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: str+'&accio=guarda&canal='+canal, success: redirect });
		return false;
	}

function enviar() {
		var str = $('formulari').serialize();
		// alert(str);
		showCarregant();

		jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: str+'&accio=enviar&canal='+canal, success: redirect });
		return false;
	}

function esborrar(mensaje) {
		if (mensaje) if (!confirm(mensaje)) return 0;
		var str = $('formulari').serialize();
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: str+'&accio=esborra&canal='+canal, success: redirect });
	}

function esborrar_imatge(mensaje,banner) {
		if (mensaje) if (!confirm(mensaje)) return 0;
		var str = $('formulari').serialize();
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: str+'&accio=esborra_imatge&banner='+banner+'&canal='+canal, success: redirect });
	}

function valorsLlistaUsuari() {
		var v = { 
			mostrar: $F('mostrar'), 
			ordenarper: $F('ordenarper'), 
			ordre: $F('ordre'), 
			pagina: $F('pagina') 
		};
	
	return v;	
}
	
function llistaUsuaris() {
		var v=valorsLlistaUsuari();
		
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: 'canal='+canal+'&sec='+sec+'&mostrar='+v.mostrar+'&ordenarper='+v.ordenarper+'&ordre='+v.ordre+'&pagina='+v.pagina+'&id=-1', success: redirect });
}

function checkPass() {
		($F('password1').length>3)? jQuery('#password1').css("background-color","#74FF60") : jQuery('#password1').css("background-color","#FF7070");
		(($F('password1').length>3)&&($F('password1')==$F('password2')))? jQuery('#password2').css("background-color","#74FF60") : jQuery('#password2').css("background-color","#FF7070");
}

function editPass() { 
	jQuery('#chpass').html('<p>'+noupassword+': <br /><input onkeyup="checkPass();" name="password1" id="password1" style="width:200px;" type="password" value=""  /></p><p>'+repeticio+': <br /><input onkeyup="checkPass();" name="password2" id="password2" style="width:200px;" type="password" value=""  /></p>');
	checkPass();
}

function editarUsuari(ident) {
		var v=valorsLlistaUsuari();
		
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: 'canal='+canal+'&sec='+sec+'&mostrar='+v.mostrar+'&ordenarper='+v.ordenarper+'&ordre='+v.ordre+'&id='+ident, success: redirect });
}
	

var menusOberts=[];

function actualitzaMenusOberts() {
	menusOberts=[];
	jQuery("ul.menuRoot ul").each(function () {
		//alert(this.id);
		if (jQuery(this).css("display")!="none") {
			menusOberts.push(this.id);
		} 
	});
	//alert(menusOberts.toString());
	jQuery.cookie("menusOberts",menusOberts.toString(),{ expires: 0.014, path: '/', secure: false });
}	

function obreMenu(codi) {
	/*jQuery("ul.menuRoot ul").hide();
	jQuery("#"+codi).each(function () {
		if (jQuery(this).css("display")=="none") {
			jQuery(this).show();
		} else {
			jQuery(this).hide();
		}
	});*/

	jQuery("ul.menuRoot ul").each(function () {
		if (codi == this.id) {
			if (jQuery(this).css("display")=="none") {
				jQuery(this).show();
			} else {
				jQuery(this).hide();
			}
		} else {
			if (jQuery(this).css("display")!="none") {
				jQuery(this).hide();
			} 
		}
	});

	actualitzaMenusOberts(); 
}


function jsload(sec) {
	for (i=0;i<FnLoad.length;i++) {
		if (FnLoad[i].sec==sec) {
			FnLoad[i].fn();
			break;
		}	
	}
}

function obreSeccio(sec) {
	showCarregant();
	//alert(wwwroot+'/ajax');
	jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', dataType:'html', data: 'canal='+canal+'&sec='+sec, success: redirect });
}

	
function getAjax(obj) {
	sec=obj.getAttribute("rel");
	showCarregant();
	//alert(wwwroot+'/ajax');
	jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', dataType:'html', data: 'canal='+canal+'&sec='+sec, success: redirect });
	return false;
}

function ajaxiza() {
	jQuery("a[@rel]").each(function () {
		this.href='#';
		if(this.rel!="#") { 
			this.onclick=function () { getAjax(this); return false; } 
		}
	});
}

function obretancaDocs(codi) {
	/*jQuery("ul ul.llistaDocs").hide('fast');
	jQuery(".flechaDoc").attr('src','/img/flechadoc.jpg');

	jQuery("#"+codi).each(function () {
		if (jQuery(this).css("display")=="none") {
			jQuery(this).show("fast");
			jQuery("."+codi).attr('src','/img/flechadocon.jpg');
		} else {
			jQuery(this).hide("fast");
		}
	});*/

	jQuery("ul ul.llistaDocs").each(function () {
	
		if (this.id!=codi) {
			if (jQuery("#"+this.id).css("display")=="block") {
				jQuery("#"+this.id).hide("fast");
				jQuery("."+this.id+".flechaDoc").attr('src','/img/flechadoc.jpg');
			} 		
		} else {
			if (jQuery("#"+codi).css("display")=="none") {
				jQuery("#"+codi).show("fast");
				jQuery("."+codi+".flechaDoc").attr('src','/img/flechadocon.jpg');
			} else {
				jQuery("#"+codi).hide("fast");
				jQuery("."+codi+".flechaDoc").attr('src','/img/flechadoc.jpg');
			}		
		}

	});
}

var tempsLoaded=false;
var tempsPagActual=0;
var resXpag=4;
var numPags=0;

/* si els links tenen rel, es converteixen en ajax cridar secció */
function init() {
	jsload(sec);
	ajaxiza();
	if (canal!="admin-ipragma") jQuery(".start-closed").css({ display: 'none'});
	jQuery("ul ul.llistaDocs").css({ display: 'none'});
	include("http://www10.gencat.cat/gencat/js/temperatures.js");
	if (sec=="home")
		 hideLoading();

}


function shuffle(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};

function actualitzaTemps() {
	tempsPagActual++;
	if (tempsPagActual==numPags) tempsPagActual=0;
	for (i=0;i<resXpag;i++) {
		idCiutat=tempsPagActual*resXpag+i;
		if (arrTemp[idCiutat] != undefined)
			jQuery("#eltemps"+i).html(arrTemp[idCiutat][1]+" "+arrTemp[idCiutat][0]+"º");
		else
			jQuery("#eltemps"+i).html('');

	}
}


function elTemps() {
	if (!tempsLoaded) {
		//shuffle(arrTemp);

		numPags=Math.ceil(arrTemp.length/resXpag);

		tempsPagActual=parseInt(Math.random() * numPags);

		for (i=0;i<resXpag;i++) {
			idCiutat=tempsPagActual*resXpag+i;
			if (idCiutat<=arrTemp.length) {
				if (arrTemp[idCiutat] != undefined)
					jQuery("#eltemps"+i).html(arrTemp[idCiutat][1]+" "+arrTemp[idCiutat][0]+"º");
				else 
					jQuery("#eltemps"+i).html('');
			} else {
				jQuery("#eltemps"+i).html('');
			}
		}
		setInterval("actualitzaTemps()",3000);
		tempsLoaded=true;
	}
}


jQuery(document).ready(init);


