	$.extend({	  getUrlVars: function(){ var vars = [], hash;	    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');	    for(var i = 0; i < hashes.length; i++)	    {	      hash = hashes[i].split('=');	      vars.push(hash[0]);	      vars[hash[0]] = hash[1];	    }	    return vars;	  },	  getUrlVar: function(name){	    return $.getUrlVars()[name];	  }	});

	var urlBase="/";
	var GranActual=0;
	
	var msAmagant=300;
	var msMostrant=400;
	var efecteBlocsIni=0;
	var efecteBlocsFi="1";
	var NoMostrisPrimeraGaleria=false;
	
	$(document).ready(function () {
		$("body").removeClass("SenseJavascript");
		preparaMenuButlleti();
		preparaSubscriute();
		preparaClicksIndex();
//		preparaMiniMiniatures();
		preparaTecles();
		preparaClicksGaleries();
		executaHash();
		preparaIdiomes();
		afegeixMagradaAFacebook();
		
		if ($("body#Comercial").length!=0) preparaPaginaNoIndex("BlocComercial", $("#ComercialIndex a"));
		if ($("body#Artistic").length!=0) preparaPaginaNoIndex("BlocArtistic", $("#ArtisticIndex a"));
		if ($("body#Butlleti").length!=0) preparaPaginaNoIndex("BlocButlleti", $("#ButlletiIndex a"));
		
	});

	function preparaPaginaNoIndex(IdBloc, jqEnllacIndex)
	{
		var jqGaleriaSeleccionada=$("#" + IdBloc + " a.Seleccionat");
		if (jqGaleriaSeleccionada.length==0)
			jqEnllacIndex.click();
		else
			jqGaleriaSeleccionada.click();
		
	}

	function afegeixMagradaAFacebook()
	{
/*		var html= '<div><div id="fb-root"></div>'
		+ '<script type="text/javascript" src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>'
		+ '<fb:like href="www.joanpujolcreus.com" send="false" layout="standard" width="300" show_faces="false" font=""></fb:like></div>';			
*/		

		var html= '<div class="fb-like" data-href="http://www.facebook.com/pages/Joan-Pujol-Creus/136797943091943" data-send="false" data-width="500" data-show-faces="false"></div>';
//		var html= '<div class="fb-like" data-href="www.joanpujolcreus.com" data-send="false" data-width="500" data-show-faces="false"></div>';

		var jqHtml=$(html);

		$(".Magrada").append(jqHtml);
	}

	function preparaIdiomes()
	{
		$(".IdiomesIndex a").click(function (event) {
			event.preventDefault();
			//alert($(this).attr("href") + location.hash);
			//location.replace($(this).attr("href") + location.hash);
			location.href=$(this).attr("href") + location.hash;
		});
	}

	function executaHash()
	{
		//alert("location.hash:" +location.hash);
		if (location.hash!="")
		{
			var indexOfIddd=location.hash.indexOf("Iddd");
			//alert("indexOfIddd:" + indexOfIddd);
			if (indexOfIddd!=-1)
			{
				// com que ho havia fet amb Iddd només pel butlletí mentenc això... però
				// ara tots els enllaços van amb Id únic
				var IdEnllac="#TitolGal" + location.hash.substr(1).replace("Iddd","");
//				alert(IdEnllac);
//				MostraGaleria($(IdEnllac),"butlleti","grans",true);


				NoMostrisPrimeraGaleria=true;
				$("a.ButlletiIndex").click();
				//SeleccionaEnllacDelHash("#MenuButlleti");
				MostraGaleria($(IdEnllac), "butlleti", "grans", false);

				return;	
			}
			
			var parts=location.hash.split("/");
			var idGal=trobaIdGalAUrl(location.hash);

			for(var i=0;i<parts.length;i++)
			{
				switch (parts[i])
				{
				case "#":	break;
				case "Nou2011":	break;
				case "cat":	break;
				case "esp":	break;
				case "eng":	break;
				
				case "com":
					if(i!=(parts.length-1))
					{ 		
						NoMostrisPrimeraGaleria=true;
						$("#ComercialIndex a").click();
						MostraGaleria($("#TitolGal" + idGal), "com", "grans", false);
					}
					else
						$("#ComercialIndex a").click();
					break;
				case "art":			
					if(i!=(parts.length-1))
					{ 		
						NoMostrisPrimeraGaleria=true;
						$("#ArtisticIndex a").click();
						MostraGaleria($("#TitolGal" + idGal), "art", "minis", false);
					}
					else
						$("#ArtisticIndex a").click();
					break;
				case "butlleti":	
					if(i!=(parts.length-1))
					{ 	
						NoMostrisPrimeraGaleria=true;
						$("a.ButlletiIndex").click();
						if (parts[parts.length-1]=="subscriute")
						{
							$("a.Subscriute").click();
						}
						else
						{
							MostraGaleria($("#TitolGal" + idGal), "butlleti", "grans", false);
						}
					}
					else
						$("a.ButlletiIndex").click();
					break;
				case "contacte":	
					$("a.ContacteIndex").click();
					break;
				case "bio":	
					$("a.BioIndex").click();
					break;
				
				default:
				}
			}
		}
	}

	function preparaMenuButlleti()
	{
		$("#MenuButlleti .SubGaleria").each(function (event){
			$(this).hide();
		});
		
		$("#MenuButlleti .Marc > ul > li > a").click(function (event){
			event.preventDefault();

			$("#MenuButlleti .Marc > ul > li > a").not($(this)).removeClass("TitolSubMenuSeleccionat");

			var actual=$(".SubGaleria", $(this).parent());
			$("#MenuButlleti .SubGaleria").each(function (event){
				if(actual.get(0)!=this)
					$(this).hide();
			});
			
			$(this).addClass("TitolSubMenuSeleccionat");
			actual.fadeToggle();

		});
	}
	
	function preparaSubscriute()
	{
		$("a.Subscriute").click(function (event) {
			event.preventDefault();
			
			AmagaBlocs(false, false, true, function (){
				location.hash="/butlleti/subscriute";
				if (!$("body").hasClass("AmagaBlocSubscriute"))
				{
					$("body .Bloc.BlocSubscriute").animate({opacity:efecteBlocsIni}, msAmagant, function (){
						$("body").addClass("AmagaBlocSubscriute");
						$("body a.Subscriute").addClass("Seleccionat");
						$("body").removeClass("AmagaBlocSubscriute");
						$("#BlocSubscriute").removeClass("Amagat");
						$("body .Bloc.BlocSubscriute").animate({opacity:1}, msAmagant);
						$(".correu").focus();
					});
				}
				else
				{
					$("body .Bloc.BlocSubscriute").css("opacity", "0");
					$("body").addClass("AmagaBlocSubscriute");
					$("body a.Subscriute").addClass("Seleccionat");
					$("body").removeClass("AmagaBlocSubscriute");
					$("#BlocSubscriute").removeClass("Amagat");
					$("body .Bloc.BlocSubscriute").animate({opacity:1}, msAmagant);				
					$(".correu").focus();
				}
				
			});
			
			
			// var BlocsAmagats=0;
			// var BlocsAAmagar=$("body .Bloc:not(:eq(0)):not(.BlocButlleti):not(.BlocSubscriute)");
			// $("#ComentariASota").animate({opacity:0}, msAmagant,function(){$(this).addClass("Amagat");});
			// $("#SotaContingut").animate({opacity:0}, msAmagant,function(){$(this).addClass("Amagat");});
			// $(".MagradaAFacebook").animate({opacity:0}, msAmagant,function(){$(this).addClass("Amagat");});
// 			
			// BlocsAAmagar.each(function () {
				// $(this).animate({opacity:0}, msAmagant,function(){
					// $(this).addClass("Amagat");
					// if(++BlocsAmagats==BlocsAAmagar.length)
					// {
						// $(".SubGaleria a.Seleccionat").removeClass("Seleccionat");
// 						
						// if (!$("body").hasClass("AmagaBlocSubscriute"))
						// {
							// $("body .Bloc.BlocSubscriute").animate({opacity:efecteBlocsIni}, msAmagant, function (){
								// $("body").addClass("AmagaBlocSubscriute");
								// $("body a.Subscriute").addClass("Seleccionat");
								// $("body").removeClass("AmagaBlocSubscriute");
								// $("#BlocSubscriute").removeClass("Amagat");
								// $("body .Bloc.BlocSubscriute").animate({opacity:1}, msAmagant);
								// $(".correu").focus();
							// });
						// }
						// else
						// {
							// $("body .Bloc.BlocSubscriute").css("opacity", "0");
							// $("body").addClass("AmagaBlocSubscriute");
							// $("body a.Subscriute").addClass("Seleccionat");
							// $("body").removeClass("AmagaBlocSubscriute");
							// $("#BlocSubscriute").removeClass("Amagat");
							// $("body .Bloc.BlocSubscriute").animate({opacity:1}, msAmagant);				
							// $(".correu").focus();
						// }
					// }
				// });
			// });
		});
	}
	
	function preparaImatgesGrans(bloc)
	{
		if ($(".ImatgeGranSeguent", bloc).length>0)
			$(bloc).mousewheel(function (event, delta){
				if (delta<0) VesAImatgeSeguent(event,bloc);
				else VesAImatgeAnterior(event,bloc);
				event.preventDefault();
			});
		
		$(".ImatgeGranSeguent", bloc).click(function (event) {VesAImatgeSeguent(event,bloc);});

		$(".ImatgeGranAnterior", bloc).click(function (event) {VesAImatgeAnterior(event,bloc);});

		// $(bloc).mousedown(function (event) {
			// $(document).mousemove(function () {
				// bloc.scrollLeft();
			// });
		// });
		
		FletxesEncaraNoFoses=true;
		vesAGranDeLaUrl(bloc);
	}
	
	function vesAGranDeLaUrl(bloc)
	{
		if ($(".ImatgeGran", bloc).length>1)
		{
			url=window.location.href;
			parts=url.split("/");
//			alert(url + "//" + parts[parts.length-1].substr(0,2) + "//" + parts[parts.length-1].substr(2));
			GranActual=0;
			VesAGranActual(true, bloc);// retallem el marc des del principi per no ensenyar més d'una foto
			if (parts.length>0 && parts[parts.length-1].substr(0,2)=="im")
			{
				GranActual=parts[parts.length-1].substr(2);
			}

			VesAGranActual(true, bloc);
		} 
	}
	
	function preparaClicksIndex()
	{
		$("h1 a").click(function (event) {
			event.preventDefault();
			AmagaBlocs(false, true, true, function () {});
				location.hash="";
		});
		
		$("#ComercialIndex a").click(function (event) {
			event.preventDefault();
			AmagaBlocs(false, true, true, function () {
				$("#ComercialIndex a").addClass("Seleccionat");
				$("body").removeClass("AmagaBlocComercial");
				$("#BlocComercial").removeClass("Amagat");
				
				$(".Bloc.BlocComercial").animate({opacity:efecteBlocsFi}, msMostrant);
//				ressaltaMinis(".MinisCom", -1);
				if (!NoMostrisPrimeraGaleria)
					location.hash="/com";
//				MostraGaleria($("#ComercialIndex a"),"com","miniminis",false);
				if (NoMostrisPrimeraGaleria)
					NoMostrisPrimeraGaleria=false;
				else if ($("#BlocComercial a.PrimeraGaleria").length>0)
					MostraGaleria($("#BlocComercial a.PrimeraGaleria"),"com","grans",false);
				else
					MostraGaleria($("#BlocComercial a:first"),"com","grans",false);
			});
		});

		$("#ArtisticIndex a").click(function (event) {
			event.preventDefault();
			AmagaBlocs(false, true, true, function () {
				$("#ArtisticIndex a").addClass("Seleccionat");
				$("body").removeClass("AmagaBlocArtistic");
				$("#BlocArtistic").removeClass("Amagat");
				$(".Bloc.BlocArtistic").animate({opacity:efecteBlocsFi}, msMostrant);
//				ressaltaMinis(".MinisArt", -1);
				if (!NoMostrisPrimeraGaleria)
					location.hash="/art";
//				MostraGaleria($("#ArtisticIndex a"),"art","miniminis",false);
				if (NoMostrisPrimeraGaleria)
					NoMostrisPrimeraGaleria=false;
				else if ($("#BlocArtistic a.PrimeraGaleria").length>0)
					MostraGaleria($("#BlocArtistic a.PrimeraGaleria"),"art","minis",false);
				else
					MostraGaleria($("#BlocArtistic a:first"),"art","minis",false);
			});
		});

		$("a.ButlletiIndex").click(function (event) {
			event.preventDefault();
			AmagaBlocs(false, true, true, function () {
				$("a.ButlletiIndex").addClass("Seleccionat");
				$("body").removeClass("AmagaBlocButlleti");
				$("#BlocButlleti").removeClass("Amagat");
				$(".Bloc.BlocButlleti").animate({opacity:efecteBlocsFi}, msMostrant);
				$(".Bloc.BlocComentari").css("opacity", 0);
				$(".Bloc.BlocImatges").css("opacity", 0);
				$(".Bloc.BlocComentari").animate({opacity:efecteBlocsFi}, msMostrant);
				$(".Bloc.BlocImatges").animate({opacity:efecteBlocsFi}, msMostrant);
				FletxesEncaraNoFoses=true;// per ensenyar les fletxes cada vegada
				VesAGranActual(true);
				if (!NoMostrisPrimeraGaleria)
					location.hash="/butlleti";
//				MostraGaleria($("a.ButlletiIndex"),"butlleti","miniminis",false);
				if (NoMostrisPrimeraGaleria)
					NoMostrisPrimeraGaleria=false;
				else 
					MostraGaleria($("#BlocButlleti a:not(.Subscriute):last"),"butlleti","grans",false);
			});
		});

		$("a.BioIndex").click(function (event) {
			event.preventDefault();
			AmagaBlocs(false, true, true, function () {
				$("a.BioIndex").addClass("Seleccionat");
				$("body").removeClass("AmagaBlocBio");
				$("#BlocBio").removeClass("Amagat");
				$(".Bloc.BlocBio").animate({opacity:efecteBlocsFi}, msMostrant);
				location.hash="/bio";
			});
		});
		
		$("a.ContacteIndex").click(function (event) {
			event.preventDefault();
			AmagaBlocs(false, true, true, function () {
				$("a.ContacteIndex").addClass("Seleccionat");
				$("body").removeClass("AmagaBlocContacte");
				$("#BlocContacte").removeClass("Amagat");
				$(".Bloc.BlocContacte").animate({opacity:efecteBlocsFi}, msMostrant);
				location.hash="/contacte";
			});
		});

	}

	function preparaMiniMiniatures(bloc)
	{
//		ressaltaMinis(bloc, $(".GrupMinis:first", bloc).attr("id").substr(9));
				
		$("#MenuComercial a").mouseenter(function (){ressaltaMinis($("#Miniscom"), this.id.substr(8));});
		$("#MenuArtistic a").mouseenter(function (){ressaltaMinis($("#Minisart"), this.id.substr(8));});
		
		if ($(".GrupMinis", bloc).length>0)
		{
			$(".GrupMinis", bloc).each(function (){
				$(this).mouseenter(function (event) { ressaltaMinis(bloc, this.id.substr(9)); });
			});
			
			$(".GrupMinis .Minis", bloc).each(function (){
				var jqThis=$(this);
				jqThis.addClass("Amagat");
				jqThis.load(function(){
					$(this).addClass("Carregat");
				});	
				if (this.complete)
					jqThis.addClass("Carregat");
			});

			
			redueixFinsQueSacabi(bloc);
		}
	}
	
	function redueixFinsQueSacabi(bloc) 
	{
		
		var jqMinis=$(".GrupMinis .Minis",bloc);
		var jqCarregats=$(".GrupMinis .Minis.Carregat",bloc);
		var jqNoCarregats=$(".GrupMinis .Minis:not(.Carregat)",bloc);
		
		if ((jqCarregats.length+jqNoCarregats.length)!=jqMinis.length)
		{
			alert("???");
		}
		else
		{
			var resultat=redueixMiniatures(bloc);
			if ((jqNoCarregats.length!=0) || (resultat==-1))
				setTimeout( function () {redueixFinsQueSacabi(bloc);},50);
		}
	}
	
	var redueixMiniaturesExecutant=false;
	function redueixMiniatures(bloc)
	{
		if (redueixMiniaturesExecutant) return -1;
		redueixMiniaturesExecutant=true;
		
		var minisCarregats=$(".Minis.Carregat", bloc);
		var jqMinis=$(".Minis",bloc);
		if (minisCarregats.length==0)
		{ 
			redueixMiniaturesExecutant=false;
			return 0;
		}

		var jqUltimaMiniatura=minisCarregats.last();
		minisCarregats.each(function () { $(this).removeClass("Amagat");});

		try{
			minisCarregats.css("padding-bottom","4px");		
			var midaABaix=(500+bloc.offset().top);
			
			while (((jqUltimaMiniatura.offset().top+jqUltimaMiniatura.height())<midaABaix)
					&& (jqUltimaMiniatura.height()<50))
				minisCarregats.css("height",(jqUltimaMiniatura.height()-(-1)) + "px");
			while ((jqUltimaMiniatura.offset().top+jqUltimaMiniatura.height())>midaABaix)
				minisCarregats.css("height",(jqUltimaMiniatura.height()-(1)) + "px");
			i=4;
			while (((jqUltimaMiniatura.offset().top+jqUltimaMiniatura.height())<midaABaix)
					&& (jqUltimaMiniatura.height()<50))
				minisCarregats.css("padding-bottom",(++i) + "px");
			while ((jqUltimaMiniatura.offset().top+jqUltimaMiniatura.height())>midaABaix)
				minisCarregats.css("padding-bottom",(--i) + "px");
			
			// i al final els deixo tots a la mateixa mida
			jqMinis.css("height",jqUltimaMiniatura.height() + "px");
			jqMinis.css("padding-bottom",i);
			
		}catch (err) {}
		redueixMiniaturesExecutant=false;
		return 0;
	}
	function preparaTecles()
	{
		// $("#Contingut").mousewheel(function (event, delta){
			// var jqBloc=-1;
			// $(".Bloc:not(.Amagat)").each(function () {
				// if ((".ImatgeGranSeguent",$(this)).length>0)
					// jqBloc=$(this);
			// });
// 			
			// if (jqBloc!=-1)
			// {
				// if (delta<0) VesAImatgeSeguent(event,jqBloc);
				// else VesAImatgeAnterior(event,jqBloc);
				// event.preventDefault();
			// }
			// event.returnValue = true;
		// });
		
		
		$("body").keydown(function (event){
			var jqBloc=-1;
			$(".Bloc:not(.Amagat)").each(function () {
				if ((".ImatgeGran",$(this)).length>0)
					jqBloc=$(this);
			});
			switch (event.keyCode)
			{
				case 37: 
					if (jqBloc!=-1)
						VesAImatgeAnterior(event,jqBloc);
				break;
				case 39: 
					if (jqBloc!=-1)
					VesAImatgeSeguent(event,jqBloc);
				break;
				case 13: 
				break;
				//			default:alert(event.keyCode);
				//		if(Event.getKeyCode(event)==KEYS.BACKSPACE)
			}
		});
	}

	function VesAImatgeAnterior(event, bloc) 
	{
		if (event) event.preventDefault();
		if(GranActual>0) GranActual--;
		VesAGranActual(false, bloc);
	}
	
	function VesAImatgeSeguent(event, bloc) 
	{
		if (event) event.preventDefault();
		if(GranActual<($(".ImatgeGran", bloc).length-1)) GranActual++;
		VesAGranActual(false, bloc);
	}	
	
	function ressaltaMinis(bloc, idUnicGal)
	{
		
		$(".GrupMinis", bloc).removeClass("Amagat");
		$(".GrupMinis", bloc).addClass("Ofuscat");
		$("#GrupMinis" + idUnicGal, bloc).removeClass("Ofuscat");
//		redueixMiniatures(bloc);
		$("#MenuComercial a").removeClass("Seleccionat");
		$("#MenuArtistic a").removeClass("Seleccionat");
		$("#TitolGal" + idUnicGal).addClass("Seleccionat");
	}

		
	var FletxesEncaraNoFoses=true;// només les vull fondre una vegada... 
	function FonFletxes(bloc)
	{
		if (!FletxesEncaraNoFoses) return;
		FletxesEncaraNoFoses=false;
		
		var jqAnterior=$(".ImatgeGranAnterior", bloc);
		var jqSeguent=$(".ImatgeGranSeguent", bloc);
		
		jqSeguent.css("opacity", 1);
		jqAnterior.css("opacity", 1);
		jqSeguent.animate({opacity:0, right:-20},2000, function (){
			jqSeguent.css("right","0");
		});
		jqAnterior.animate({opacity:0, left:-20},2000, function (){
			jqAnterior.css("left","0");
		});
	}

	var ImatgeGranLoadsAfegits=false;
	function VesAGranActual(EsInici, bloc)
	{
		GranActualApunt(EsInici, bloc);
		var jqImage=$(".ImatgeGran:eq(" + GranActual + ")", bloc);
		if (jqImage.length==0) return;
	
		
		// in case it is already loaded...
		jqImage.load(function () {FonFletxes(bloc);});// només per la que toca
		if (jqImage.get(0).complete)
		{
			FonFletxes(bloc);
		}
//		if (!ImatgeGranLoadsAfegits)
//		{
//			$(".ImatgeGran").load(function (){GranActualApunt(EsInici);});
//			jqImage.load(function (){GranActualApunt(EsInici);});
//		}
//		ImatgeGranLoadsAfegits=true;
	} 

	function GranActualApunt(EsInici, bloc) 
	{
		var jqImage=$(".ImatgeGran:eq(" + GranActual + ")", bloc);
		if (jqImage.length==0) return;
		
		if ((GranActual==0) && !EsInici)
		{
			$("#ImatgeGran", bloc).animate({scrollLeft: jqImage.position().left+50}, 50, 'linear', function () {
				$("#ImatgeGran", bloc).animate({scrollLeft: jqImage.position().left}, 50, 'linear');
			});	
		}
		else if ((GranActual==($(".ImatgeGran", bloc).length-1)) && !EsInici)
		{
//			$("body").append("<div  class=Xibato>ultim//</div>");
			$("#ImatgeGran", bloc).animate({scrollLeft: jqImage.position().left-50}, 50, 'linear', function () {
				$("#ImatgeGran", bloc).animate({scrollLeft: jqImage.position().left}, 50, 'linear');
			});	
		}
		else
		{
			$("#ImatgeGran", bloc).animate({scrollLeft: jqImage.position().left}, 100, 'linear');
		}
		$("#ImatgeGran", bloc).css("width",jqImage.width() + "px");
		$("#ComentariASota", bloc).css("width",(jqImage.width()-20) + "px");
	}

//	var AmagaBlocsCallback;
	function AmagaBlocs(AmagaIndex, AmagaSegonBloc, DeseleccionaSegonBloc, callback)
	{
		var AmagaBlocsCallback=callback;

		if (AmagaSegonBloc)
		{
			$("a.BioIndex").removeClass("Seleccionat");
			$("a.ContacteIndex").removeClass("Seleccionat");
			$("a.FotoDelMesIndex").removeClass("Seleccionat");
			$("a.ButlletiIndex").removeClass("Seleccionat");
			$("#ComercialIndex a").removeClass("Seleccionat");
			$("#ArtisticIndex a").removeClass("Seleccionat");
		}
		
		if (DeseleccionaSegonBloc)
		{
			$("#BlocArtistic a").removeClass("Seleccionat");
			$("#BlocComercial a").removeClass("Seleccionat");
			$("#BlocButlleti a").removeClass("Seleccionat");
		}
		
		$("a.Subscriute").removeClass("Seleccionat");
//		$("body").addClass("AmagaBlocMiniatures");
		
		var animacions=0;
		var animacionsAcabades=0;
		$(".Bloc").each(function () {
			var jqBloc=$(this);
			var idBloc=jqBloc.attr("id");
			
			//if (idBloc=="BlocCarregantMiniMinis") return;
			
			if (!AmagaIndex &&(idBloc=="BlocIndex")) return;
			if (!AmagaSegonBloc && 
				(
				(idBloc=="BlocArtistic")
				|| (idBloc=="BlocComercial")
				|| (idBloc=="BlocBio")
				|| (idBloc=="BlocButlleti")
				|| (idBloc=="BlocContacte")
				)) return;
			
			if (!jqBloc.hasClass("Amagat") || !$("body").hasClass("Amaga" + idBloc))
			{
				animacions++;
				jqBloc.animate({opacity:efecteBlocsIni}, msAmagant, function (){
					jqBloc.addClass("Amagat");
					$("#Index").addClass("Amaga" + idBloc);
					
					animacionsAcabades++;
					if (AmagaBlocsCallback && (animacions==animacionsAcabades))
					{ 
						AmagaBlocsCallback();
						$(".Carregant:not(#BlocCarregantMiniMinis)").each(function () {$(this).removeClass("Carregant");});
					}
				});
			}
		});


		if (AmagaBlocsCallback && (animacions==0))
		{
			AmagaBlocsCallback();
			$(".Carregant:not(#BlocCarregantMiniMinis)").each(function () {$(this).removeClass("Carregant");});
		}

		
		return;
	}
	
	function preparaClicksGaleries()
	{
		$("#MenuComercial a").click(function (event) {
			event.preventDefault();
			MostraGaleria($(this), "com", "grans", true);
		});
		
		$("#MenuArtistic a").click(function (event) {
			event.preventDefault();
			MostraGaleria($(this), "art", "minis", true);
		});

		$("#MenuButlleti .SubGaleria a").click(function (event) {
			event.preventDefault();
			MostraGaleria($(this), "butlleti", "grans", true);
		});
	}
	
	var MostraGaleriaEnProcess=false;
	function MostraGaleria(jqEnllac, seccio, tipus, deseleccionaSegonNivell)
	{
		if (MostraGaleriaEnProcess) return;
		MostraGaleriaEnProcess=true;
		
		if(tipus=="miniminis")
		{
			var url=jqEnllac.attr("href");
			var jqBloc=$(".Bloc#Minis" + seccio);
			var idGal=0;
		}
		else
		{
			var url=jqEnllac.attr("href");
			location.hash=url;

			var idGal=trobaIdGalAUrl(url);

			var jqBloc=$(".Bloc#" + seccio + idGal + tipus);
			var jqBlocComentari=$(".Bloc#" + seccio + idGal + "comentari");
		}
		if (jqBloc.length==0)
		{
			var jqGaleriaOberta=$(".Bloc:not(.Amagat):not(#BlocIndex):not(#BlocArtistic):not(#BlocComercial):not(#BlocButlleti)");
			if (jqGaleriaOberta.length==0)
			{
				var jqCarregantMiniMinis=$("#BlocCarregantMiniMinis");
				$("body").removeClass("AmagaBlocCarregantMiniMinis");
				if (jqCarregantMiniMinis.length==0)
				{
					$("#Contingut").append('<div class="Bloc Carregant" id="BlocCarregantMiniMinis"><div class=CarregantMiniMinis><div class=Carregador></div></div></div>');
				}
				else
					jqCarregantMiniMinis.removeClass("Amagat");
			}
			else
				jqGaleriaOberta.addClass("Carregant");
			
			var IdiomaActual;
			switch($(".IdiomesIndex a.Seleccionat").attr("href").substr(0,4)) 
			{
				case "/cat": IdiomaActual=0;break;
				case "/esp": IdiomaActual=1;break;
				case "/eng": IdiomaActual=2;break;
			}
			
			if (tipus=="miniminis")
				$("#Contingut").append('<div class="Bloc Carregant" id="BlocCarregantMiniMinis"><div class=CarregantMiniMinis><div class=Carregador></div></div></div>');

			if(seccio=="butlleti")
			{
				var jqSubGaleriaActual=jqEnllac.parents(".SubGaleria");
				$("#MenuButlleti .SubGaleria").not(jqSubGaleriaActual).hide();
				$("#MenuButlleti > ul > li > a").not($("> a", jqSubGaleriaActual.parents("li"))).removeClass("TitolSubMenuSeleccionat");
			}
			
        	var jqxhr = $.ajax({ url: urlBase + "tros.php?I=" + IdiomaActual + "&seccio=" + seccio + "&gal=" + idGal + "&tipus=" + tipus})
    			.success(function() {
		    			var retorn=jqxhr.responseText;
		    			jqBloc=$(retorn);
						AmagaBlocs(false, false, deseleccionaSegonNivell, function () {
							jqEnllac.addClass("Seleccionat");
							if(seccio=="butlleti")
							{
								var jqSubGaleriaActual=jqEnllac.parents(".SubGaleria");
								jqSubGaleriaActual.fadeIn();
								$("> a", jqSubGaleriaActual.parents("li")).addClass("TitolSubMenuSeleccionat");
							}
							
			    			if (tipus=="grans")
			    			{
								jqBloc.css("opacity", "0");
				    			$("#Contingut").append(jqBloc); 
			    				preparaImatgesGrans(jqBloc);
								jqBloc.removeClass("Amagat");
								try{
									jqBloc.animate({opacity:efecteBlocsFi}, msMostrant);
								} catch(err){
									jqBloc.css("opacity",1);
								}
								
			    			}
			    			else if (tipus=="minis")
			    			{
				    			$("a",jqBloc).click(function (event) {
				    				event.preventDefault();
				    				MostraGaleria($(this),seccio,"grans", false);
				    			});
								jqBloc.css("opacity", "0");
								jqBloc.removeClass("Amagat");
				    			$("#Contingut").append(jqBloc);
				    			try {
									jqBloc.animate({opacity:efecteBlocsFi}, msMostrant);
								} catch(err){
									jqBloc.css("opacity",1);
								}
				    		}
				    		else if (tipus=="miniminis")
				    		{
				    			$("#Contingut").append(jqBloc); 
								$("#BlocCarregantMiniMinis").remove();
				    			preparaMiniMiniatures(jqBloc);
				    		}
				    		try{
				    		jqBloc.append("<div class=Carregador></div>");
				    		}catch(err){
				    		}
		    				MostraGaleriaEnProcess=false;
	
						});
    			})
    			.error(function() { 
					MostraGaleriaEnProcess=false;
    			});			
		}
		else
		{
			AmagaBlocs(false, false, deseleccionaSegonNivell, function () {
				jqEnllac.addClass("Seleccionat");
				jqBloc.css("opacity", "0");
				jqBloc.removeClass("Amagat");
				$("body").removeClass("Amaga" + jqBloc.attr("id"));
			
				try{
					jqBloc.animate({opacity:efecteBlocsFi}, msMostrant);
				} catch(err) {
					jqBloc.css("opacity",1);
				}

				
				try{
				jqBlocComentari.css("opacity", "0");
				jqBlocComentari.removeClass("Amagat");
				jqBlocComentari.animate({opacity:efecteBlocsFi}, msMostrant);
				}catch(err){}
				FletxesEncaraNoFoses=true;
				vesAGranDeLaUrl(jqBloc);
				MostraGaleriaEnProcess=false;
				
			});
		}
	}
	

function trobaIdGalAUrl(url)
{
	try
	{
		var parts=url.split("/gal");
		var parts=parts[1].split("/");
		return parts[0];
	}
	catch(e)
	{
		return 0;
	}
}

