$(document).ready(function() {
	initArticle();
});

/**
 * Init
 */
function initArticle() {
	if($('.fiches')[0])$('.fiches').slideshowtxt(5000);
	if($('.fiches2')[0])$('.fiches2').slideshowtxt(5000);
	
	$(".fancy").fancybox({autoScale:false,height:500,width:940,autoDimensions:false,onComplete:function(){
		$('.diapo_simple').each(function(){
			var time=$(this).next("input").val();
			$(this).cycle({
				fx: 'fade',
				delay: (time/1),
				timeout: (time/1)
			});
		});
	}});

	jQuery('.jcarousel').jcarousel({
		auto : 8,
		scroll : 3,
		animation : 1000,
		wrap : 'circular',
		initCallback : jcarouselbt
	});
	
	if($('.jcarouselt')[0] && $('.jcarouselt img').length>9){
		jQuery('.jcarouselt').jcarousel({
			auto : 8,
			scroll : 1,
			animation : 5000,
			wrap : 'circular',
			initCallback : jcarouselbt
		});
	}else{
		jQuery('.jcarouselt').jcarousel({
			initCallback : jcarouselbthide
		});
	}
}

function jcarouselbt() {
	var width = window.innerWidth;
	var height = window.innerHeight;
	var pos = $('.jcarousel-prev').offset();
	$('.jcarousel-prev').css('left', (-1 * pos.left) + 'px');
	$('.jcarousel-next').css('right', (-1 * pos.left) + 5 + 'px');
}
function jcarouselbthide() {
	$('.jcarousel-prev').hide();
	$('.jcarousel-next').hide();
}

/**
 * Filtrage des références par catégorie
 */
function filtrecat(cat) {
	
	//No uncheck all
	var count=0;
	$('.checkboxcat').each(function() {
		var element = $(this);
		img = getSrcImg(element);
		if (img == 'cocheb.png') {
			count++;
		}
	});
	
	var element = $('#cat' + cat);
	img = getSrcImg(element);
	
	if(count>1 || img == 'cochea.png'){
	
	if (img == 'cocheb.png') {
		element.attr('src', element.attr('src').replace('cocheb.png', 'cochea.png'));
	} else {
		element.attr('src', element.attr('src').replace('cochea.png', 'cocheb.png'));
	}
	//Query
	var query=new Array();
	var i=0;
	$('.checkboxcat').each(function() {
		var element = $(this);
		img = getSrcImg(element);
		if (img == 'cocheb.png') {
			query[i]='id_cat[]=' + element.attr('id').replace('cat','');
			i++;
		}
	});
	$('#pagereference').load('/plug/article/pageref',query.join('&') + '&ajx=1',function(){
		initArticle();
	});
	}
}

/**
 * Recherche de l'image d'un element img
 * 
 * @param element
 */
function getSrcImg(element) {
	var imgSrc = element.attr('src');
	var arrImgSrc = imgSrc.split('/');
	return arrImgSrc[arrImgSrc.length - 1];
}

