(function ($) {
    $.popup = function (id, options) {
        init(id, options);
    };

    $.fn.popup = function (id, options) {
        init(id, options);
        bind(this, id);
        return this;
    };

    $.popupdisplay = function (id) {
        display_popup(id);
    };

    $.popuphide = function (id) {
        disable_overlay(id);
    };

    $.popupcenter = function (id) {
        center_popup(id);
    };

    var config = {
        speed: 'slow',
        event: 'click',
        effect: true,
        move: 'default',
        close: true,
        center: true
    };

    init = function (id, options) {
        if (options) {
            jQuery.extend(config, options);
        }

        $("#" + id).hide();
        $("#" + id + "_overlay").hide();

        $("#" + id + "_overlay").bind('click', function () {
            disable_overlay(id);
            return false;
        });

        if (config.close) {
            $("#" + id + "_close").bind('click', function () {
                disable_overlay(id);
                return false;
            });
        }

        $(window).scroll(function () {
            resizeheights(id);
        });

        resizeheights(id);
    };

    bind = function (link, id) {
        $(link).bind(config.event, function () {
            display_popup(id);
        });
    };

    // functions
    brwstester = function () {
        return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
    };

    resizeheights = function (id) {
        $("#" + id + "_overlay").css({ 'height': $(window).height() + brwstester().scrollTop + "px" });
    };

    display_overlay = function (id) {
        if (config.effect) {
            $("#" + id + "_overlay").show().fadeTo(config.speed, 0.33);
        }
        else {
            $("#" + id + "_overlay").css({ opacity: '0.33' }).show();
        }
    };

    disable_overlay = function (id) {
        $("#" + id).hide();
        if (config.effect) {
            //$("#" + id + "_overlay").fadeOut(config.speed).fadeTo("", 100);
            $("#" + id + "_overlay").hide();
        }
        else {
            $("#" + id + "_overlay").hide();
        }
    };

    center_popup = function (id) {
        var scwidth = $(window).width();
        var scheight = $(window).height();
        var tblwidth = $("#" + id).outerWidth();
        var tblheight = $("#" + id).outerHeight();

        var D_width = (scwidth - tblwidth);
        if (D_width > 0) {
            D_width /= 2;
        }
        else if (D_width < 0) {
            D_width *= -1;
            D_width /= 2;
        }
        $("#" + id).css('left', D_width + 'px');

        var D_height = (scheight - tblheight);
        if (D_height > 0) {
            config.center = true;
            D_height /= 2;
        }
        else if (D_height < 0) {
            config.center = false;
            D_height = 0;
        }
        $("#" + id).css('top', brwstester().scrollTop + D_height + 'px');
    };

    display_popup = function (id) {
        display_overlay(id);
        center_popup(id);
        $(window).scroll(function () {
            if (config.center) {
                center_popup(id);
            }
        });

        if (config.move == 'down') {
            //move from up
            var top = document.getElementById(id).style.top;
            $("#" + id).css({ 'top': (brwstester().scrollTop - 100) + 'px' }).show().animate({ "top": top }, config.speed);
        }
        else if (config.move == 'up') {
            //move from down
            var top = document.getElementById(id).style.top;
            $("#" + id).css({ 'top': (brwstester().scrollTop + (screen.height)) + 'px' }).show().animate({ "top": top }, config.speed);
        }
        else if (config.move == 'right') {
            //move from left
            var left = document.getElementById(id).style.left;
            $("#" + id).css({ 'width': $("#" + id).width(), 'left': screen.width + 'px' }).show().animate({ "left": left }, config.speed);
        }
        else if (config.move == 'left') {
            //move from right
            var left = document.getElementById(id).style.left;
            $("#" + id).css({ 'width': $("#" + id).width(), 'left': '-' + $("#" + id).width() + 'px' }).show().animate({ "left": left }, config.speed);
        }
        else if (config.move == 'slidedown') {

            $("#" + id).slideDown(config.speed);
        }
        else {
            //default
            if (config.effect) {
                $("#" + id).fadeIn(config.speed);
            }
            else {
                $("#" + id).show();
            }
        }
    };
})(jQuery);
