var elt = $('#content_flash');
var containerH = $(elt).height();
var containerW = $(elt).width();
var containerTop = $('#container').css('marginTop');

jQuery.fn.popin = function(o) {

    var settings = jQuery.extend({
        width: containerW,
        height: containerH,
        className: "",
        loaderImg: "",
        opacity: .5,
        onStart: null,
        onComplete: null,
        onExit: null
    }, o);

    // Action ouverture
    jQuery(this).each(function() {
        jQuery(this).click(function(e) {
            PPNopen($(this).attr("href"));
            return false;
        });
    });

    // Popin Ouverture
    var Loader = new Image();
    Loader.src = settings.loaderImg;

    ie6 = ($.browser.msie && ($.browser.version == "6.0")) ? true : false;

    // CSS
    $("body").css("position", "relative");


    function PPNopen(url) {

        if (settings.onStart != null) {
            settings.onStart();
        }

        if (ie6 == true) {
            $("#PPNCSS").remove();
            $("body").append(''
				+ '<style type="text/css" id="PPNCSS">'
				+ '.popin-voile {top:expression(documentElement.scrollTop + body.scrollTop + "px")}'
				+ '.popin {top:expression(documentElement.scrollTop + body.scrollTop + (documentElement.clientHeight/2) - ' + (settings.height / 2) + ' + "px")}'
				+ '</style>'
				+ '');
        }

        // 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"));



            $("html, body").css("height", "100%");
            $("html, body").css("overflow", "hidden");
            $("body").height($("body").height());
            PPNbodyHeight = parseInt($("body").height());
            $("html, body").css("overflow", "visible");
            $("html, body").css("overflow-x", "visible");

            PPNbodyTop = ((PPNbodyMargin.top + PPNbodyMargin.bottom) < PPNhtmlScroll) ? (PPNbodyMargin.top + PPNbodyMargin.bottom - PPNhtmlScroll) : 0;
            $("body").css("top", PPNbodyTop);

        } else {
            //$("html, body").css("overflow", "hidden");
        }


        // Requête
        $.ajax({
            type: "GET",
            url: url,
            dataType: "html",
            success: function(m) {
                // Création de la popin
                $("body").append('<div class="popin ' + settings.className + '"><div class="popin-content"></div></div>');

                // CSS du voile
                $(".popin").css({
                    "position": "absolute",
                    "left": "50%",
                    "z-index": "9500",
                    "width": settings.width,
                    "height": settings.height,
                    "overflow": "hidden",
                    "margin-left": -(495),
                    'top': 0
                });
                $(".popin-content").css({
                    "overflow": "visible",
                    "height": $(".popin").height() - parseInt($(".popin").css("padding-top")) - parseInt($(".popin").css("padding-bottom"))
                });
									
                if (ie6 == true) {
                    $(".popin").css({
                        "position": "absolute",
                        "margin-top": 0
                    });
                }
                else {
                    $(".popin").css("position", "absolute");
                }

                // Chargement du contenu
                $(".popin-content").html(m);

            },
            complete: function() {


                // Affichage
                if (ie6 == true) {
                    $(".popin").css("top", parseInt($(".popin").css("top")) - PPNbodyTop);
                }
                $(".popin").fadeIn("slow", function() {
                    if (settings.onComplete != null) {
                        settings.onComplete();
                    }
                });

                // Action fermeture
                $("#close").click(function() {
                    PPNclose();
                    return false;
                });
            }
        });

    }

    // Popin fermeture
    function PPNclose() {

        $("html").unbind("keydown");

            // Suppression du voile & Déverrouillage du scroll	
            if (ie6 == true) {
                $("html, body").css("height", "auto");
                $("html, body").css("overflow", "auto");
                $("html, body").css("overflow-x", "hidden");
                $("body").css("top", 0);
                window.scrollTo(0, (PPNhtmlScroll));
            } else {
                //$("html, body").css("overflow", "auto");
            }
            $(".popin").remove();

            if (settings.onExit != null) {
                settings.onExit();
            }

    }


};

