// PRE dom **************************************

//on cache les images puis on fade in quand le dom est chargé
//jenleve le code, ca chie a cause du cache et des prechargements
//$('.Collection img').css('opacity', '0.5');
//$(".Mosaic img").css("opacity", "0.5");

// DOM ********************************************
$(document).ready(function() {

	// lien sur accroche, mis en Js. Pas envie de pourrir le DOM avec une betise pareille
	$('.Accroche').click(function() {
		window.location = 'http://www.bookapp.com/';
	});

	//boutons carousel
	$('.CarouselBtnPrev, .CarouselBtnNext').css({'opacity':'0', 'display':'block'}); //on active les btns que si JS actif
	
/* 	$('.CarouselBtnPrev, .CarouselBtnNext').hover(function() {  //animation clic fleches carousel
		$(this).attr("src", $(this).attr("src").split(".").join("-hover.")); 
	}, function() {
		$(this).stop(true,false); 
		$(this).attr("src", $(this).attr("src").split("-hover.").join("."));
	}); */

	$('#JS-Carousel').slides({ //params carousel
		play: 5000,
		pause: 500,
		pagination: false,
		hoverPause: true,
		fadeSpeed: 400,
		effect: 'fade',
		preload : true,
		crossfade: true,
		next: 'CarouselBtnNext',
		prev: 'CarouselBtnPrev',
		container: 'slides_container',
		generatePagination: true,
		paginationClass: 'CarouselPagination',
		preloadImage: 'img/loading.gif',
		severalSlide: function() {
			$('.Carousel').hover(function() { //fade btn carousel
				$('.CarouselBtnPrev, .CarouselBtnNext').stop().animate({ opacity: 1}, 100);
				 }, function() {
				$('.CarouselBtnPrev, .CarouselBtnNext').delay(1000).stop().animate({ opacity: 0}, 4000);
			});
		}
	});
	
	
	// Activation des tabs
	 $("#JS-Tabs").tabs({fx:{opacity: "toggle"}});

	//si tabs, on met le BG en reprenant la couleur du conteneur des tabs
	 if ($("#JS-Tabs").length > 0){	
		$bgColor = $("#JS-Tabs").css('backgroundColor');
		  $('<div id="JS-BG" style="position: absolute; top: 125px; left: 0; z-index: -5; background-color: ' + $bgColor + ' ;  width: 100%; height: 640px"></div>').appendTo($('body'));
	}
	
	//target blank pour les liens sortants
	$(".Container a[href^='http']").attr('target','_blank');
	
	//on ré-affiche les images qui etait en opacity 0.5 
	//imagesMosaique('.Mosaic');
	//imagesMosaique('.Collection');
	//annule

	//La nav0 pour IE, il gere tres mal les pseudo classes. ** YOU DON'T SAAYYY **
	if ( $.browser.msie ) {
		$("nav.Menu li.Nav0").each(function(){
			$(this).children('ul').css('opacity' , '1');
			
			$(this).hover(function() {  //animation
				$(this).children('ul').css({'visibility' : 'visible', 'opacity' : '1'});
			}, function() {
				$(this).children('ul').stop(true,false); 
				$(this).children('ul').css({'visibility' : 'hidden', 'opacity' : '0'});
			})
		})
	}

});  
// FIN CHARGEMENT

/* VARS */

