var Slider = ( function () {
	
	var current = 1,
		old = 0,
		total = 0,
		delay = 6000,
		auto = true,
		duration = 600,
		reg = /[0-9]$/,
		timer = null;

	function show() {

		$('#actu-' + current).css("display", "block");
		$('#actu-' + current).animate({
			opacity : "1"
		}, duration, onShow);
	}

	function hide() {
		$('#actu-' + old).animate({
			opacity : "0"
		}, duration, onHide);
	}

	function onShow() {
		timer = window.setTimeout(autorun, delay);
	}

	function onHide() {
		$('#actu-' + old).css("display", "none");
		changeMenu();
		show();
	}

	function changeMenu() {

		$("a.lien-numeros").each( function () {
			var matches = this.id.match(reg);
			if (matches !== null && matches.length > 0) {
				var num = parseInt(matches[0], 10);
				if (num === current) {
					if (!$(this).hasClass("current")) {
						$(this).addClass("current");
					}
				}
				else {
					if ($(this).hasClass("current")) {
						$(this).removeClass("current");
					}
				}
			}
		});
	}

	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 num;
		var matches = e.currentTarget.id.match(reg);
		if (matches !== null && matches.length > 0) {
			num = parseInt(matches[0], 10);
			if (!isNaN(num)) {
				auto = false;
				window.clearTimeout(timer);
				change(num);

			}
		}
	}

	function init() {

		total = $("div.news-container").length;
		timer = window.setTimeout(autorun, delay);

		$("div.news-container").each( function () {
			if (!/-1$/.test(this.id)) {
				$(this).css("opacity", "0").css("display", "none");
			}
		});
		$("a.lien-numeros").each( function () {
			$(this).click(click);
		});
	}

	init();
}());

