(function($){

	$(function() {

/*
 * @section Éléments communs
******************************************************************************/

	var $body = $('body');
	$.fn.hauteurEgale = function() {
		var maxH = 0;
		$(this)
		.css( 'height', 'auto' )
		.each(function() {
			if ( $(this).height() > maxH ) {
				maxH = $(this).height();
			}
		})
		.height( maxH );
		return $(this);
	}

	$.fn.bindHauteurEgale = function() {
		var $this = $(this).hauteurEgale();
		$(document).bind("fontresize", function (event, data) {
			$this.hauteurEgale();
		});
	}

	// Liens externes
	$('a[href^=http://]').not('a[href*='+window.location.host+']').not('a[href*='+BOREAL_BLOG_URL+']').not('a[href*='+BOREAL_URL+']').addClass('externe').attr('target','_blank');

/*
 * @section Accueil - Diaporama
******************************************************************************/
	$(document).ready(function(){
		$(".diaporama").diaporama({
			animationSpeed: "slow",
			delay: 7,
			controls: false
		});
	});
/*
 * @section Champs de recherche
******************************************************************************/

	$('#recherche #s, #sous-recherche span input').not('.plein').focus(function() {
		if ( $(this).val() == this.defaultValue ) {
			$(this).val( '' );
		}
	}).blur( function() {
		if ( $(this).val() == '' ) {
			$(this).val( this.defaultValue );
		}
	});

/*
 * @section Auto-complete
******************************************************************************/

	$('#livre-recherche').autocomplete( 'ajax.html', {
		delay: 400,
		minChars : 2,
		scroll : false,
		autoFill : false,
		highlight : false,
		width: 215,
		selectFirst: false,
		cacheLength: 1
	});

	$('#auteur-recherche').autocomplete( 'ajax.html', {
		delay: 400,
		minChars : 2,
		scroll : false,
		autoFill : false,
		width: 215,
		selectFirst: false,
		cacheLength: 1
	});


/*
 * @section Liste de sélection personnalisée
******************************************************************************/

		$('div.select ul').hide();
		$('div.select .label a').click(function(e) {
			$(this).parent().next().slideToggle(100);
			e.stopPropagation();
			e.preventDefault();
			return false;
		});

		$(document).click(function() {
			var $ul = $('div.select ul');
			if ( $ul.is(':visible') ) {
				$ul.slideUp(100);
			}
		});

/*
 * @section Fiche d'auteur
******************************************************************************/

		if ( $body.is('.template-fiche-auteur') ) {
			$('#presse').hide();
			$('.toggle a').click(function() {
				$(this).parent().addClass('active').siblings().removeClass('active');
				var id = $(this).attr('href');
				$( id ).show().siblings('.contenu-texte').hide();
				return false;
			});
		}

/*
 * @section Auteurs récent
******************************************************************************/

		if ( $body.is('.template-auteurs-recents') ) {
			$('.auteur .publications li a').each(function() {
				var meta = $.metadata.get(this);
				if (meta && meta.vignette) {
					$(this).attr('title', '<img src="'+meta.vignette+'" />');
				}
			}).tooltip({
				delay : 0,
				track: true,
				showURL : false
			});
		}

/*
 * @section Fiche de livre
******************************************************************************/

		if ( $body.is('.template-fiche-livre') ) {

			$('.catalogue-entete .livre-nav a').each(function() {
				var meta = $.metadata.get(this);
				if (meta && meta.vignette) {
					$(this).attr('title', '<img src="'+meta.vignette+'" />');
				}
			}).tooltip({
				delay : 0,
				track: true,
				showURL : false
			});

			$('.couverture a').click(function() {
				var couverture = new Image;
				var $this = $(this);
				var $contenu = $(this).parents('#contenu');
				var largeur = $(this).find('img').width();
				var hauteur = $(this).find('img').height();
				couverture.onload = function() {
					couverture.onload = function() {};
					var l_largeur = couverture.width; var l_hauteur = couverture.height;
					$contenu.find('.couverture-large').remove();
					$contenu.append('<div style="display: none;" class="couverture-large"><img src="'+$this.attr('href')+'" alt="" /></div>').find('.couverture-large img').click(function() {
						$(this).stop().animate({
							width: largeur,
							height: hauteur
						},{
							duration: 300,
							easing: 'easeOutCirc',
							complete : function() {
								$(this).parent().remove();
							}
						});
					});
					$contenu.find('.couverture-large img').css('width', largeur+'px').css('height', hauteur+'px').parent().show().find('img').stop().animate({
						width: l_largeur,
						height: l_hauteur
					},{
						duration: 300,
						easing: 'easeOutCirc'
					});

				};
				couverture.src = $this.attr('href');
				return false;
			});

		}

/*
 * @section Hauteurs de rangée égale
******************************************************************************/


		if ( $body.is('.template-fiche-auteur') ) {
			$('#sidebar .livres .liste').each(function() {
				$(this).find('.titre').bindHauteurEgale();
				$(this).find('.type').bindHauteurEgale();
			});
		}

		if ( $body.is('.template-liste-livres') || $body.is('.template-auteurs-recents') || $body.is('.template-livres-recents') ) {
			$('#contenu .rangee').each(function() {
				$(this).find('h2').bindHauteurEgale();
				$(this).find('.description').bindHauteurEgale();
				$(this).find('.type').bindHauteurEgale();
			});
		}

		if ( $body.is('.template-accueil-themes') ) {
			$('#themes .rangee').each(function() {
				$(this).find('h2').bindHauteurEgale();
			});
		}

		if ( $body.is('.template-foreign-rights') ) {
			$('.livres').find('.titre').bindHauteurEgale();
		}

		if ( $body.is('.template-foreign-rights') && $body.is('.template-foreign-rights-catalogue') ) {
			$('.listing-livres .rangee').each(function() {
				$(this).find('.titre').bindHauteurEgale();
			});
		}

		if ( $body.is('.template-accueil') ) {
			$('.listing-livres').each(function() {
				var compteur = 0;
				var livres = [];
				$(this).find('.liste .span-2').each(function() {
					compteur++;
					livres.push(this);
					if (compteur == 5) {
						$( livres ).find('h2').bindHauteurEgale();
						$( livres ).find('.type').bindHauteurEgale();
						compteur = 0;
						livres = [];
					}
				});
				if (compteur%5!=0) {
					$( livres ).find('h2').bindHauteurEgale();
				}
			});
		}

/*
 * @section Listing de livres à l'accueil
******************************************************************************/

		if ( $body.is('.template-accueil') ) {

			$('.listing-livres .pagination ul').show();
			$('.livres .nav-commune a').click(function() {
				$(this).parent().addClass('active').siblings().removeClass('active');
				$(this).parentx(3).next().find( $(this).attr('href') ).show().find('.liste').trigger('activate').parentx(2).siblings().hide();
				return false;
			});

			jQuery.fn.pagination = function() {

				$(this).each(function() {

					var $self = $(this);
					var $numeros = $self.find('li').not('.next, .prev');
					var $fleches = $self.find('li').filter('.next, .prev');
					var $liste = $self.parent().next().find('.liste');
					var page_actuelle = 0;
					var width = 600;
					var duration = 500;
					var $next = $fleches.filter('.next');
					var $prev = $fleches.filter('.prev');

					function update_nav() {
						if ( page_actuelle == 0 ) { $prev.addClass('prev-inactive'); }
						else { $prev.removeClass('prev-inactive'); }
						if ( page_actuelle == $numeros.length - 1 ) { $next.addClass('next-inactive'); }
						else { $next.removeClass('next-inactive'); }
					}
					update_nav();

					function aller(i) {
						page_actuelle = i;
						$numeros.eq(page_actuelle).addClass('active').siblings().removeClass('active');
						$liste.stop().animate({
							'marginLeft' : (page_actuelle * width * -1)+'px'
						},{
							duration: duration
						});
						update_height( true );
						update_nav();
					}

					function update_height( animate ) {
						var $page = $liste.find('.span-10').eq( page_actuelle );
						
						if ( animate ) {
							$liste.parent().stop().animate({
								height: $page.height() + 'px'
							},{
								duration: duration
							});
						} else {
							$liste.parent().height( $page.height() );
						}
					}
					update_height( false );
					
					$liste.bind('activate', function() {
						update_height( false );
					});
					
					$(document).bind("fontresize", function (event, data) {
						if ( $liste.is(':visible') ) {
							update_height( false );
						}
					});

					$numeros.find('a').click(function() {
						var index = $numeros.index( this.parentNode );
						aller( index );
						return false;
					});

					$fleches.find('a').click(function() {
						if ( $(this).parent().is('.next-inactive') || $(this).parent().is('.prev-inactive') ) { return false; }
						if ( $(this).parent().is('.next') ) { aller( page_actuelle + 1 ); }
						else { aller( page_actuelle - 1 ); }
					});

				});

				return $(this);

			}

			$('.listing-livres .pagination ul').pagination();
			$('.listing-livres').eq(0).siblings().hide();

		}

	});

	/**
	 *
	 *
	 */
	$(window).load(function() {
		var $body = $('body');
		if ( $body.is('.template-liste-livres') || $body.is('.template-auteurs-recents') || $body.is('.template-livres-recents') ) {
			$('#contenu .rangee').each(function() {
				$(this).find('.photo').bindHauteurEgale();
			});
		}
		if ( $body.is('.template-liste-auteurs') ) {
			$('#contenu .rangee').each(function() {
				$(this).find('.photo').bindHauteurEgale();
			});
		}
		if ( $body.is('.template-liste-livres') || $body.is('.template-auteurs-recents') || $body.is('.template-livres-recents') ) {
			$('#contenu .rangee').each(function() {
				$(this).find('.photo').bindHauteurEgale();
			});
		}
		if ( $body.is('.template-fiche-auteur') ) {
			$('#sidebar .livres .liste').each(function() {
				$(this).find('.vignette').bindHauteurEgale();
			});
		}
		if ( $body.is('.template-foreign-rights') && $body.is('.template-foreign-rights-catalogue') ) {
			$('.listing-livres .rangee').each(function() {
				$(this).find('.photo').bindHauteurEgale();
			});
		}
	});

})(jQuery);

