﻿; (function($) {
    $.fn.popup = function(options) {
        var options = $.extend({
            duration: 50
        }, options);

        if ($('#popup-overlay').size() < 1) {
            $('<div id="popup-overlay" />').appendTo('body');
        }

        if ($('#popup-container').size() < 1) {
            $('<div id="popup-container"><span class="close" /><div class="content" /></div>').appendTo('body');
        }

        $('#popup-container').bind('close', function() {
            $('#popup-container').fadeOut(options.duration, function() {
                $('#popup-overlay:eq(0)').fadeOut(options.duration, function() {
                    $('#popup-container > div.content:eq(0)').empty();
                    if ($.browser.msie && $.browser.version < 7.0) {
                        $('select').css('visibility', 'visible');
                    }
                });
            });
        });

        $('#popup-container').bind('reposition', function() {
            if (options.top) {
                $(this).css("top", options.top).css("margin-top", 0);
            } else {
                var offsetScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
                $(this).css("top", "50%").css("margin-top", Math.round(offsetScrollTop - Math.min(($(this).height() / 2), ($(window).height() / 2) - 30)));
            }
            if (options.left) {
                $(this).css("left", options.left).css("margin-left", 0);
            } else {
                var offsetScrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
                $(this).css("left", "50%").css("margin-left", Math.round(offsetScrollLeft - Math.min(($(this).width() / 2), ($(window).width() / 2) - 30)));
            }
        });

        $('#popup-container').bind('repopulate', function(event, content) {
            $("#popup-container > div.content:eq(0)").html(content);
            if (options.width) $('#popup-container').width(options.width);
            if (options.height) $('#popup-container').height(options.height);
            $('#popup-container').trigger("reposition");
            $('#popup-container').fadeIn(options.duration);
        });

        $('#popup-container').bind('callback', function(event, data) {
            if (typeof options.callback == "function") {
                options.callback(data);
            }
        });

        $('#popup-container > span.close').bind('click', function() {
            $('#popup-container').trigger('close');
        });

        return $(this).click(function(e) {
            var href = $(this).attr('href');
            if ($.browser.msie && $.browser.version < 7.0) {
                $('select').css('visibility', 'hidden');
            }

            $('#popup-overlay').css({ 'opacity': 0, 'display': 'block' }).fadeTo(options.duration, 0.8, function() {
                if (options.content) {
                    $('#popup-container').trigger('repopulate', options.content);
                } else if (options.image) {
                    $('#popup-container').trigger('repopulate', '<img src="' + href + '" alt="" />');
                    $('#popup-container > div.content:eq(0) > img').bind('click', function() {
                        $('#popup-container').trigger('close');
                    });
                } else {
                    $.ajax({
                        url: options.url ? options.url : href,
                        cache: options.cache ? options.cache : false,
                        success: function(data) {
                            if (data.indexOf('401') == 0) {
                                window.location.reload();
                            } else {
                                $('#popup-container').trigger('repopulate', data);
                            }
                        },
                        error: function(request, textStatus, errorThrown) {
                            $('#popup-container').trigger('repopulate', request.status + ' ' + request.statusText + '<br />' + request.responseText);
                        }
                    });
                }
            });
            return false;
        });
    };
})(jQuery);
