jQuery.fn.popin = function(o) {
    
    var settings = jQuery.extend({
        loaderImg: "",
        opacity: .5
    }, o);

    // Action ouverture
    jQuery(this).each(function() {
        jQuery(this).click(function() {
            if (window.PPHhere == 1) {
                return false;
            }
            window.PPHhere = 1;
            if ($(this).hasClass("small")) {
                PPNopen($(this).attr("href"), "");
            } else {
                PPNopen($(this).attr("href"), "smallpopin");
            }
            return false;
        });
    });
    
    // Popin Ouverture
    var Loader = new Image();
    Loader.src = settings.loaderImg;
    
    ie6 = ($.browser.msie && ($.browser.version == "6.0")) ? true : false

    function PPNopen(url, classname) { 
    
        // Insertion du voile & Verrouillage du scroll  
        $("body").prepend('<div class="popin-voile"></div>');
        
        // Patch IE6
        if(ie6 == true) {
            
            PPNhtmlScroll           = document.getElementsByTagName("html")[0].scrollTop;
            var PPNbodyMargin       = new Object();
            PPNbodyMargin.top       = parseInt($("body").css("margin-top"));
            PPNbodyMargin.right     = parseInt($("body").css("margin-right"));
            PPNbodyMargin.bottom    = parseInt($("body").css("margin-bottom"));
            PPNbodyMargin.left      = parseInt($("body").css("margin-left"));
            
            PPNbodyHeight = parseInt($("body").height());
            
            PPNbodyTop = ((PPNbodyMargin.top + PPNbodyMargin.bottom) < PPNhtmlScroll) ? (PPNbodyMargin.top + PPNbodyMargin.bottom - PPNhtmlScroll) : 0;
            $("body").css("top", PPNbodyTop );      
            $(".popin-voile").css("top", -(PPNbodyMargin.top + PPNbodyMargin.bottom - PPNhtmlScroll) - 4);
        }
        
        // Affichage du voile
        $(".popin-voile").css("opacity", settings.opacity);
        $(".popin-voile").animate({height:((ie6 == true) ? (PPNbodyHeight + PPNbodyMargin.top + PPNbodyMargin.bottom) : "100%")}, 500, function() {
            
            // Loader
            $(".popin-voile").css("background-image", "url('"+settings.loaderImg+"')");
            
            // Insertion de la popin et animation
            $(".popin").css("height", $("body").height() );
                            
            // Requ?te
            $.ajax({
                type: "GET",
                url: url,
                dataType: "html",
                success: function(m){
    
                    // Cr?ation de la popin
                    $("body").prepend('<div class="popin popin-content '+classname+'"><div class="popin-content"></div></div>');
                    
                    // Chargement du contenu
                    $(".popin-content").html(m);
    
                },
                complete: function(){
                    
                    // Loader
                    $(".popin-voile").css("background-image", "");
                    
                    // Affichage
                    if(ie6 == true) {
                        $(".popin").css("top", parseInt($(".popin").css("top")) - PPNbodyTop );
                    }
                    $(".popin").fadeIn("slow");
                    
                    if (typeof(activeteForm) == 'function') { activeteForm(); }
                    
                    // Action fermeture
                    $("a.popin-close").click(function() {
                        PPNclose();
                        return false;
                    });
                }
            });
    
            
        });
            
        $("html").keydown(function(e){
            if(e.keyCode == '27') {
                PPNclose();
            }
        });
    
    }
    
    // Popin fermeture
    function PPNclose() {
        $("html").unbind("keydown");
        
        $(".popin").fadeOut("slow", function() {
        
            $(".popin-voile").animate({opacity:0,height:0}, 500, function() {

                $(".popin, .popin-voile").remove();
                window.PPHhere = 0;

            });
        });
    }

};