$(window).load( function() {

	/*
	 * ATTRIBUTS
	 */

	var settings = {
		sliderName: 'slider',
		sliderListName: 'sliderList',
		buttonContainer: 'sliderControl',
		buttonNext: 'sliderNext',
		buttonPrev: 'sliderPrev',
		initFade: 200,
		fade: 200,
		basicFade: 1200,
		enableRotator: true,
		rotatorTimer: 9000,
		current: 0
	}

	var $count = $('#background-container img').length,
	interval = "",
	timeout = "";

	var arrayColor = ['#f8f8f8', '#fcf7e4', '#ededef', '#f8f8f8', '#e8eeeb', '#e5ecef'],
	arrayImage = ['/upload/images/home/body/body-1.jpg', '/upload/images/home/body/body-2.jpg', '/upload/images/home/body/body-3.jpg', '/upload/images/home/body/body-4.jpg', '/upload/images/home/body/body-5.jpg', '/upload/images/home/body/body-6.jpg'],
	arrayLink = ['http://apps.after-mouse.com/fiche-produit/presentation-move.html', 'http://apps.after-mouse.com/fiche-produit/resto-touch-tpc.html', 'http://apps.after-mouse.com/fiche-produit/share-touch-server-license.html', 'http://apps.after-mouse.com/fiche-produit/retail-move.html', 'http://apps.after-mouse.com/fiche-produit/survey-touch.html', 'http://apps.after-mouse.com/fiche-produit/map-touch.html'];

	/*
	 * Initialisation
	 */

	var windowHeight = $(document).height(); // Get window height
	$('#background-container').css('height', windowHeight); // Set window height to background-container

	function initialisation() {
		/* On load  */
		$('#background-container').css({
			background : 'url('+arrayImage[settings.current]+') no-repeat center top '+arrayColor[settings.current],
			display: 'none'
		}).fadeIn(settings.initFade);
		linkRotator();
		$('#home #header #textes-visuels div').eq(settings.current).fadeIn();
		animation();
		clickFunction();
	}

	/* Rotator */
	function animation() {
		clearInterval(interval); /* Not necessary but prevents probs */
		rotator();
	}

	/* Heart of animation */
	function rotator() {
		interval = setInterval( function() {
			nextClick();
			linkRotator();
		}, settings.rotatorTimer);
	}

	/* Rotator murderer */
	function stopRotator() {
		timeout = setTimeout( function() {
			animation();
		}, 500);
	}

	/* Rotator murderer */
	function killAnimation(intervalToKill) {
		clearInterval(intervalToKill);
	}

	/* Main link div changes of href in the same time the new diapo appears */
	function linkRotator() {
		$('#header #links a').attr('href', arrayLink[settings.current]);
	}

	/* Check current image */
	function checkCurrent() {
		if(settings.current >= $count - 1) {
			settings.current = 0;
		} else if(settings.current < 0) {
			settings.current = ($count - 1);
		}
	}

	function nextClick() {
		$('#background-container').fadeOut(50, function() {
			$('#home #header #textes-visuels div').eq(settings.current).fadeOut();
			settings.current++; /* Incrementation */
			checkCurrent();
			$(this).css({
				background : 'url('+arrayImage[settings.current]+') no-repeat center top '+arrayColor[settings.current]
			}).fadeIn(settings.initFade);
			$('#home #header #textes-visuels div').eq(settings.current).fadeIn( function() {
				linkRotator();
			});
		});
	}

	function prevClick() {
		$('#background-container').fadeOut(50, function() {
			$('#home #header #textes-visuels div').eq(settings.current).fadeOut();
			settings.current--; /* Incrementation */
			checkCurrent();
			$(this).css({
				background : 'url('+arrayImage[settings.current]+') no-repeat center top '+arrayColor[settings.current]
			}).fadeIn(settings.initFade);
			$('#home #header #textes-visuels div').eq(settings.current).fadeIn( function() {
				linkRotator();
				console.log('link changes : number' + settings.current);
			});
		});
	}

	function clickFunction() {
		$('#slider a').click( function(e) {
			killAnimation(interval); /* Kill animation */
			clearTimeout(timeout);
			$this = $(this);
			if($this.is('.sliderPrev')) {
				prevClick();
			} else {
				nextClick();
			}
			stopRotator();
			e.preventDefault();
		});
	}

	initialisation(); /* Go ! */

});
