﻿(function ($) {

    $.fn.lightbox = function (options) {

        var defaults = {};
        var options = $.extend(defaults, options);

        return this.each(function () {

            var $contentToLightbox = $(this);

            $contentToLightbox.before("<div class='lightbox-content-placeholder' />");
            $contentToLightbox[0].contentPlaceholder = $contentToLightbox.prev(".lightbox-content-placeholder")[0];

            var $lb = $("#lightbox");
            var $body = $("body");
            var $lbContent = $(">.lightbox-content", $lb);
            var $lbContentContainer = $(">.lightbox-content-container", $lbContent);
            var $lbOverlay = $(">.lightbox-overlay", $lb);

            var lightboxCenterContent = function ($elm) {

                $lbContent.css({
                    top: Math.round(($(window).height() - $lbContent.height()) / 2) + $(window).scrollTop(),
                    left: Math.round(($(window).width() - $lbContent.width()) / 2) + $(window).scrollLeft()
                });

            };

            var lightboxClose = function () {

                $lbContent.stop().animate({

                    opacity: 0

                }, {

                    duration: 300,
                    complete: function () {

                        $lbOverlay.stop().animate({

                            opacity: 0

                        }, {

                            duration: 300,
                            complete: function () {

                                $lb.hide();
                                $($contentToLightbox[0].contentPlaceholder).after($contentToLightbox);
                                $($contentToLightbox[0].contentPlaceholder).remove();

                            }

                        });

                    }

                });

            };

            var lightboxSize = function () {

                $lb.css({

                    width: $body.outerWidth(true),
                    height: $body.outerHeight(true)

                });

            };

            var lightboxShow = function () {

                $lbContentContainer.empty().append($contentToLightbox);
                $contentToLightbox.show();

                lightboxSize();

                $lb.show();

                lightboxCenterContent();

                $lbOverlay.stop().animate({

                    opacity: .75

                }, {

                    duration: 300,
                    complete: function () {

                        $lbContent.stop().animate({

                            opacity: 1

                        }, {

                            duration: 300,
                            complete: function () {

                                if (parseFloat($lbContent.css("opacity")) == 1)
                                    $lbContent.css("opacity", "");

                            }

                        });

                    }

                });


            };

            if (!$lb.length) {

                $body.append("<div id='lightbox'><div class='lightbox-content'><a href='javascript:;' class='close' title='Close'>Close</a><div class='lightbox-content-container'/></div><div class='lightbox-overlay' /></div>");
                $lb = $("#lightbox");

                $lb.hide();

                $lbContent = $(">.lightbox-content", $lb);
                $lbContentContainer = $(">.lightbox-content-container", $lbContent);
                $lbOverlay = $(">.lightbox-overlay", $lb);

                $(window).scroll(function () {

                    if ($lb.is(":visible"))
                        lightboxCenterContent();

                }).resize(function () {

                    if ($lb.is(":visible")) {

                        lightboxSize();
                        lightboxCenterContent();

                    }

                }).keyup(function (evt) {

                    if ($lb.is(":visible")) {

                        if (evt.which == 27)
                            lightboxClose();

                    }

                });

                $(">.close", $lbContent).click(function () {

                    lightboxClose();

                    return false;

                });

                $lbContent.css("opacity", 0);
                $lbOverlay.css("opacity", 0)

            }

            lightboxShow();

        });

    };

})(jQuery);
