$(document).ready(function() {

	// enllaços externs
	$('a[rel=external], a[rel=document]').attr('target', '_blank');
	$("a[href$=.pdf]").attr("target", "_blank");

	$("#content ul.llista_fons.llista_separadors li:last-child").addClass("last");
	$("#content #content_main .premis:first-child").addClass("first");
	
	$("#centres #boxes .caixa:nth-child(2n+1),#centres .options li:nth-child(2n+1)").addClass("first");
	
	
	// camp cerca
	$('#text-cerca').focus(function() {
		$(this)	.val('');
	});

	// antispam
	$("a[href*='(ELIMINAR)']").each(function(i) {
		var t = $(this);
		omg = t.attr('href');
		omg2 = t.text();
		t.attr('href', omg.split('(ELIMINAR)').join(''));
		t.text(omg2.split('(ELIMINAR)').join(''));
	});
	
	$('#houdiniControl').remove();

	// colorbox
	$('a.colorbox').colorbox({
		maxWidth: '95%',
		maxHeight: '95%'
	});
	// colorbox iframe
	$('a.colorboxIframe').colorbox({
		iframe: 'true',
		width: '650px',
		height: '70%',
		maxHeight: '95%'
	})

	// hover menú
	$('#menu li').each(function() {
		var li = $(this);
		if (!(li.hasClass('current'))) {
			li
				.mouseover(function() { li.addClass('current'); })
				.mouseout(function() { li.removeClass('current'); });
		}
	});
	
	//box clicacles subportades centres
	/*$("#centres #boxes .caixa").click(function(){  
      window.location=$(this).find("a").attr("href");  
      return false;  
	}); */ 

	// tractament llistats
	$('ul li:last').addClass('last');

	// selector centres portada
	$('#ourCentres > ul > li, #centresSelector, #ct, li.translate').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	);

	// creació automàtica de contenidors amb pestanyes
	$('div.auto-tabbed').each(function() {

		var t = $(this);

		t.children('div.tabbed-content').not(':eq(0)').hide();
		t.prepend('<ul class="tabs clearfix"></ul>');

		t.children('div.tabbed-content').each(function(i) {

			var u = $(this);

			u.addClass(''+i+''); // afegim classe per no haver de buscar després la seva posició
			txt = u.children('h3').text();
			// si és el primer element, li posem la classe 'current'
			if (i == 0) { u.parent().children('ul.tabs').append('<li class="current"><a href="#" class="'+i+'">'+txt+'</a></li>');  }
			else { u.parent().children('ul.tabs').append('<li><a href="#" class="'+i+'">'+txt+'</a></li>'); }
			u.children('h3').hide();

		});

		t.children('ul.tabs').children('li').children('a').click(function() {

			var v = $(this);
			var p = v.parent();
			var ppp = p.parent().parent();
			srch = 'div.'+v.attr('class');

			if (ppp.children(srch).is(':hidden')) {
				p.parent('ul').children('li').removeClass('current');
				p.addClass('current');
				ppp.children('div').hide();
				ppp.children(srch).show();
			}

			return false;

		});

	});

});
