/* Basic overlay plugin. */
(function ($) {
    var $overlay;
    // replace 'pluginName' with the name of your plugin
    $.fn.adidoPopup = function (options) {
        // plugin default options
        var defaults = {
            close: '.overlayClose',
            modal: false,
            preventClick: true,
            target: '.overlay'
        }

        // extends defaults with options provided
        if (options) { $.extend(defaults, options); }
        options = defaults;

        // iterate over matched elements
        return this.each(function () {
            var $this, $target, $close;

            $this = $(this);
            $target = $(options.target);
            $overlay = $('.overlay');
            $close = $(options.close, $target);
            $close.click(function () { overlayOff(); });

            if ($overlay.length < 1) {
                $('form').append('<div class="overlay"></div>');
                $overlay = $('.overlay');
            }
            $target.appendTo('form');

            $this.click(function (e) {
                if (options.preventClick) { e.preventDefault(); }
                overlayOn(options.target);
                if (!options.modal) { $overlay.click(function () { overlayOff(); }); }
            })
        });

    };

    // public functions definition
    $.fn.adidoPopup.functionName = function (foo) {
        return this;
    };

    // private functions definition
    function overlayOn(t) {
        var dh = $(document).height();
        $overlay.fadeTo(250, 0.5).css({ height: dh }).attr('rel', t);
        $(t).removeClass('hidden').each(function () {
            var h = $(this).outerHeight(),
                wh = $(window).height(),
                os = $(window).scrollTop(),
                t = ((wh - h) / 2) + os;
            $(this).css({ top: t });
        }).css({ 'visibility': 'visible', 'display': 'none' }).fadeIn();
    }

    function overlayOff() {
        var t = $overlay.attr('rel');
        $overlay.fadeOut().unbind('click');
        $(t).fadeOut();
    }

})(jQuery);
