var bModal = {

    width: 500,
    height: 500,
    top: 40,
    overlay: null,
    modal: null,
    closeBtn: null,
    frame: null,

    init: function() {

        html = '<div id="b-modal-overlay"></div>';
        html += '<div id="b-modal">';
        html += '<span class="header">';
        html += '<a href="#">Fermer</a>';
        html += '</span>';
        html += '<iframe id="b-modal-frame" name="b-modal-frame" frameborder="0" width="100%" marginheight="10" marginwidth="10" scrolling="auto"></iframe>';
        html += '</div>';

        $("body").prepend(html);

        //initialisation des variables
        this.overlay = $("#b-modal-overlay");
        this.modal = $("#b-modal");
        this.closeBtn = $("#b-modal .header a");
        this.frame = $("#b-modal-frame");

        //initialisation du modal
        this.overlay
            .css("opacity", 0.7)
           /* .bind("click", function() {
                bModal.hide();
            });*/
        this.modal
            .css({
                width: this.width,
                height: this.height,
                top: this.top
            });
        this.closeBtn.bind("click", function() {
            bModal.hide();
            //alert("https://wwww.liferent-pro.com/account/logout.asp");
            //s.modal.find("iframe").attr("src", "https://www.liferent-pro.com/account/logout.asp");
            //return false;
        });

        //Ciblage des liens déclancheurs du modal
        $("a[rel^=bModal]").bind("click", function() {
            url = $(this).attr("href");
            param = $(this).attr("rel").split(" ");
            bModal.show(url, param[1], param[2]);
            return false;
        });
    },

    show: function(url, w, h) {
        s = this;
        this.overlay.css("height", ($(document).height()) + "px").fadeIn("fast", function () {
            s.modal.css("width", ((typeof (w) != 'undefined') ? w : s.width) + "px");
            s.modal.css("height", ((typeof (h) != 'undefined') ? h : s.height) + "px");

            if (document.documentElement.clientWidth < parseInt(s.modal.css("width"))) {
                s.modal.css("width", (document.documentElement.clientWidth - 40) + "px");
            }
            if (document.documentElement.clientHeight < (parseInt(s.modal.css("height")) + s.top)) {
                s.modal.css({
                    top: 10,
                    height: (document.documentElement.clientHeight - 20)
                });
            }

            s.modal.css("margin-left", ((parseInt(s.modal.css("width")) / 2) * -1));
            s.modal.find("iframe").attr("src", url).css("height", (parseInt(s.modal.css("height")) - parseInt(s.modal.find(".header").css("height")) - 2));
            s.modal.fadeIn("fast");
        });
    },

    hide: function() {
        s = this;
        this.modal.fadeOut("fast", function () {
            s.modal.find("iframe").attr("src", "");
            s.overlay.fadeOut("fast");
        });
    }

}

$(function(e) {
    bModal.init();
});
