$(document).ready ( function() {
	
	// Esborrar value camp cercar onclick
	//____________________________________________________________________ *//
	$("form#cerca input#words[value='cercar']").click(function() {
		$("form#cerca input#words[value='cercar']").attr("value","");	
	});
	
	// Imprimir
	//____________________________________________________________________ *//
	$('<li class="print"><a href="javascript:window.print();" title="Print">Print</a></li>').insertBefore("li.share");
	
	// Menu Drop Down -- Script per a IE6 i reemplacament imatges
	//____________________________________________________________________ *//
	$('ul#main-menu li,#tools ul li.share').hover(
		function(){$(this).addClass("over")},
		function(){$(this).removeClass("over")}
	);
	
	$('#tools ul li.print').hover(
		function(){$(this).addClass("nover")},
		function(){$(this).removeClass("nover")}
	);
		
	// share
	//____________________________________________________________________ *//	
	$("#tools ul li.share a.urldesti[href]").remove();
	$("#tools ul li.share").append("<a title='Share'>Share</a>");
	
	$("body #marc-web li.share").click(
		function() { 
			$("body #marc-web #tools ul li.share div").show();
		}
	);
	
	$("#marc-web #header-links-frame,#marc-web #header,#marc-web div#capcalera-n1,#marc-web #content-frame,#marc-web #hightlights-frame,#marc-web #footer,#marc-web #enllacos-gencat,#marc-web h1").click(
		function() {
			$("#tools ul li.share div").hide();
		}
	);
	
	// anchors
	//____________________________________________________________________ *//
	$("div.contingut-ancora").hide();
	
	$("span.ancora").hide();
	
	$("div.contingut-ancora:first").show();
	
	$("ul#links-n4 li a").click(function() {
		var anchor = $(this).attr("name");
		if (anchor != "") {
			$("div.contingut-ancora").hide();
			$("ul#links-n4 li").removeClass();
			$(this).removeAttr("href");
			
			var i=0;
			for (i=1; i<=20;i++) {
				if (anchor==i) {
					$("div.contingut-ancora h3[id]").eq(i-1).parent().show();
					$("ul#links-n4 li a[name]").eq(i-1).parent().addClass("select");
				}
			}
		}
	});
	
	$("ul#links-microsite li a").each(function() {
		var pathUrl = window.location.pathname;
		var pathLi= $(this).attr("href");
				
		if (pathUrl == pathLi) {
			$(this).parent().addClass("select");
		} else {
			var title = $("ul#breadcums li a.ultim").text();
			if (title == $(this).text()) {
				$(this).parent().addClass("select");
			}
		}
	});

	// finestra nova
	//____________________________________________________________________ *//
	$(function(){
		$("a.new-window").click(function(){
			window.open(this.href);
			return false;
		});
	});

	// Protegir dels robots les adreces de correu
	//____________________________________________________________________ *//
	$('.dir_mail').each(function() {
		var txt= $(this).text();
		
		$(this).attr('href','mailto:' + $(this).attr('href').replace('[arrova]','@'));
		$(this).attr('title', $(this).attr('title').replace('[arrova]','@'));
		
		if(txt != ''){
			$(this).text(txt.replace('[arrova]','@'));
		}	
	});

	// Afegir novetat a les publicacions si estan publicades fa menys de 30 dies
	//____________________________________________________________________ *//
	$('.detall-colleccio').each(function() {	
		var txt= $("h4", this).text();
		var mesArray = new Array("gener","febrer","març","abril","maig","juny","juliol","agost","setembre","octubre","novembre","desembre"); 
		var PosMes = 0;
		
		if (txt != "" ){
			if (txt.split(":").length > 1) {
				var Data = txt.split(":")[1];
				var Mes = Data.split(" ")[1];
				var Any = Data.split(" ")[2];
			}
		}		
		
		for(var i=0;i<12;i++){
			PosMes = 0
			if(mesArray[i] == Mes){
				PosMes = i;
			}
		}
		
		var today = new Date();
		today.setDate(1);
		var diaCreacio = new Date(Any,PosMes,1);
		
		var diff = today-diaCreacio;
		diff = Math.round(diff/1000/60/60/24); 
		if(diff <= 62){
			var diffMes = parseInt(today.getMonth()) - parseInt(diaCreacio.getMonth());
			if(diffMes <= 1){
				$("h3",this).append('<span class="publicacio-novetat">Novetat</span>');
			}
		}
	});
	
	// Capa serveis aleatoria
	//____________________________________________________________________ *//
	$('#contenidor-serveis').each(function(){
		var IntDivs = $('div#contenidor-serveis div').length;
		var randomNum = Math.floor(Math.random() * IntDivs);
		
		$('div#contenidor-serveis div:eq(' + randomNum + ')').css("display", "block");
	});

	// Capa Casos d'èxit
	//____________________________________________________________________ *//
	$('div#cas-exit').each(function(){
		var IntDivs = $('div#cas-exit div').length;
		var randomNum = Math.floor(Math.random() * IntDivs);
		
		$('div#cas-exit div:eq(' + randomNum + ')').css("display", "block");
	});

	// Any peu
	//____________________________________________________________________ *//
	$('div#footer div#links-frame address span:first-child').each(function() {
		var data = new Date();
		var dataAny = data.getUTCFullYear();
		var txtAddress = $(this).text();
						
		$(this).text(txtAddress.replace(/[0-9]{4}/g, dataAny));
	});
});

