/*
 * jQuery throttle / debounce - v1.1 - 3/7/2010
 * http://benalman.com/projects/jquery-throttle-debounce-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function(b,c){var $=b.jQuery||b.Cowboy||(b.Cowboy={}),a;$.throttle=a=function(e,f,j,i){var h,d=0;if(typeof f!=="boolean"){i=j;j=f;f=c}function g(){var o=this,m=+new Date()-d,n=arguments;function l(){d=+new Date();j.apply(o,n)}function k(){h=c}if(i&&!h){l()}h&&clearTimeout(h);if(i===c&&m>e){l()}else{if(f!==true){h=setTimeout(i?k:l,i===c?e-m:e)}}}if($.guid){g.guid=j.guid=j.guid||$.guid++}return g};$.debounce=function(d,e,f){return f===c?a(d,e,false):a(d,f,e!==false)}})(this);


/*
 * jQuery hashchange event - v1.3 - 7/21/2010
 * http://benalman.com/projects/jquery-hashchange-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}$(f.start)},teardown:function(){if(d){return false}$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}p=setTimeout(n,$.fn[c].delay)}$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);





jQuery.extend( jQuery.easing,
{
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
});






// * ------------------------------------------------------------
// * !Une lightbox de plus...
// * ------------------------------------------------------------

(function($){
$.fn.dd_lightbox = function() {
    // si aucun élément n'est ciblé (p. ex. parce qu'on
    // n'est pas sur une page contenant des photos à afficher
    // dans une lightbox...), on sort silencieusement.
    if (this.length === 0) {
        return this;
    } else {


        // Paramètres de base
        var root = this,
            BASE = {};
            BASE.params = {
                slowFade : 200,
                fastFade : 80,
                markup :   $('<div id="dd-lightbox"><div><img></div></div>')
            };
            BASE.controls = {
                    prev     : $('<div class="dd-lb-control" id="dd-lb-controls-prev" title="Précédent">Précédent</div>'),
                    next     : $('<div class="dd-lb-control" id="dd-lb-controls-next" title="Suivant">Suivant</div>'),
                    close    : $('<div class="dd-lb-control" id="dd-lb-controls-close" title="Fermer">Fermer</div>'),
                    caption  : $('<p   class="dd-lb-control" id="dd-lb-title">Chargement en cours…</p>'),
                    
                    get      : function() {
                                    return $('.dd-lb-control').not('#dd-lb-title, #dd-lb-controls-close'); },
                    appendTo : function($target) {
                                    $target
                                        .append(this.prev)
                                        .append(this.next);
                                    this.get().show(); },
                    remove   : function(){
                                    this.get().remove(); }
            };
            
            // Obtenir la hauteur de la fenêtre à chaque resize.
            BASE.params.windowHeight =  function() { return $(window).height(); };
            $(window).bind('resize', function(){ BASE.params.windowHeight(); });
        
            
        // On optimise un peu...
        var ff = BASE.params.fastFade,
            sf = BASE.params.slowFade;
            
        // Pour chaque élément retourné
        return root.each(function() {
            
            var that = $(this);
            
                // Tous les éléments "frères" dans la galerie d'après leur attribut rel.
                // Au cas où l'attribut "rel" de this est absent, on recherche 
                // une valeur improbable pour ne pas fausser les résultats.
                that.siblings = $('[rel=' + (that.attr('rel') || 'youkaidiyoukaida') + ']');
                
                // Nombre total d'éléments dans la galerie
                that.siblingsNumber = that.siblings.length;
                
                // Position de l'élément en cours dans la galerie
                that.index    = root.index(that);
                
                   
            that.bind('click', function(e){

                e.preventDefault();
                
                // On commence par insérer dans le DOM les éléments de la lightbox
                BASE.params.markup
                    .appendTo('body')
                    .fadeIn(sf);
                
                // Référence à l'image en taille réelle.
                that.fullsize = that.attr('href'); 
                
                // On met le conteneur en cache et on règle l'action en cas de clic.
                var $lb                 = $('#dd-lightbox').data('action', 'c');
                    // Le wrapper pour l'image.
                    $lb.wrapper         = $lb.find('> div');
                    // Le tag image de la lb.
                    $lb.image           = $lb.find('img');
                    
                    // Réglage de la source de l'image avec ajout d'un argument
                    // aléatoire pour éviter des problèmes de cache.                    
                    $lb.image.setSource = function(){
                                                return $('#dd-lightbox img')
                                                    .attr('src', that.fullsize
                                                                + '?cache='
                                                                + Math.floor(Math.random()*100001) ); }
                	$lb.image.originalHeight = 0;
                	
                    // On insère le titre
                    BASE.controls.caption.appendTo($lb.wrapper);
                    
                    // On règle position et dimensions du wrapper
                    $lb.wrapper.css({
                                        'width'  : $(this).width(),
                                        'height' : $(this).height(),
                                        'top'    : $(window).height()/2 - $(this).height()/2
                                        });                        

                // Au resize la fenêtre, on place et on redimensionne
                // ce qui doit l'être si nécessaire.
                $(window).bind('resize', function(){
                    // on replace le wrapper.
                    $lb.wrapper
                        .css('top', $(window).height()/2 - $lb.image.height()/2);
                    
                    // On redimensionne l'image s'il n'y a pas assez de place
                    if ( BASE.params.windowHeight() < $lb.image.originalHeight ) {
                        $lb.image.height(BASE.params.windowHeight);
                        $lb.wrapper.width($lb.image.width());
                    }                
                });
            
                $lb.image 
                // On règle la source de l'image
                .setSource()
                // On règle l'action à effectuer en cas de clic
                .data('action','n')
                // Une fois que l'image est chargée
                .bind('load', function(){
                	
                	// On enregistre la hauteur
                	$lb.image.originalHeight = $lb.image.height();
                	
                	// On déclenche un resize pour que l'image
                	// soit redimensionnée si nécessaire
                	$(window).resize();
                	
                    // On règle les contrôles
                    BASE.controls.next.add($lb.image).add(BASE.controls.caption)
                        .data('action', that.index === that.siblingsNumber - 1 // est-ce le dernier élément?
                                            // 'close'
                                            ? 'c'
                                            // 'next'
                                            : 'n' );
                    BASE.controls.prev
                        .data('action', that.index === 0                       // est-ce le premier élément?
                                            // 'close'
                                            ? 'c'
                                            // 'prev'
                                            : 'p' );
                    
                    // On insère le bouton de fermeture
                    BASE.controls.close.appendTo($lb.wrapper)
                        .data('action','c');
                    
                    // On obtient l'élément de la galerie
                    // actuellement affiché d'après son index.
                    that.current = root.eq(that.index);
                    
                    // Auteur et titre
                    that.author  = that.current.attr('data-auteur') ? 'Photo: ' + that.current.attr('data-auteur') : '';
                    that.title   = that.current.attr('title') || '';
                    
                    // On insère un titre et un auteur éventuels
                    if (that.title && that.author) {
                        BASE.controls.caption.text(that.title + ' | ' + that.author);
                    } else if (that.title) {
                        BASE.controls.caption.text(that.title);
                    } else if (that.author) {
                        BASE.controls.caption.text(that.author);
                    } else {
                        BASE.controls.caption.remove();
                    }
                    
                    // Liens suivant et précédent.
                    if (that.siblingsNumber <= 1) {
                        BASE.controls.remove();
                    } else {
                        that.next = that.siblings.eq(that.index +1);
                        that.prev = that.siblings.eq(that.index -1);
                        BASE.controls.appendTo($lb.wrapper);
                    }
                    
                    // On règle position et dimensions du wrapper
                    $lb.wrapper.animate({
                            'width'  : $lb.image.width(),
                            'height' : $lb.image.height(),
                            'top'    : $(window).height()/2 - $lb.image.height()/2
                            }, sf);
                    
                    // On finit par afficher l'image jusque là invisible.
                    $(this).fadeIn(ff);
                    
                    // Montrer / cacher les boutons au hover.
                    $lb.wrapper.hover(function(e){
                        if (e.type == 'mouseleave') {
                            $('.dd-lb-control').fadeOut(ff);
                        }else {
                            $('.dd-lb-control').fadeIn(ff);
                        }
                    });

                });





                // Au CLICK
                $lb.bind('click', function(e){
                    
                    e.preventDefault();
                    
                    // On obtient l'action souhaitée.
                    var action = $(e.target).data('action');
                    
                    // On agit selon l'action souhaitée.
                    switch (action) {
                    case 'p' : // 'prev'
                        that.index = that.index-1;
                    	that.fullsize = that.prev.attr('href');
                    	break;
                    case 'n' : // 'next'
                        that.index = that.index+1;
                    	that.fullsize = that.next.attr('href');
                    	break;
                    case 'c' : // 'close'
                    	$lb.fadeOut(sf, function(){
                    	        BASE.controls.remove();
                    	            $(this).remove();
                    	    });
                    default :
                    	break;
                    }
                    
                    $lb.image
                        .fadeOut(sf, function(){   
                            // on règle la source de l'image d'après that.fullsize 
                            $lb.image.setSource();
                            BASE.controls.caption.remove();
                            BASE.controls.caption.appendTo($lb.wrapper).text('Chargement en cours...');
                    });
                    
                });
            });
        });
    }
};
})(jQuery);



// Crée un nouvel objet sur la base d'un littéral
// © douglas crockford
Object.create = function(o) {
    function F() {}
    F.prototype = o;
    return new F();
}



