
Element.implement({

    hide: function() {
        var d;
        try {
            //IE fails here if the element is not in the dom
            d = this.getStyle('display');
        } catch(e) {}
        if (d == 'none') return this;
        return this.store('element:_originalDisplay', d || '').setStyle('display', 'none');
    },
    show: function(display) {
        if (!display && this.isDisplay()) return this;
        this.fireEvent('onshow', this);
        display = display || this.retrieve('element:_originalDisplay') || '';
        return this.setStyle('display', (display == 'none') ? 'block': display);
    },
    isDisplay: function() {
        return !(this.getStyle('display') == 'none' || (this.offsetWidth + this.offsetHeight) === 0);
    },
    //获取padding,margin,border值
    getPatch: function() {
        var args = arguments.length ? Array.from(arguments) : ['margin', 'padding', 'border'];
        var _return = {
            x: 0,
            y: 0
        };

        Object.each({x: ['left', 'right'], y: ['top', 'bottom']}, function(p2, p1) {
            p2.each(function(p) {
                try {
                    args.each(function(arg) {
                        arg += '-' + p;
                        if (arg == 'border') arg += '-width';
                        _return[p1] += this.getStyle(arg).toInt() || 0;
                    }, this);
                } catch(e) {}
            }, this);
        }, this);
        return _return;
    },
    // the elements outer size
    outerSize: function() {
        if (this.getStyle('display') === 'none') return {x: 0, y: 0};
        return {
            x: (this.getStyle('width').toInt() || 0) + this.getPatch().x,
            y: (this.getStyle('height').toInt() || 0) + this.getPatch().y
        };
    },
	toSize: function(XY, val) {
		if (!XY) return {
			height:this.toSize("height"),
			width:this.toSize("width"),
			x:this.toSize('width'),
			y:this.toSize('height')
		};
		if (typeOf(XY) == "object") return this.setStyles(XY);
		if (val) return this.setStyle(XY, val);
		var getXY = function(){
			return this.getStyle(XY).toInt() || 0;
		}.bind(this);
		return this.measure(getXY);
	},
	//mootools more
	measure: function(fn){
		var visibility = function(el){
			return !!(!el || el.offsetHeight || el.offsetWidth);
		};
		if (visibility(this)) return fn.apply(this);
		var parent = this.getParent(),
			toMeasure = [];
		while (!visibility(parent) && parent != document.body){
			toMeasure.push(parent.expose());
			parent = parent.getParent();
		}
		var restore = this.expose();
		var result = fn.apply(this);
		restore();
		toMeasure.each(function(restore){
			restore();
		});
		return result;
	},
	expose: function(){
		if (this.getStyle('display') != 'none') return function(){};
		var before = this.style.cssText;
		this.setStyles({
			display: 'block',
			position: 'absolute',
			visibility: 'hidden'
		});
		return function(){
			this.style.cssText = before;
		}.bind(this);
	}
});

(function() {
    Element.implement({
        position: function(options) {
            options = Object.merge({
                target: document.body,
                to: {x: 'center', y: 'center'}, //定位到目标元素的基点
                base: {x: 'center', y: 'center'}, //此元素定位基点 --为数值时类似offset
                offset: {x: 0, y: 0}, // true 或 to:滑动使this可视。in:把element限制在视窗内
                intoView: false
            }, options);

            this.setStyle('position', 'absolute');

            var el = options.target || $(document.body);
            var base = getOffset(this, options.base);
            var to = getOffset(el, options.to);
            var x = to.x - base.x + el.getPosition().x + el.getScroll().x + options.offset.x;
            var y = to.y - base.y + el.getPosition().y + el.getScroll().y + options.offset.y;

            if (options.intoView === 'in') {
                x = x.limit(0, window.getScroll().x + window.getSize().x - this.getSize().x);
                y = y.limit(0, window.getScroll().y + window.getSize().y - this.getSize().y);
            }

            this.setStyles({
                left: x,
                top: y
            });
            if (options.intoView === true || options.intoView === 'to') try {
                new Fx.Scroll(document).scrollIntoView(this);
            } catch(e) {}
            return this;
        }
    });
    //取得九点定位的坐标
    function getOffset(el, base) {
        var size = el.getSize(), x, y;
        base = base || {
            x: 'center',
            y: 'center'
        };
        switch (base.x.toString().toLowerCase()) {
        case '0':
        case 'left':
            x = 0;
            break;
        case '100%':
        case 'right':
            x = size.x;
            break;
        case '50%':
        case 'center':
            x = size.x / 2;
            break;
        default:
            x = base.x.toInt();
            break;
        }
        switch (base.y.toString().toLowerCase()) {
        case '0':
        case 'top':
            y = 0;
            break;
        case '100%':
        case 'bottom':
            y = size.y;
            break;
        case '50%':
        case 'center':
            y = size.y / 2;
            break;
        default:
            y = base.y.toInt();
            break;
        }

        return {
            x: x,
            y: y
        };
    }
})();

