(function($) {
	$.fn.startWaiter = function() {
           $('<div id="system_waiter_bg"></div>').appendTo(document.body)
                            .addClass('ui-widget-overlay').css({
                                    width:  width(),
                                    height: height(),
                                    'z-index': 2000
                            });
           var divImg = $('<img alt="" id="system_waiter" style="position:absolute;z-index: 2001" src="'+ DOMAIN_PATH +'/res/img/waiter.gif"/>')
                .appendTo(document.body);
              // .appendTo("#system_waiter_bg");
           var pos = position(divImg,{});
           divImg.css(pos);
	}

	$.fn.destroyWaiter = function() {
            $("#system_waiter").remove();
            $("#system_waiter_bg").remove();
	}

	function width() {
            // handle IE 6
            if ($.browser.msie && $.browser.version < 7) {
                var scrollWidth = Math.max(
                    document.documentElement.scrollWidth,
                    document.body.scrollWidth
                );
                var offsetWidth = Math.max(
                    document.documentElement.offsetWidth,
                    document.body.offsetWidth
                );

                if (scrollWidth < offsetWidth) {
                    return $(window).width() + 'px';
                } else {
                    return scrollWidth + 'px';
                }
            // handle "good" browsers
            } else {
                return $(document).width() + 'px';
            }
        }

    function height() {
        // handle IE 6
        if ($.browser.msie && $.browser.version < 7) {
            var scrollHeight = Math.max(
                document.documentElement.scrollHeight,
                document.body.scrollHeight
            );
            var offsetHeight = Math.max(
                document.documentElement.offsetHeight,
                document.body.offsetHeight
            );

            if (scrollHeight < offsetHeight) {
                return $(window).height() + 'px';
            } else {
                return scrollHeight + 'px';
            }
        // handle "good" browsers
        } else {
            return $(document).height() + 'px';
        }
    }

    function position (element,pos) {
        var wnd = $(window), doc = $(document),
            pTop = doc.scrollTop(), pLeft = doc.scrollLeft(),
            minTop = pTop;

        if ($.inArray(pos, ['center','top','right','bottom','left']) >= 0) {
            pos = [
                pos == 'right' || pos == 'left' ? pos : 'center',
                pos == 'top' || pos == 'bottom' ? pos : 'middle'
            ];
        }
        if (pos.constructor != Array) {
            pos = ['center', 'middle'];
        }
        if (pos[0].constructor == Number) {
            pLeft += pos[0];
        } else {
            switch (pos[0]) {
                case 'left':
                    pLeft += 0;
                    break;
                case 'right':
                    pLeft += wnd.width() - element.outerWidth();
                    break;
                default:
                case 'center':
                    pLeft += (wnd.width() - element.outerWidth()) / 2;
            }
        }
        if (pos[1].constructor == Number) {
            pTop += pos[1];
        } else {
            switch (pos[1]) {
                case 'top':
                    pTop += 0;
                    break;
                case 'bottom':
                    pTop += wnd.height() - element.outerHeight();
                    break;
                default:
                case 'middle':
                    pTop += (wnd.height() - element.outerHeight()) / 2;
            }
        }

        // prevent the dialog from being too high (make sure the titlebar
        // is accessible)
        pTop = Math.max(pTop, minTop);
        return {top: pTop, left: pLeft};
    }
 
    $.extend($.ui.waiter, {
        version: "1.0.0",
        defaults: {
            value: 0
        }
    });
})(jQuery);