$(document).ready(function() {
	// CAROUSEL HOME
	if ($('#pagehome').length){
		if ( !device_smallwidth(480) ){
			mycarousel_prepare();
		}
	}
	
	// ACTIVITES
	$('.activ_item').last().addClass('last');
	$('.activ_desc').each(function(){
		$(this).children().first().addClass('first_child_line');
	});
	
	
	// PUBLICATIONS
	$('#publi_liste li').last().addClass('last');
	
});



$(window).load(function(){
	// PUBLICATIONS
	if (!device_smallwidth(480)){
		$('.publi_head').click(function(){
			$(this).toggleClass('bodyon').next('.publi_body').slideToggle();
		});
	}
	
	// HOME
	$('#homecarousel').find('.prehome_go').click(function(event){
		event.preventDefault();
		$('#homecarousel').jcarousel('next');
		$('#homecarousel').jcarousel('startAuto', 3);
	});
});



$(window).resize(function() {
	if ( !device_smallwidth(480) & ($('#homecarousel').length) ){
		mycarousel_prepare();
		$('#cloneintro').css('display','none');
	} else if ( device_smallwidth(480) & ($('#homecarousel').length) ){
		$('.jcarousel-skin-ha').css('display','none');
		if (cloneintro===undefined) {
			var cloneintro=$('<div id="cloneintro"/>').html( $('#homecarousel li.intro').html() ).appendTo('#main');
		}
		$('#cloneintro').css('display','block');
	}
});



function mycarousel_prepare(){
	if (!$('#homecarousel').find('img').length) {
		// prepare the carousel
		var carousel = $('#homecarousel').addClass('jcarousel-skin-ha');
		carousel.find('li:not(.intro)').append(function(index){
			var hash = $(this).data('activite-imgsrc');
			return '<img src='+hash+' width="925" height="500" alt="" />';
		});
	}
	if (!$('div.jcarousel-skin-ha').length){
		carousel.jcarousel({
			scroll : 1,
			// 'auto' : 3,
			animation: 800,
			easing: 'easeInSine',
			wrap : 'last',
			itemFallbackDimension : 925,
			initCallback: mycarousel_initCallback
		});
	}else{
		$('.jcarousel-skin-ha').css('display','block');
	}
}

function mycarousel_initCallback(carousel){
	// Disable autoscrolling if the user clicks the prev or next button.
	carousel.buttonNext.bind('click', function() {
		carousel.startAuto(0);
	});
	carousel.buttonPrev.bind('click', function() {
		carousel.startAuto(0);
	});

	// Pause autoscrolling if the user moves with the cursor over the clip.
	// $('#homecarouselGO').hover(
	// 	function() { carousel.stopAuto();  }, 
	// 	function() { carousel.startAuto(2); }
	// );
};

function device_smallwidth(thesize){
	// if (screen.width<thesize) ? return true : return false;			// max-device-width
	// if (document.documentElement.clientWidth<thesize) ? return true : return false;			// max-device-width

	//si max-device-width<480 alors certains css sont appliqués
	if ($('#header_ppal').find('h1').width()==238) {return true } else { return false;}
}





