(function ($) {
    $.fn.ivmodal = function (options) {
        var defaults = { event: "click", classname: "modal", callback: "", animation: "fade", speed: 500, coverspeed: 500, coverfade: .5 };
        var options = $.extend(defaults, options);
        var event = options.event;
        this.each(function () {
            $(this).bind(event, function (e) {
                called = $(this);
                e.preventDefault();
                var a = [options.classname, options.callback, options.animation, options.speed, options.coverspeed, options.coverfade];
                var ivm = "." + a[0];
                if ($(ivm).length == 0) {
                    $('<div class="' + a[0] + '-cover"></div><div class="' + a[0] + '-span"><span></span><div class="' + a[0] + '"><div class="' + a[0] + '-close">X</div></div></div>').appendTo("body");
                    $("." + a[0] + "-cover").css("opacity", 0).fadeTo(a[4], a[5]);
                    $("." + a[0] + "-close").click(function () {
                        $("." + a[0] + "-cover").stop().fadeTo(a[4], 0, function () {
                            $(this).remove();
                        })
                        $("." + a[0] + "-span").empty().remove();
                        clearInterval(ietimer);
                    });
                    if (jQuery.browser.msie) {
                        if (parseInt(jQuery.browser.version) <= 7) {
                            var ietimer = setInterval(iePosition, 200);
                        };
                    }
                    if (a[2] == "zoom") {
                        var store = [$(ivm).height(), $(ivm).width()];
                        $(ivm).css({ "height": $(this).height() + "px", "width": $(this).width() + "px", "left": 0 - ($(window).width() / 2 - $(this).offset().left) + ($(this).width() / 2) + "px", "top": 0 - ($(window).height() / 2 - $(this).offset().top) + ($(this).height() / 2) + "px" }).animate({ "height": store[0] + "px", "width": store[1] + "px", "left": "0px", "top": "0px" }, a[3], theend);
                    } else if (a[2] != "fade") {
                        var pos = [a[2].split(" ")[1] * ($(window).width() / 2 + $(ivm).width()), a[2].split(" ")[0] * ($(window).height() / 2 + $(ivm).height())];
                        $(ivm).css({ "left": pos[0], "top": pos[1] }).animate({ "left": "0px", "top": "0px" }, a[3], theend);
                    } else if (a[3] > 0) {
                        $(ivm).css("opacity", 0).fadeTo(a[3], 1, theend);
                    } else {
                        theend();
                    }
                } else {
                    theend();
                }
                function iePosition() {
                    $(ivm).css({ "margin-top": ($(window).height() - $(ivm).height()) / 2, "margin-left": ($(window).width() - $(ivm).width()) / 2, "position": "absolute" });
                }
                function theend() {
                    if (typeof a[1] == 'function') {
                        a[1]($(ivm), called);
                    }
                }
            });
        });
        return ($(this));
    };
})(jQuery);