/* FUNCTIONS */


	/* cree le bg, applique la couleur et l'image au background en fondu */
	/* plus utilisé */
	function backgroundsCarousel(current,fadeSpeed){
	
		$bgColor = $("#JS-Carousel article").eq(current).css('backgroundColor');	//couleur du slide
		$bgImage = $("#JS-Carousel article").eq(current).css('backgroundImage');	//image du slide
		$height = $("#JS-Carousel").css('height');	//hauteur du carousel
		
		if (current == undefined){current = '0'} //si pas de slide, on prend la premiere
		if ($bgColor == undefined){$bgColor = '#fff'} //si pas de slide, on prend la premiere
	
		// si la div est pas encore là on la cree, sinon on anime
		if ($("#JS-BG").length > 0){
			$('#JS-BG').animate({backgroundColor : $bgColor}, fadeSpeed);
			$('#JS-BG').css('background-image',$bgImage);
			// ! background image impossible a animer
		}else{
			$('<div id="JS-BG" ></div>').appendTo($('body')); //firefux  fault : 
			$('#JS-BG').css({'position':'absolute','top':'125px', 'left': '0', 'z-index': '-5', 'background-color': $bgColor , 'background-image': $bgImage,'width': '100%', 'height': $height,'opacity':'0'});
			$('#JS-BG').animate({ opacity: 1}, fadeSpeed);
		}
	}

	
		//fonction books home
		function menuAsynchrone () {
			// nav0
			$("div.Menu li.Nav0 a").not('#menu_search').click(function(event){ // Affichage contenu : menu & animation menu/contenu
					
					event.preventDefault(); //desacticvation href

					$("div.Menu li.Nav0 a").removeClass();	//statut actif du menu
					$(this).addClass('Actived');
					
					//activation affichage tab
					var currentId = $('#wrapper ul:visible').attr('id').split("_"); //Tab active
					var actualActiveTab = 'tab_' + currentId[1];
					var futureId = $(this).attr('id').split("_");//Future tab
					var newActiveTab = 'tab_' + futureId[1];
					var dureeAnimation = 200;
					
					// on teste avant animation
					if( actualActiveTab != newActiveTab) {

						//animation
						$("#" + newActiveTab).css('opacity', '0');
						$("#" + actualActiveTab).animate({
							opacity: 0
						}, dureeAnimation, function() {
								$(this).css('display' , 'none');
								$(this).attr('hidden', 'hidden');
								$("#" + newActiveTab).css('display', 'block');
								$("#" + newActiveTab).removeAttr('hidden');
						});
						
						$("#" + newActiveTab).delay(dureeAnimation).animate({
							opacity: 1
						}, dureeAnimation);
						
						//Breadcrubs. On recupere le tab actif et on ajoute 
						if (futureId[1] == 'all'){
							newBD = '';
						}
						if (futureId[1] == 'auteurs'){
							newBD = ' > Auteurs';
						}
						if (futureId[1] == 'editeurs'){
							newBD = ' > Editeurs';
						}
						if (futureId[1] == 'tags'){
							newBD = ' > Thèmes';
						}
						if (futureId[1] == 'genres'){
							newBD = ' > Genres';
						}
						if (futureId[1] == 'result'){
							newBD = ' > Recherche';
						}
						$('#JS-breadcrubs').text(newBD); //on insere l'HTML
					}

			});//fin nav0
			
			//nav1
			$("div.Menu li.Nav1 a").click(function(event){ // detection tri
			
				event.preventDefault(); //desacticvation href
				
				//activation affichage tab
					var currentId = $('#wrapper ul:visible').attr('id').split("_"); //Tab active
					var actualActiveTab = 'tab_' + currentId[1]; //Var Tab active
					
					var newActiveTab = 'tab_result';
					var dureeAnimation = 200;

					//animation
					$("#" + newActiveTab).css('opacity', '0');
					$("#" + actualActiveTab).animate({
						opacity: 0
					}, dureeAnimation, function() {
							$(this).css('display' , 'none');
							$("#" + newActiveTab).css('display', 'block');
					});
					$("#" + newActiveTab).delay(dureeAnimation).animate({
						opacity: 1
					}, dureeAnimation);

			});// fin nav1
		};
		
// form changement langue
	$('li.JS-Lang').click(function() {
		$('#JS-langForm').submit();
	});
	
//fonction  chargement image mosaique
	function imagesMosaique(target) {
		//on la ré-affiche avec un effet de transparence
		if ( $.browser.msie ) {
			$('img', target).each(function(){  this.src = this.src + '?stupid-IE=' + (new Date()).getTime()}).load(function(){ $(this).delay(50).animate({ opacity: 1}, 200);});
		} else {
			$('img', target).load(function() { $(this).delay(50).animate({ opacity: 1}, 200);});
		}

	};
