(function($) {

	var isIE6 = $.browser.msie && $.browser.version == 6.0;
    var position = isIE6 ? 'absolute' : 'fixed';
    var options;
    var overlay = false;

    $.jOverlay = {
        init: function(o){
            // Set Options
            options = $.extend({}, $.jOverlay.options, o);

            // Added overlay
            $('body').prepend("<div id='jOverlay' />");

            // Overlay Style
            $('#jOverlay').css({
                backgroundColor : options.overlayColor,
                position: position,
                top: '0px',
                left: '0px',
                filter : 'alpha(opacity='+ (options.opacity * 100) +')',
                opacity : options.opacity,
                zIndex: options.zIndex,
                width: !isIE6 ? '100%' : $(window).width() + 'px',
                height: !isIE6 ? '100%' : $(document).height() + 'px',
                display: 'none'
            });
        }
    }

	$.fn.jOverlayShow = function() {
        var element = this;
        
        $('#jOverlay').show();
        overlay = true;

        $(element).css({
            position: position,
            zIndex: options.zIndex + 5
        }).show();

        if (options.center) {
            $.center(element);
        }

		if (isIE6) {
			$(window).scroll(function(){
				if (options.center && element.outerHeight()<$(window).height()) {
					$.center(element);
				}
			});

			$(window).resize(function(){

				$('#jOverlay').css({
					width: $(window).width() + 'px',
					height: $(document).height() + 'px'
				});

				if (options.center) {
					$.center(element);
				}
			});
		}

        return false;
	};

    $.fn.jOverlayHide = function()
    {
        $(this.hide());
        $('#jOverlay').hide();
        overlay = false;

        return false;
    }

	$.center = function(element) {
        if (overlay)
        {
            var element = $(element);
            var elemHeight = element.height();
            var elemWidth = element.width();

            element.css({
                width: elemWidth + 'px',
                marginLeft: '-' + (elemWidth / 2) + 'px',
                marginTop: '-' + elemHeight / 2 + 'px',
                //height: 'auto',
                top: !isIE6 ? '50%' : $(window).scrollTop() + ($(window).height() / 2) + "px",
                left: '50%'
            });
            if (!isIE6){
                if (element.height()>$(window).height())
                    $(element).css('position','absolute');
                else
                    $(element).css('position','fixed');
            }
            if (element.height()>$(window).height())
                $(element).css({
                    top:$(window).scrollTop()+'px',
                    marginTop:0
                    });
        }
	};

	$.fn.center = function() {
        if (overlay)
        {
            var element = $(this);
            var elemHeight = element.height();
            var elemWidth = element.width();

            element.css({
                width: elemWidth + 'px',
                marginLeft: '-' + (elemWidth / 2) + 'px',
                marginTop: '-' + elemHeight / 2 + 'px',
                height: 'auto',
                top: !isIE6 ? '50%' : $(window).scrollTop() + ($(window).height() / 2) + "px",
                left: '50%'
            });

            if (!isIE6){
                if (element.height()>$(window).height())
                    $(element).css('position','absolute');
                else
                    $(element).css('position','fixed');
            }
            if (element.height()>$(window).height())
                $(element).css({
                    top:$(window).scrollTop()+'px',
                    marginTop:0
                    });
        }
	};

	// Options default
	$.jOverlay.options = {
		overlayColor : '#000', // Background color for overlay
		opacity : '0.5', // Opacity for overlay
		zIndex : 10, // z-index for overlay
		center : true // If 'true' then center popup
	};
})(jQuery);