/***************************************/
/****** Initialise les fonctions */
/***************************************/
function init() {
	slider();
	initNav();
	initSlideshow();
	initSlideshowRange();
	initTabs();
	initAccordion();
	initSelectBox();
	nth();
}
/***************************************/
/******** slider               *******/
/*************************************/

function slider() {
		
	$(window).blur(function() {
		$('#slider-centered').cycle('pause');
	});

	$(window).focus(function() {
		$('#slider-centered').cycle('resume');
	});

	$('#slider-centered').cycle({ 
		slideExpr: '.slider',
		timeout: 6000,
		before: onBefore,
		//after: onAfter,
		fx:'custom'
	}); 
	
	function onBefore(curr, next, opts, fwd) {
		var $curr = $(curr);
		var $next = $(next);
		
		$curr.find('.lang-fr').animate({left:'-350px'},{duration:300, easing:'easeInQuint'});
		$curr.find('.lang-nl').animate({left:'-350px'},{duration:400, easing:'easeInQuint'});
		$curr.find('.shadow').animate({left:'-350px'},500, 'easeInQuint', function(){
			$curr.hide();
			
			$next.find('.lang-fr').css("left","1200px");
			$next.find('.lang-nl').css("left","1200px");
			$next.find('.shadow').css("left","1200px");
			
			$next.show();
			$next.find('.lang-fr').show();
			$next.find('.lang-nl').show();
			$next.find('.shadow').show();
			
			$next.find('.lang-fr').animate({left:'0'},{duration:300, easing:'easeOutExpo'});
			$next.find('.lang-nl').animate({left:'0'},{duration:400, easing:'easeOutExpo'});
			$next.find('.shadow').animate({left:'0'},{duration:600, easing:'easeOutExpo'});
		});

	}	

}

/********************************/
/* Simulation des Pseudo-child */
/******************************/
function nth() {
	$('#dealers > li:nth-child(3n+1)').css({clear:'both', marginLeft:'0'});
}

/********************************/
/* Slideshow home */
/******************************/
function initSlideshow() {
	$('#slideshow').nivoSlider({effect:'boxRainGrow', animSpeed:500, pauseTime:4000, controlNav:false, directionNav:false});	
}

function initSlideshowRange() {
	$('.slideshow').nivoSlider({effect:'boxRainGrow', animSpeed:500, pauseTime:6000, controlNav:false, directionNav:false});	
}

function initTabs() {
	$("#tabs").tabs({
			collapsible: true,
			selected: -1
		});	
	$(".ui-tabs-nav li").each(function(index) {
		$(this).addClass('tab-element-' + index);							   
	});
}

function initAccordion() {
	$("#accordion").accordion({
			autoHeight: false,
			navigation: true
		});	
}

function initSelectBox() {
	$('.fake-select').selectBox();	
}

/********************************/
/* Navigation */
/******************************/
function initNav() {
	
	$('#nav > li:not(#r_179) > ul').each(function() {
		var firstHeight = $(this).outerHeight();
		var newHeight = (firstHeight - 22);

		$(this).find('ul').each(function() {
			var secondHeight = $(this).outerHeight();
			
			if (firstHeight >= secondHeight) {
				$(this).css('height', newHeight + 'px');
			} 
		});
	});
	/* On déplace certaines rubriques du menu XXL */
	var gammeArtyk = $('#nav #r_247 div').html();
	var videos = $('#nav #r_248 div').html();
	$('#nav #r_238 div').append(gammeArtyk);
	//$('#nav #r_230 div').append(videos);
	$('#nav #r_247').remove();
	//$('#nav #r_248').remove();
	
	/* Taille égale pour les colonnes du menu XXL */
	$('#nav #r_179 li div').equalHeightColumns();
	
	/* On enlève le position:relative des éléments de second niveau, puis on les masque */
	$('#nav > li:not(#r_179) > ul > li').css('position','static');
	$('#nav > li > ul').hide().css('left','0');
	/* Le dernier élément est calé à droite */
	$('#nav > li:last-child > ul').css({left:'auto', right:'0px'});
	/* Positionnement des éléments de 3ème niveau */
	$('#nav > li:not(#r_179) > ul > li > div > ul').hide().css({left:'202px',top: '-1px'});
	$('#nav #r_182 > ul > li > div > ul').css({left:'242px',top: '-1px'});
	$('#nav #r_185 > ul > li > div > ul').css({left:'192px',top: '-1px'});

	/* Apparition des sous niveau */
	$("#nav > li").hoverIntent(
		function() {
			$('> ul', this).stop(true, true).slideDown();
		},
		function() {
			$('> ul', this).stop(true, true).slideUp();
		}
	);
	$("#nav li:not(#r_179) ul li").hoverIntent(
		function() {
			$('div ul', this).fadeIn();
		},
		function() {
			$('div ul', this).fadeOut();
		}
	);
	
}

/***************************************/
/* initialisation des fonctions */
/*************************************/
$(init);
