
$(document).ready(function(){

/***************************************************************************************
	=> Ouverture des liens externes (et des PDF) dans un nouveau navigateur/onglet <=
***************************************************************************************/
	$('a[href^=http]:not([href*="+window.location.host+"]), a[href$=.pdf]').attr('target', '_blank').append('<img src="/images/interface/icone-lien-externe.gif" width="15" height="11" class="lien-externe" alt="Lien externe" title="Lien externe" border="0" />');

/***************************************************************************************
	=> Bouton d'affichage de la grille <=
***************************************************************************************/

	$('#toggle-grille').click(function (){
		var b = $('body');
		if (b.is('.la-grille')) {
			b.removeClass('la-grille');
		} else {
			b.addClass('la-grille');
		}
		return false;
	});

/***************************************************************************************
	=> Bouton d'affichage des 3 boutons de "liens" (détails du livre) <=
***************************************************************************************/

	$('a.toggle').click(function (){
		if ($(this).hasClass('actif')) {
			$(this).removeClass('actif');
			$('.sous-menu ul').css('display','none');
		} else {
			$(this).addClass('actif');
			$('.sous-menu ul').css('display','block');
		}
		return false;
	});

/***************************************************************************************
	=> Partager
***************************************************************************************/
    var partager_urls = {
        'delicious': function() {
	        window.open('http://del.icio.us/post?v=4;url=' + encodeURIComponent(location.href) + ';title=' + encodeURIComponent(document.title), "", "width=1000,height=600,menubar=1,resizable=1,scrollbars=1");
        },
        'publieca': function() {
			window.open('http://publie.ca/submit/?url='+encodeURIComponent(window.location.href.substr(0,1024))+'&title='+encodeURIComponent(document.title.substr(0,64))+'&body='+encodeURIComponent(document.getSelection().substr(0,255)));
        },
        'facebook': function() {
            var d = document,
			f = 'http://www.facebook.com/share',
			l = d.location,
			e = encodeURIComponent,
			p = '.php?src=bm&v=4&i=0&u=' + e(l.href) + '&t=' + e(d.title);
            a = function() { if (!window.open(f + 'r' + p, 'sharer', 'toolbar=0,status=0,resizable=1,width=626,height=436')) l.href = f + p };
            if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0);
            else { a() }
        },
        'google': function() {
        	window.open('http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk=' + encodeURIComponent(location.href) + '&title=' + encodeURIComponent(document.title), "", "width=1000,height=600,menubar=1,resizable=1,scrollbars=1");
        }
    }

    $('.facebook a, .delicious a, .google a').click(function() {
        var classe = this.parentNode.className;
        partager_urls[classe].call();
        return false;
	});

/***************************************************************************************
	=> Vedette
***************************************************************************************/
	var vedette_largeur = 636;

	$('#vedette .livre').width(58);
	ouvrirLivre($('#vedette .livre:first'), true);
	
	$('#vedette .livre .couverture img').each(function() {
		var offsetLeft = 48;
		var offsetRight = 48;

		var $this = $(this);
		var imgPreloader = new Image();
		imgPreloader.onload = function() {
			imgPreloader.onload = null;
			var largeur = $this.width();
			$this.parent().parent().width( largeur ).next().width(vedette_largeur-largeur-offsetLeft-offsetRight).css('left', (largeur+offsetLeft)+'px');
		};
		imgPreloader.src = $(this).attr('src');

	});

	function ouvrirLivre($livre, init) {
		$livre.trigger('mouseout');

		var easing = (init) ? 'easeOutQuad' : 'easeOutExpo';
		var speed = (init) ? 1000 : 500;

		$('#vedette .ouvert').stop().animate({
			width: '58px'
		},{
			duration: speed,
			easing : easing,
			complete : function() {
				$(this).removeClass('ouvert');
			}
		}).find('.dos').show().animate({
			opacity: 1
		},{
			duration: speed/2,
			complete: function() {
				$(this).show();
			}
		});

		$livre.addClass('ouvert').stop().animate({
			width: vedette_largeur+'px'
		},{
			duration: speed,
			easing : easing

		}).find('.dos').animate({
			opacity: 0
		},{
			duration: speed/2,
			complete : function() {
				$(this).hide();
			}
		});
	}

	$('#vedette .livre .dos a').click(function() {

		if ($(this).parent().parent().is('.ouvert')) { return false; }
		ouvrirLivre($(this).parent().parent(), false);
		return false;

		}).parent().parent().mouseover(function() {
		if ($(this).is('.ouvert')) { return false; }
		$(this).siblings().find('.dos img').stop().animate({
			opacity: 0.4
		},{
			duration: 300
		});
	}).mouseout(function() {
		$(this).siblings().find('.dos img').stop().animate({
			opacity: 1
		},{
			duration: 300
		});
	});

});
