jQuery.noConflict();
jQuery(document).ready(function() {
	var Slider = (function() {
		var current = 1,
			old = 0,
			total = 0,
			delay = 6000,
			auto = true,
			duration = 600,
			reg = /[0-9]$/,
			timer = null;
		
		function show() {
			jQuery('#img-carousel-' + current).animate({opacity : "1"}, duration, onShow);
		}
		
		function hide() {
			jQuery('#img-carousel-' + old).animate({opacity : "0"}, duration, onHide);			
		}
		
		function onShow() {
			timer = setTimeout(autorun, delay);
		}

		function changeMenu() {
			
			jQuery(".bouton-carousel").each(function() {
				var matches = this.id.match(reg);				
				if (matches !== null && matches.length > 0){
					var num = parseInt(matches[0], 10);
					if (num === current) {						
						if (!jQuery(this).hasClass("actif")) {
							jQuery(this).addClass("actif");
						}
					}
					else{					
						if (jQuery(this).hasClass("actif")) {
							jQuery(this).removeClass("actif");
						}
					}
				}
			});
		}
		
		function onHide() {		
			changeMenu();
			show();
		}
		
		function change(num) {
			if (num != current) {	
				old = current;
                current = num;
                hide();
            }
		}
		
		function autorun() {			
			if (auto){
				var num = current + 1;
				if (num > total) {
					num = 1;
				}
				change(num);			
			}
		}

		function click(e) {

			var matches = e.currentTarget.id.match(reg);			
			if (matches !== null && matches.length > 0) {
				var num = parseInt(matches[0], 10);
				if (!isNaN(num)) {					
					auto = false;
					clearTimeout(timer);	
					change(num);
									
				}	
			}
		}
		
		function init() {
			total = jQuery(".img-carousel").length;
			timer = setTimeout(autorun, delay);

			jQuery(".img-carousel").each(function() {
				if (!/-1$/.test(this.id)) {
					jQuery(this).css("opacity", "0");
				}
			});
			
			jQuery(".bouton-carousel").each(function() {
				jQuery(this).click(click);
			});
		}
		init();
	}());
});
