﻿// This code block extends the uiDialog widget by adding a new boolean option 'sticky' which,
// by default, is set to false. When set to true on a dialog instance, it will keep the dialog's
// position 'anchored' regardless of window scrolling.

/****************** Start of uiDialog widget extension ****************/

(function($) {
    var _init = $.ui.dialog.prototype._init;
    $.ui.dialog.prototype._init = function() {
        var self = this;
        _init.apply(this, arguments);
        if (self.options.sticky) {
            self.uiDialog.css('position', 'fixed');
        };
        this.uiDialog
            .bind('dragstop', function(event, ui) {
                if (self.options.sticky)
                    storeLocation(ui.position.left,
                                  ui.position.top);
            })
            .bind('resizestart', function(event, ui) {
                if (self.options.sticky)
                    storeLocation(ui.position.left - $(window).scrollLeft(),
                                  ui.position.top - $(window).scrollTop());
            })
            .bind('resizestop', function(event, ui) {
                if (self.options.sticky) {
                    self.uiDialog.css({
                        'position': 'fixed',
                        'left': self.location.left,
                        'top': self.location.top
                    });
                };
            });
        function storeLocation(_left, _top) {
            self.location = { left: _left, top: _top };
        };
    };
    $.ui.dialog.defaults.sticky = true;
})(jQuery);
/***************** End of uiDialog widget extension *********************/