(function(f) {
    f.ui = f.ui || {};
    f.extend(f.ui, {
        plugin: {
            add: function(a, b, c) {
                var d = f.ui[a].prototype;
                for (var i in c) {
                    d.plugins[i] = d.plugins[i] || [];
                    d.plugins[i].push([b, c[i]])
                }
            },
            call: function(a, b, c) {
                var d = a.plugins[b];
                if (!d) return;
                for (var i = 0; i < d.length; i++) {
                    if (a.options[d[i][0]]) d[i][1].apply(a.element, c)
                }
            }
        },
        cssCache: {},
        css: function(a) {
            if (f.ui.cssCache[a]) return f.ui.cssCache[a];
            var b = f('<div class="ui-resizable-gen">').addClass(a).css({
                position: 'absolute',
                top: '-5000px',
                left: '-5000px',
                display: 'block'
            }).appendTo('body');
            f.ui.cssCache[a] = !!((!/auto|default/.test(b.css('cursor')) || (/^[1-9]/).test(b.css('height')) || (/^[1-9]/).test(b.css('width')) || !(/none/).test(b.css('backgroundImage')) || !(/transparent|rgba\(0, 0, 0, 0\)/).test(b.css('backgroundColor'))));
            try {
                f('body').get(0).removeChild(b.get(0))
            } catch(e) {}
            return f.ui.cssCache[a]
        },
        disableSelection: function(e) {
            e.unselectable = "on";
            e.onselectstart = function() {
                return false
            };
            if (e.style) e.style.MozUserSelect = "none"
        },
        enableSelection: function(e) {
            e.unselectable = "off";
            e.onselectstart = function() {
                return true
            };
            if (e.style) e.style.MozUserSelect = ""
        },
        hasScroll: function(e, a) {
            var b = /top/.test(a || "top") ? 'scrollTop': 'scrollLeft',
            has = false;
            if (e[b] > 0) return true;
            e[b] = 1;
            has = e[b] > 0 ? true: false;
            e[b] = 0;
            return has
        }
    });
    f.each(['Left', 'Top'], function(i, a) {
        if (!f.fn['scroll' + a]) f.fn['scroll' + a] = function(v) {
            return v != undefined ? this.each(function() {
                this == window || this == document ? window.scrollTo(a == 'Left' ? v: f(window)['scrollLeft'](), a == 'Top' ? v: f(window)['scrollTop']()) : this['scroll' + a] = v
            }) : this[0] == window || this[0] == document ? self[(a == 'Left' ? 'pageXOffset': 'pageYOffset')] || f.boxModel && document.documentElement['scroll' + a] || document.body['scroll' + a] : this[0]['scroll' + a]
        }
    });
    var g = f.fn.remove;
    f.fn.extend({
        position: function() {
            var a = this.offset();
            var b = this.offsetParent();
            var c = b.offset();
            return {
                top: a.top - num(this[0], 'marginTop') - c.top - num(b, 'borderTopWidth'),
                left: a.left - num(this[0], 'marginLeft') - c.left - num(b, 'borderLeftWidth')
            }
        },
        offsetParent: function() {
            var a = this[0].offsetParent;
            while (a && (!/^body|html$/i.test(a.tagName) && f.css(a, 'position') == 'static')) a = a.offsetParent;
            return f(a)
        },
        mouseInteraction: function(o) {
            return this.each(function() {
                new f.ui.mouseInteraction(this, o)
            })
        },
        removeMouseInteraction: function(o) {
            return this.each(function() {
                if (f.data(this, "ui-mouse")) f.data(this, "ui-mouse").destroy()
            })
        },
        remove: function() {
            f("*", this).add(this).trigger("remove");
            return g.apply(this, arguments)
        }
    });
    function num(a, b) {
        return parseInt(f.curCSS(a.jquery ? a[0] : a, b, true)) || 0
    };
    f.ui.mouseInteraction = function(a, b) {
        var c = this;
        this.element = a;
        f.data(this.element, "ui-mouse", this);
        this.options = f.extend({},
        b);
        f(a).bind('mousedown.draggable', function() {
            return c.click.apply(c, arguments)
        });
        if (f.browser.msie) f(a).attr('unselectable', 'on');
        f(a).mouseup(function() {
            if (c.timer) clearInterval(c.timer)
        })
    };
    f.extend(f.ui.mouseInteraction.prototype, {
        destroy: function() {
            f(this.element).unbind('mousedown.draggable')
        },
        trigger: function() {
            return this.click.apply(this, arguments)
        },
        click: function(e) {
            if (e.which != 1 || f.inArray(e.target.nodeName.toLowerCase(), this.options.dragPrevention || []) != -1 || (this.options.condition && !this.options.condition.apply(this.options.executor || this, [e, this.element]))) return true;
            var a = this;
            var b = function() {
                a._MP = {
                    left: e.pageX,
                    top: e.pageY
                };
                f(document).bind('mouseup.draggable', function() {
                    return a.stop.apply(a, arguments)
                });
                f(document).bind('mousemove.draggable', function() {
                    return a.drag.apply(a, arguments)
                });
                if (!a.initalized && Math.abs(a._MP.left - e.pageX) >= a.options.distance || Math.abs(a._MP.top - e.pageY) >= a.options.distance) {
                    if (a.options.start) a.options.start.call(a.options.executor || a, e, a.element);
                    if (a.options.drag) a.options.drag.call(a.options.executor || a, e, this.element);
                    a.initialized = true
                }
            };
            if (this.options.delay) {
                if (this.timer) clearInterval(this.timer);
                this.timer = setTimeout(b, this.options.delay)
            } else {
                b()
            }
            return false
        },
        stop: function(e) {
            var o = this.options;
            if (!this.initialized) return f(document).unbind('mouseup.draggable').unbind('mousemove.draggable');
            if (this.options.stop) this.options.stop.call(this.options.executor || this, e, this.element);
            f(document).unbind('mouseup.draggable').unbind('mousemove.draggable');
            this.initialized = false;
            return false
        },
        drag: function(e) {
            var o = this.options;
            if (f.browser.msie && !e.button) return this.stop.apply(this, [e]);
            if (!this.initialized && (Math.abs(this._MP.left - e.pageX) >= o.distance || Math.abs(this._MP.top - e.pageY) >= o.distance)) {
                if (this.options.start) this.options.start.call(this.options.executor || this, e, this.element);
                this.initialized = true
            } else {
                if (!this.initialized) return false
            }
            if (o.drag) o.drag.call(this.options.executor || this, e, this.element);
            return false
        }
    })
})(jq2j);
