// JavaScript Document
(function ($) {

    $.fn.TopFrameLoad = function (options) {
        var defaults = {
            link: "",
            larghezza: 600,
            altezza: "60%",
            titolo: "Chiudi",
            timer: 0
        };
        var options = $.extend(defaults, options);
        Create(options);
        Center();
        Open(options.link, options.timer);

        $("#topFrameTitle").click(function () {
            Close();
        });
        $("#topFrameFullBox").click(function () {
            Close();
        });
        $(window).resize(function () {
            Center();
            SetAltezza();
        });
    }

    //--------------------------------------------------------

    $.fn.TopFrameClick = function (options) {
        var defaults = {
            larghezza: 600,
            altezza: "60%",
            titolo: "Chiudi",
            timer: 0
        };
        var options = $.extend(defaults, options);
        this.each(function () {
            var obj = $(this);
            obj.click(function (event) {
                event.preventDefault();
                if (obj.attr("rel") != "") {
                    var misure = obj.attr("rel").split("x");
                    options.larghezza = misure[0];
                    options.altezza = misure[1];
                }
                if (obj.attr("title") != "") {
                    options.titolo = obj.attr("title");
                }
                Create(options);
                Center();
                Open(obj.attr("href"), 0);
                $("#topFrameTitle img").click(function () {
                    Close();
                });
                $("#topFrameFullBox").click(function () {
                    Close();
                });
                $(window).resize(function () {
                    Center();
                    SetAltezza();
                });
            });
        });
    }

    //--------------------------------------------------------

    function SetAltezza() {
        $("#topFrameCorpo").css({ "height": $("#topFrameCornice").height() - ($("#topFrameTitle").height() + 4) + "px" });
        $("#topFrameContent").css({ "height": $("#topFrameCornice").height() - ($("#topFrameTitle").height() + 4) + "px" });
    }

    //--------------------------------------------------------

    function Create(options) {
        var imgClose = $("<img />").attr("src", "image_sito/topFrame/close.png").attr("id", "imgTopframeClose");
        var divBoxCornice = $("<div></div>").attr("id", "topFrameCornice").prependTo($("body"));
        divBoxCornice.width(options.larghezza);
        divBoxCornice.height(options.altezza);
        divBoxCornice.css({ "margin-top": "-" + $("#topFrameCornice").height() + "px" });
        var divBoxCorpo = $("<div></div>").attr("id", "topFrameCorpo").prependTo(divBoxCornice);
        var divTitle = $("<div></div>").attr("id", "topFrameTitle").text(options.titolo).appendTo(divBoxCornice);
        if (options.timer != 0) {
            options.titolo = options.titolo + " | Chiusura automatica tra: ";
            var tempo = options.timer / 1000;
            var contoAllaRovescia = setInterval(function () {
                if (tempo >= 0) {
                    divTitle.text(options.titolo + tempo + " secondi |");
                    imgClose.appendTo(divTitle);
                    tempo = tempo - 1;
                }
                if (tempo < 0) { clearInterval(contoAllaRovescia); }
            }, 1000);
        }
        imgClose.appendTo(divTitle);
        SetAltezza();
        $("<iframe></iframe>").attr("frameborder", "0").attr("id", "topFrameContent").prependTo(divBoxCorpo);
        $("<div></div>").attr("id", "topFrameFullBox").prependTo($("body"));
        $("<div></div>").attr("id", "topFrameOmbraBox").prependTo($("body"));
    }

    //--------------------------------------------------------

    function Center() {
        $("#topFrameCornice").css({ "margin-left": "-" + ($("#topFrameCornice").width() / 2) + "px" });
    }

    //--------------------------------------------------------

    function Open(link, timer) {
        $("#topFrameOmbraBox").fadeIn(1000);
        $("#topFrameFullBox").fadeIn(1000, function () {
            $("#topFrameCornice").animate({ "margin-top": 30 + "px" }, 500);
            $("#topFrameCornice").animate({ "margin-top": 0 + "px" }, 300, function () {
                $("#topFrameContent").attr("src", link);
                if (timer != 0) {
                    var t = setInterval(function () {
                        Close();
                        clearInterval(t);
                    }, timer);
                }
            });
        });
    }

    //--------------------------------------------------------

    function Close() {
        $("#topFrameCornice").animate({ "margin-top": 30 + "px" }, 700);
        $("#topFrameCornice").animate({ "margin-top": "-" + $("#topFrameCornice").height() + "px" }, 500, function () {
            $("#topFrameCornice").remove();
        });
        $("#topFrameFullBox").fadeOut("slow", function () {
            $("#topFrameFullBox").remove();
            $("#topFrameOmbraBox").fadeOut(1000, function () {
                $("#topFrameOmbraBox").remove();
            });

        });
    }
}
)(jQuery);
