; 

	var carouselFadeSpeed = 0;
	var carousel_current = null;

	function carousel_initCallback(carousel) {
	};

	function carousel_switchText() {
		if(carousel_current.first != null) {
			carousel_setText(carousel_current.get(carousel_current.first + 1));
		} else {
			carousel_unblockall(carousel_current, carousel_current.get(1),1);
			carousel_current.first = 1;			
			carousel_setText(carousel_current.get(2));
		}
	}

	function carousel_stopAll() {
		$('#carousel1').data('jcarousel').list.stop(false,true);
		$('#carousel1').data('jcarousel').stopAuto();

		$('#carousel2').data('jcarousel').list.stop(false,true);
		$('#carousel2').data('jcarousel').stopAuto();

		$('#carousel3').data('jcarousel').list.stop(false,true);
		$('#carousel3').data('jcarousel').stopAuto();
	}

	function onglet_hideAll() {
		$('#onglet-expertise').hide();
		$('#onglet-publications').hide();
		$('#onglet-livres').hide();
		$('.carousel-onglet').hide();
	}

	function carousel_setText(item) {
		data = item.contents('.carousel-data');
		$('#titre-livre').html(data.contents('.titre-livre').html());
		$('#auteurs-livre').html(data.contents('.auteurs-livre').html());
		$('#description-livre').html(data.contents('.description-livre').html());
		$('#nbrpage-livre').html(data.contents('.nbrpage-livre').html());
		$('#tarif-livre').html(data.contents('.tarif-livre').html());
		$('#edition-livre').html(data.contents('.edition-livre').html());
		$('#edition2-livre').html(data.contents('.edition2-livre').html());
		$('#image-livre').attr('src',data.contents('.image-livre').attr('src'));
		$('#maximage-livre').attr('src',data.contents('.maximage-livre').attr('src'));
		$('#url-livre').attr('href',data.contents('.url-livre').attr('href'));
		if(data.contents('.exemple-livre').html() != '') {
		/*	if(data.contents('.exemple-livre').html() == '/catalogue/expertise_locale.html') {
				$('#maxi-link').attr('href',data.contents('.exemple-livre').html());
				$('#maxi-link2').attr('href',data.contents('.exemple-livre').html());
				$('#maxi-link').unbind('click');	
				$('#maxi-link2').unbind('click');	
			} else { */
				$('#maxi-link').attr('href',data.contents('.exemple-livre').html());
				$('#maxi-link2').attr('href',data.contents('.exemple-livre').html());
				$('#maxi-link').unbind('click').click(function() { 
					sorman_exemple_swf_show($(this));
					return false;
				}); 
				$('#maxi-link2').unbind('click').click(function() { 
					sorman_exemple_swf_show($(this));
					return false;
				});
			/* } */
		} else {
			$('#maxi-link').attr('href','#').unbind('click')
			$('#maxi-link2').attr('href','#').unbind('click');
		}
	};

	function lbox() {
			$('#biblio-article').unbind('hover').hover(function() {},function() {});
			carousel_current.container.unbind('hover').hover(function() {},function() {});
			carousel_current.stopAuto();
    			$.blockUI({ 
				message: $('img#maximage-livre'), 
				fadeIn: 500,
			        css: { 
               		 		top:  ($(window).height() - 618) /2 + 'px', 
               	 			left: ($(window).width() - 400) /2 + 'px', 
					width: '400px'
				}
			});
			$('.blockOverlay').attr('title','Cliquez pour sortir').click(function() {
				$.unblockUI({
					fadeOut: 500
				});
				carousel_current.startAuto();
			}); 
	}

	function carousel_unblockall (carousel, item, idx, state) {
		items_all = $(item).siblings().andSelf();
		items = $(item).siblings();
		items_all.unblock();
		$(item).prev().block();
		$(item).next().block();
		$(item).next().next().block();
		$(item).next().next().next().block();
		$(item).block({fadeIn: 300 });
		$(item).next().unblock({fadeOut: 300 });
	};

	function carousel_changed(carousel, item, idx, state) {
		// Prevent overload
		if(!(carousel.ItemCountP)) {
			carousel.ItemCountP = $(item).siblings().andSelf().length; 
		}
		if(idx > carousel.ItemCountP) {
			for(i=-100;i<0;i++) {
				carousel.remove(i);
			}
			for(i=carousel.ItemCountP+2;i<idx;i++) {
				carousel.remove(i);
			}
		}
		carousel_setText($(item).next());
	};

	function carousel_startup() {
		$.blockUI.defaults.fadeOut = carouselFadeSpeed;
		$.blockUI.defaults.fadeIn = carouselFadeSpeed;
		$.blockUI.defaults.message = null;
		$.blockUI.defaults.overlayCSS.cursor = 'default';
		$.blockUI.defaults.overlayCSS.backgroundColor = '#fff';

		$('#carousel1').jcarousel({
			scroll: 1,
			visible: 4,
			wrap: 'circular',
			auto: 5,
			initCallback: carousel_initCallback,
			itemFirstInCallback:{ 
				onBeforeAnimation: carousel_unblockall,
				onAfterAnimation: carousel_changed
			}
		});

		$('#carousel2').jcarousel({
			scroll: 1,
			visible: 4,
			wrap: 'circular',
			auto: 5,
			initCallback: carousel_initCallback,
			itemFirstInCallback:{ 
				onBeforeAnimation: carousel_unblockall,
				onAfterAnimation: carousel_changed
			}
		});

		$('#carousel3').jcarousel({
			scroll: 1,
			visible: 4,
			wrap: 'circular',
			auto: 5,
			initCallback: carousel_initCallback,
			itemFirstInCallback:{ 
				onBeforeAnimation: carousel_unblockall,
				onAfterAnimation: carousel_changed
			}
		});

		// Tabs
		$('#publications').click(function() {

			$('#publications').removeClass('onglet-off').addClass('onglet-on');
			$('#publications a').removeClass('onglet-off').addClass('onglet-on');
			$('#livres').removeClass('onglet-on').addClass('onglet-off');
			$('#livres a').removeClass('onglet-on').addClass('onglet-off');
			$('#expertise').removeClass('onglet-on').addClass('onglet-off');
			$('#expertise a').removeClass('onglet-on').addClass('onglet-off');
			$('#agenda').removeClass('onglet-on').addClass('onglet-off');
			$('#abonnement').removeClass('onglet-on').addClass('onglet-off');

			$('#onglet-separateur1').removeClass('onglet-separation1').removeClass('onglet-separation2').addClass('onglet-separation3');
			$('#onglet-separateur2').removeClass('onglet-separation2').removeClass('onglet-separation3').addClass('onglet-separation1');
			$('#onglet-separateur3').removeClass('onglet-separation2').removeClass('onglet-separation3').addClass('onglet-separation1');

			$('#maxi-link2').html('Feuilleter ce specimen');

			carousel_stopAll();
			onglet_hideAll();
			$('#onglet-publications').show();
			carousel_current = $('#carousel1').data('jcarousel');
			carousel_switchText();
			carousel_stopAll();
			carousel_current.startAuto();

		});
		$('#livres').click(function() {

			$('#publications').removeClass('onglet-on').addClass('onglet-off');
			$('#publications a').removeClass('onglet-on').addClass('onglet-off');
			$('#livres').removeClass('onglet-off').addClass('onglet-on');
			$('#livres a').removeClass('onglet-off').addClass('onglet-on');
			$('#expertise').removeClass('onglet-on').addClass('onglet-off');
			$('#expertise a').removeClass('onglet-on').addClass('onglet-off');
			$('#agenda').removeClass('onglet-on').addClass('onglet-off');
			$('#abonnement').removeClass('onglet-on').addClass('onglet-off');

			$('#onglet-separateur1').removeClass('onglet-separation1').removeClass('onglet-separation3').addClass('onglet-separation2');
			$('#onglet-separateur2').removeClass('onglet-separation1').removeClass('onglet-separation2').addClass('onglet-separation3');
			$('#onglet-separateur3').removeClass('onglet-separation2').removeClass('onglet-separation3').addClass('onglet-separation1');

			$('#maxi-link2').html('Couverture et sommaire');

			carousel_stopAll();
			onglet_hideAll();
			$('#onglet-livres').show();
			carousel_current = $('#carousel2').data('jcarousel');
			carousel_switchText();
			carousel_stopAll();
			carousel_current.startAuto();

		});
		$('#expertise').click(function() {

			$('#publications').removeClass('onglet-on').addClass('onglet-off');
			$('#publications a').removeClass('onglet-on').addClass('onglet-off');
			$('#livres').removeClass('onglet-on').addClass('onglet-off');
			$('#livres a').removeClass('onglet-on').addClass('onglet-off');
			$('#expertise').removeClass('onglet-off').addClass('onglet-on');
			$('#expertise a').removeClass('onglet-off').addClass('onglet-on');
			$('#agenda').removeClass('onglet-on').addClass('onglet-off');
			$('#abonnement').removeClass('onglet-on').addClass('onglet-off');

			$('#onglet-separateur1').removeClass('onglet-separation2').removeClass('onglet-separation3').addClass('onglet-separation1');
			$('#onglet-separateur2').removeClass('onglet-separation1').removeClass('onglet-separation3').addClass('onglet-separation2');
			$('#onglet-separateur3').removeClass('onglet-separation2').removeClass('onglet-separation3').addClass('onglet-separation1');

			$('#maxi-link2').html('Accéder au sommaire');

			carousel_stopAll();
			onglet_hideAll();
			$('#onglet-expertises').show();
			carousel_current = $('#carousel3').data('jcarousel');
			//carousel_switchText();
			carousel_setText(carousel_current.get(1));
			carousel_stopAll();
			//carousel_current.startAuto();
		});

		$('#agenda').click(function() {
			$('#publications').removeClass('onglet-on').addClass('onglet-off');
			$('#publications a').removeClass('onglet-on').addClass('onglet-off');
			$('#livres').removeClass('onglet-on').addClass('onglet-off');
			$('#livres a').removeClass('onglet-on').addClass('onglet-off');
			$('#expertise').removeClass('onglet-on').addClass('onglet-off');
			$('#expertise a').removeClass('onglet-on').addClass('onglet-off');
			$('#agenda').removeClass('onglet-off').addClass('onglet-on');
			$('#abonnement').removeClass('onglet-on').addClass('onglet-off');

			$('#onglet-separateur1').removeClass('onglet-separation2').removeClass('onglet-separation3').addClass('onglet-separation1');
			$('#onglet-separateur2').removeClass('onglet-separation2').removeClass('onglet-separation3').addClass('onglet-separation1');
			$('#onglet-separateur3').removeClass('onglet-separation1').removeClass('onglet-separation2').addClass('onglet-separation3');

			carousel_stopAll();
			onglet_hideAll();
			$('#onglet-livres').show();
			carousel_current = $('#carousel2').data('jcarousel');
			carousel_switchText();
			carousel_stopAll();
			carousel_current.startAuto();


		});
		$('#abonnement').click(function() {
			$('#publications').removeClass('onglet-on').addClass('onglet-off');
			$('#publications a').removeClass('onglet-on').addClass('onglet-off');
			$('#livres').removeClass('onglet-on').addClass('onglet-off');
			$('#livres a').removeClass('onglet-on').addClass('onglet-off');
			$('#expertise').removeClass('onglet-on').addClass('onglet-off');
			$('#expertise a').removeClass('onglet-on').addClass('onglet-off');
			$('#agenda').removeClass('onglet-on').addClass('onglet-off');
			$('#abonnement').removeClass('onglet-off').addClass('onglet-on');

			$('#onglet-separateur1').removeClass('onglet-separation2').removeClass('onglet-separation3').addClass('onglet-separation1');
			$('#onglet-separateur2').removeClass('onglet-separation2').removeClass('onglet-separation3').addClass('onglet-separation1');
			$('#onglet-separateur3').removeClass('onglet-separation1').removeClass('onglet-separation3').addClass('onglet-separation2');

			carousel_stopAll();
			onglet_hideAll();
			$('#onglet-publications').show();
			carousel_current = $('#carousel1').data('jcarousel');
			carousel_switchText();
			carousel_stopAll();
			carousel_current.startAuto();

		});
		
		// Initial state
		$('#maxi-link2').html('Feuilleter ce specimen');
		carousel_current = $('#carousel1').data('jcarousel');
		carousel_stopAll();
		onglet_hideAll();
		$('#onglet-publications').show();
		carousel_current.startAuto();

		/*$('#image-livre').click(function() { 
			lbox();
		}); 
		$('#maxi-link').click(function() { 
			lbox();
		}); */
		
		
		// Fix for Chrome
		/*carousel = $('#carousel3').data('jcarousel');
		item = carousel.get(1);
		carousel_unblockall(carousel,item,1);
		//carousel.next();
		carousel = $('#carousel2').data('jcarousel');
		item = carousel.get(1);
		carousel_unblockall(carousel,item,1);
		//carousel.next();
		carousel = $('#carousel1').data('jcarousel');
		item = carousel.get(1);
		carousel_unblockall(carousel,item,1);*/

		carousel_setText(carousel_current.get(2));
		$('#publications').addClass('onglet-on');
		$('#publications a').addClass('onglet-on');
		$('#livres').addClass('onglet-off');
		$('#livres a').addClass('onglet-off');
		$('#expertise').addClass('onglet-off');
		$('#expertise a').addClass('onglet-off');
		$('#agenda').addClass('onglet-off');
		$('#agenda a').addClass('onglet-off');
		$('#abonnement').addClass('onglet-off');
		$('#abonnement a').addClass('onglet-off');

		$('#onglet-separateur1').removeClass('onglet-separation1').addClass('onglet-separation3');


	};

