$(document).ready(function(){
		$('#calendari a').Tooltip({showURL: false, delay: 0, track: true, extraClass: 'calendari', showBody: '/**/'});
});
$(document).ready(function(){
	$("a[rel^='prettyPhoto']").prettyPhoto();
});
$(document).ready(function()
{

	$('<div id="visor-imatges"></div>').appendTo('body').hide();
	$('<div id="visor-imatges-carregant">Carregant...</div>').appendTo('body').hide();
	$('<div id="visor-imatges-fons"></div>').appendTo('body')
	.css({opacity: '0.8', background: '#000', position: 'absolute', left: '0', top: '0'})
	.hide();

});

function mides_imatge(imatge) {
	if ( ( $(window).height() / ( imatge.height + 200 ) ) > ( $(window).width() / ( imatge.width + 100 ) ) ) {
		if(imatge.width > $(window).width()) {
			var amplada = $(window).width() - 100;
			$('#visor-imatges p img').width(amplada);
		}
	} else {
		if(imatge.height > $(window).height()) {
			var amplada = $(window).height() - 200;
			$('#visor-imatges p img').height(amplada);
		}
	}
	$('#visor-imatges').width(imatge.width + 10);
}

function VisorImatges()
{
	$('a.galeria-imatges').click(function()
	{
		$('#visor-imatges-fons').css({width: '' +$(document).width() +'px', height: '' +($(window).height()<$(document).height()?$(document).height():$(window).height()) +'px'})
		$('#visor-imatges-fons').fadeIn(300);

		carrega_imatge($(this));
		return false;
	});
}

function carrega_imatge(objecte)
{
	var btnTancar = '<div id="visor-imatges-tancar">Tancar</div>';
	var btnAnterior = '<div><div id="visor-imatges-anterior">&lt;&lt; Anterior</div>';
	var imgTitle = (objecte.attr('title') == undefined) ? '' : '<div id="visor-imatges-titol"><h2>' +objecte.attr('title') +'</h2></div>';
	var btnSeguent = '<div id="visor-imatges-seguent">Següent &gt;&gt;</div><hr class="clear" /></div>';
	if(objecte.attr('href').substr(0,7) == 'http://') {
		var imgSrc = (objecte.attr('href') == undefined) ? '' : '<p><img src="' +objecte.attr('href') +'" style="max-width:'+$(window).width() - 100+'; max-height:'+$(window).height() - 200+';" alt="Feu clic per a tancar." /></p>';
	} else {
		var imgSrc = (objecte.attr('href') == undefined) ? '' : '<p><img src="' +objecte.attr('href') +'/mw='+ ( $(window).width() - 100 ) +'/mh='+ ( $(window).height() - 200 ) +'" alt="Feu clic per a tancar." /></p>';
	}
	var imgDesc = (objecte.find('img').attr('alt') == undefined) ? '' : '<p>' +objecte.find('img').attr('alt').replace(/\n/g,"<br />") +'</p>';

	// Preload image
	var preload = new Image();
	if(objecte.attr('href').substr(0,7) == 'http://') {
		preload.src = objecte.attr('href');
	} else {
		preload.src = objecte.attr('href')+'/mw='+ ( $(window).width() - 100 ) +'/mh='+ ( $(window).height() - 200 );
	}
	$('#visor-imatges').html(btnTancar +btnAnterior +btnSeguent +imgSrc +imgTitle +imgDesc)
	if(preload.complete)
	{
		mides_imatge(preload);
		$('#visor-imatges').fadeIn(300).center();
	} else {
		$('#visor-imatges-carregant').fadeIn(300).center();
		preload.onload = function()
		{
			mides_imatge(preload);
			$('#visor-imatges-carregant').fadeOut(300);
			$('#visor-imatges').fadeIn(300).center();
		}
	}
	$('#visor-imatges-tancar').click(function()
	{
		$('#visor-imatges-carregant').hide();
		$('#visor-imatges-fons').fadeOut(300);
		$('#visor-imatges').fadeOut(300);
	//	$(document).keydown(function(e) {});
	});
	$('#visor-imatges img').click(function()
	{
		$('#visor-imatges-carregant').hide();
		$('#visor-imatges-fons').fadeOut(300);
		$('#visor-imatges').fadeOut(300);
	//	$(window).keydown(function(e) {});
	});
	$('div#visor-imatges-seguent').click(function() { $('#visor-imatges-carregant').hide(); $('#visor-imatges').hide(); carrega_imatge($('#'+(parseInt(objecte.attr('id'))+1)%num_fotos)); });
	$('div#visor-imatges-anterior').click(function() { $('#visor-imatges-carregant').hide(); $('#visor-imatges').hide(); carrega_imatge($('#'+(parseInt(objecte.attr('id'))-1+num_fotos)%num_fotos)); });
}
$(document).ready(function()
{
 VisorImatges();
 });
		jQuery(function($) {
  $('#nav ul').hide();
  var path = '/media/imatges/actuacions';
  $('#nav a[@href="' + path + '"]').parents("ul").show();
  $('#nav a[@href="' + path + '"]').addClass('active').siblings("ul").show();
  $('#nav #menu').show();
});
jQuery.fn.equalizeCols = function(){
  var height = 0;
  return this.css("height","auto").each(function(){
    height = Math.max( height, jQuery(this).outerHeight() );
  }).css("height", height);

}; 
$(document).ready( function() {
	$('principal-actuacions > div').equalizeCols();
});
