$(function(){

    // Pour supprimer les overlays
    $('body').live('click', function(e){
        if ( !$(e.target).closest('.overlay').length){
            $('.overlay').each(function(){
                if ($(this).data('visible')) {
                    // Tous les overlays "fermables
                    // exposent une méthode "close".
                    this.close();   
                }
            });
        }
    });


    $(window).load(function(){
        $(window).resize();
        
        $('#accueil-news').find('.article p:not(:first)')
         .each(function(){
            var $this = $(this),
                $title = $this.closest('.article').find('h4'),
                text = $this.text();
            if (text.length > 80){
                $this.data({
                    'originalText'  : text,
                    'originalButton': 'Masquer',
                    'shortText'     : text.substr(0,45) + ' (…)',
                    'shortButton'   : 'Afficher…'
                });
                $title.append('<button class="small-button">' + $this.data('shortButton') + '</button>');
                // Garder la même taille de bouton même après changement du texte du bouton
                $title.find('button').css('width', $title.find('button').innerWidth());
                $this.html($this.data('shortText'));
            }
         });
                
        // quickfix dégueu mais guère problématique
        // Il arrive que le resize ne se fasse pas si le contenu
        // est en cache.
        setTimeout(function(){ $(window).resize(); }, 500);

    });
    
    $('#accueil-news').find('button')
     .live('click', function(){
        var $this = $(this),
            $paragraph = $this.closest('.article').find('p');

        if ($paragraph.hasClass('open')) {
            $paragraph.html($paragraph.data('shortText'));
            $this.text($paragraph.data('shortButton'));
        }else {
            $paragraph.html($paragraph.data('originalText'));
            $this.text($paragraph.data('originalButton'));
        }
        
        $paragraph.toggleClass('open');
        $(window).resize();
    });
    
    
    $('a[rel=popup]')
     .live('click', function(e){
     
        e.preventDefault();
        
        var $popup = $('#popup-wrapper'),
            $this  = $(this),
            popupHeight = $popup.outerHeight();
        
        $popup[0].close = function(){
            $popup.animate({top: -popupHeight-1}, 200, function(){
                $popup
                 .removeClass('open')
                 .data('visible', false)
                 .find('.current')
                  .removeClass('current');
            });
        }
        
        $popup[0].open = function(){
            $popup
             .addClass('current')
             .animate({top: -1}, 200, function(){
                $popup
                 .addClass('open')
                 .data('visible', true);
            });
        }
        
        if (($popup.hasClass('open') && !$this.hasClass('current')) || !$popup.hasClass('open')) {
            $popup[0].open();
        }else {
            $popup[0].close();
        }
        
        $popup.find('.current').removeClass('current');
        $this.toggleClass('current');
        
        $popup
         .find('.popup')
          .hide(0)
         .end()
         .find('#' + $this.data('hashtarget'))
          .show(0);
        return false;
     });

    $(window).resize($.throttle(250, function(e){
    
 /*       if ($(e.target).is($(window))){
            // Redimensionner les zones de contenu
            // pour remplir la fenêtre.
            var largest = $(window).height(),
                $section = $('.section').not('#tempLoader .section'),
                offset  = $section.offset().top;
                
            $section.each(function(){
                $(this).css('height', 'auto');
                largest = $(this).outerHeight()+offset > largest ? $(this).outerHeight()+offset : largest;
            });
                
            $section.height(largest - offset - 48);
        }*/

        $('.alert').css('top', ($(window).height()/2 - $('.alert').outerHeight()/2) );
    }));

    // ------------------------------------------------------------
    // !Gestion des messages d'alerte générés par PHP
    // ------------------------------------------------------------

    $('.alert').each(function(){
        this.close = function(){
            $(this).fadeOut(100, function(){
                $(this).remove()
            });
        }
    });
    
    $('.alert button').live('click', function(e){
        e.preventDefault();
        $(this)
            .closest('.alert')
            .close();
    });
    
    $('#contact-form').validate();
});
