(function ($) {

    $.fn.centerFrame = function (options) {
        var defaults = {
            w: 760,
            h: 200,
            titolo: "",
            timer: 0
        }
        var op = $.extend(defaults, options);
        var link;
        var divBox;
        var iFrame;
        var fullBox;
        var imgClose;

        $(this).each(function () {
            var obj = $(this);
            obj.click(function (event) {
                event.preventDefault();
                link = obj.attr("href");
                openCenterFrame();

                centerCenterFrame();

                fullBox.click(function () {
                    closeCenterFrame(false);
                });
                imgClose.click(function () {
                    closeCenterFrame(false);
                });
                $(window).resize(function () {
                    centerCenterFrame();
                });
                iFrame.load(function () {
                    if (iFrame.contents().find("#close").attr("value") == "close") {
                        closeCenterFrame(true);
                    }
                    if (iFrame.contents().find("#close").attr("value") == "closeNoSubmit") {
                        closeCenterFrame(false);
                    }
                    if (link != "") {
                        var newHeight = iFrame.contents().find("body").outerHeight(true);
                        if (newHeight > $(window).height()) {
                            newHeight = ($(window).height() * 90) / 100;
                        }
                        divBox.animate({ "margin-top": "-" + (newHeight / 2) + "px" }, 500, function () {

                            imgClose.show();
                            var offset = divBox.offset();
                            offset.left += -10;
                            offset.top += -10;
                            imgClose.offset(offset);

                        });
                        iFrame.animate({ "height": newHeight + "px" }, 500);
                        /*
                        * non funziona la lettura della larghezza è_é
                        var newWidth = iFrame.contents().find("body").width();
                        alert(newWidth);
                        if (newWidth > $(window).width()) {
                        newWidth = "80%";
                        }
                        divBox.animate({ "margin-left": "-" + (newWidth / 2) + "px" }, 500);
                        iFrame.animate({ "width": newWidth + "px" }, 500);
                        */
                    }

                });
            });
        });

        function openCenterFrame() {
            divBox = $("<div />").addClass("divCF").prependTo($("body"));
            iFrame = $("<iframe />").addClass("iFrameCF").width(op.w).height(op.h);
            divBox.html(iFrame);
            fullBox = $("<div />").addClass("fullBoxCF").prependTo($("body"));
            imgClose = $("<div />").addClass("imgCloseCF").attr("title", "Close").prependTo($("body"));
            fullBox.fadeIn(500, function () {
                divBox.show(500, function () {
                    iFrame.attr("src", link);
                    //imgClose.show();
                });
            });
        }

        function closeCenterFrame(submit) {
            link = "";
            iFrame.attr("src", link);
            imgClose.hide();
            divBox.fadeOut(500, function () {
                fullBox.fadeOut(500, function () {
                    fullBox.remove();
                    imgClose.remove();
                    divBox.remove();
                    if (submit == true) { $("#form1").submit(); }
                    firstOpen = true;
                });
            });
        }

        function centerCenterFrame() {
            var newWidth = op.w;
            if (op.w > $(window).width()) {
                newWidth = ($(window).width() * 80) / 100;
            }
            iFrame.css("width", newWidth + "px");

            var newHeight = iFrame.outerHeight(true);
            if (op.h > $(window).height()) {
                newHeight = ($(window).height() * 90) / 100;
            }
            iFrame.css("height", newHeight + "px");

            var newHeight = iFrame.contents().find("body").outerHeight(true);
            if (newHeight > $(window).height()) {
                newHeight = ($(window).height() * 90) / 100;
            }
            divBox.css({ "margin-top": "-" + (newHeight / 2) + "px" });
            iFrame.css({ "height": newHeight + "px" });

            divBox.css("margin-top", "-" + (iFrame.outerHeight(true) / 2) + "px");
            divBox.css("margin-left", "-" + (iFrame.outerWidth(true) / 2) + "px");

            var offset = divBox.offset();
            offset.left += -10;
            offset.top += -10;
            imgClose.offset(offset);

        }
    }

})(jQuery);
