// Ajuntament de Barcelona 
// www.bcn.cat | www.bcn.es


// Definicio de variables globals.
// ---------------------------------------------------------------------------------------------------------------------
var idioma = $("html").attr("lang");
var pathSite = $("meta[name=base]").attr("content");


$(document).ready ( function() {
	// S'inicialitza el reemplaçament de text per imatges o Flash.
	// ---------------------------------------------------------------------------------------------------------------------
	if (jQuery.JIR) jQuery.JIR.Init(pathSite,'img','mov', true);
	
	// Carreguem les cantonades arrodonides dels elements.
	// ---------------------------------------------------------------------------------------------------------------------
	$(".cantonades").prepend('<span class="se"></span><span class="sd"></span><span class="ie"></span><span class="id"></span>');
	$("div.bloc-presentacio, div.bloc-contingut, div.bloc-llistat, div.bloc-home-flash").prepend('<span class="se"></span><span class="sd"></span>');
	
		// Amaguem les cantonades inferiors en IE per problemes de refresc.
		$("form.cantonades").resize( function() { $(".ie, .id").css({ display: "none" }); });
		
	// Marquem els enllaços que s'obren en finestra nova.
	// ---------------------------------------------------------------------------------------------------------------------
	$("a.obrir-finestra").each ( function() {
		if (idioma=='es') 		var txt = 'Se abre en ventana nueva';
		else if (idioma=='en') 	var txt = 'Open new window';
		else 					var txt = 'S\'obre en finestra nova';
		
		if ($(this).children().is("img")) $(this).attr("title", $(this).attr("title") + ' (' + txt + ')');  
		else $(this).append(' <img src="'+pathSite+'/img/icona_finestra_nova.gif" width="8" title="'+txt+'" alt="'+txt+'" />');
		
		$(this).click ( function() {
			window.open(this.href, '', 'menubar, location, toolbar, directories, status, resizable, scrollbars');
			return(false);
		});
	});

	// Tractament de finestres de l'Enviar a un amic.
	// ---------------------------------------------------------------------------------------------------------------------
	$("a#envia-amic").each( function() {
		if (idioma=='es') 		var txt = 'Se abre en ventana nueva';
		else if (idioma=='en') 	var txt = 'Open new window';
		else 					var txt = 'S\'obre en finestra nova';
		
		$(this).append(' <img src="http://w3.bcn.es/V43/imatges/webdings.gif" alt="'+txt+'" />');
		
		$(this).click ( function() {
			window.open(this.href, 'PopupEnvia', 'width=520, height=640,menubar=no, location=no, toolbar=no, directories=no, status, resizable=0, scrollbars=yes');
			return false;
		});
	});

	$("a#tornar").each ( function() {
		if (window.name == 'PopupEnvia') {
			if ($(this).attr('class')=='correct') {
				$(this).html('Tanca');
				$(this).click (function() { 
					window.close();
					return false;
				});
			}
			if ($(this).attr('class')=='error') {
				$(this).html('Torna');
				$(this).click (function() { 
					window.back();
					return false;
				});
			}
		}
	});
	
	// Boto de tornar enrera en una fitxa.
	// ---------------------------------------------------------------------------------------------------------------------
	$("#fitxa").each ( function() {
		if (idioma=='es') 		var txt = 'Volver atrás';
		else if (idioma=='en') 	var txt = 'Back';
		else 					var txt = 'Tornar enrera';
		
		$(this).append('<p><input type="button" id="tornar" value="'+txt+'" /></p>');
		$("input#tornar").click ( function() {
			window.history.back();
		});
	});


	//Redimensionament de la mida del bloc-contingut per igualar-lo al menu en ie
	//------------------------------------------------------------------------------------------------------------------------
	  if(navigator.appName == "Microsoft Internet Explorer"){
		if( $("#columna0").height() > $(".bloc-contingut").height() ){
		 
			$(".bloc-contingut").height( ($("#columna0").height()) - 15 );
			
		}
	  }





});


// Retorna el valor del radio activat
// ---------------------------------------------------------------------------------------------------------------------
function getRadio(radioObj) {
	var valor_radio = '';
	
	if (radioObj) {
		for(var i=0; i<radioObj.length; i++) {
			if (radioObj[i].checked) valor_radio = radioObj[i].value;
		}
	}
	return(valor_radio);
}

// Retorna si una data te un format correcte, o no
// ---------------------------------------------------------------------------------------------------------------------
function verifyFormatData(strData) {	
	var dia = '';
	var mes = '';
	var any = '';
	var state = false;
	var diesDelMes = new Array(31, (isLeapYear(strData) ? 29:28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	if (strData!='' && strData.indexOf('/')!=-1) {
		var dataTemp = strData.split('/');
		dia = dataTemp[0];
		mes = dataTemp[1];
		any = dataTemp[2];
		
		if (dia.substring(0,1)==0) dia = dia.substring(1, 2);
		if (mes.substring(0,1)==0) mes = mes.substring(1, 2);
		
		if ((parseInt(dia)>0 && parseInt(dia)<=diesDelMes[parseInt(mes)-1]) &&
			(parseInt(mes)>0 && parseInt(mes)<13) &&
			(parseInt(any)>1900 && parseInt(any)<3000)) {
			state = true;
		}
	}
	return(state);
}

function isLeapYear(strData) {
	var d = new Date(strData);
	var y = d.getFullYear()-2;
	return (y%4==0 && y%100!=0) || y%400==0;
}
