document.observe('dom:loaded', function() {
	observeThematicTabs();
});

function observeThematicTabs() {
	$$('div.rental_teaser_conteneur_tabs ul li a').invoke('observe', 'click', onclickThematicTab);
}

function onclickThematicTab(event) {
	event.stop();
	
	link = event.element();
	if ('a' != link.nodeName.toLowerCase()) {
		link = link.up();
	}
	
	thematicId	= link.readAttribute('rel');
	linkId 		= link.identify();
	divId		= 'thematic' + thematicId + '_products';
	div			= $('thematic' + thematicId + '_products');

	// on ne fait l'appel Ajax que si le div a jamais été affiché (= si y a que l'image de loading dedans)
	if (
		(1 == div.immediateDescendants().length)
		&& ('img' == div.firstDescendant().nodeName.toLowerCase())
	) {
		new Ajax.Updater(
			divId,
			_fxcv4_url_root + '/direct/b2f/thematic-tabs/format/html/site_id/' + _fxcv4_site_id + '/thematic_id/' + thematicId,
			{
				method: 'get',
				onFailure: function() {
					alert('An error has occured, the request failed.');
				},
				onSuccess: function() {
					$('contenu_' + linkId).down('a.rental_teaser_bt_afficher_tout').show();
				}
			}
		);
	}
	
	showContent(link);
}
