/* Author: Lam@2009.09.08 (PHP+JS+CSS)
 * Email:  gzkakaxi@qq.com, liancanwei@gmail.com
 * Frame:  Mootools
 */

// 弹出对话框
var isFirst = true;
function dialog(obj, isHover) {
	var $this = $(obj);
	var oSize, wSize, cssLeft, cssTop;
	var mask, thisFx;
	
	// 默认有覆盖层
	if (isHover !== false) isHover = true;
	
	// 判断是否IE6
	var isIE6 = false;
	if (Browser.Engine.trident4) {
		isIE6 = true;	// For CSS hack
		
		if (isFirst) {
			var bodyHeight = document.body.offsetHeight-30;
			isFirst = false ;
		} else {
			var bodyHeight = document.body.offsetHeight;
		}
		var bodyTop = $(document).getScroll().y;
		$this.setStyle('position', 'absolute')
	}
	
	$this.setStyle('display', '');	// 计算size所以显示出来
	
	oSize = $this.getSize();
	wSize = $(window).getSize();
	cssLeft = (wSize.x - oSize.x) / 2;
	cssTop = (wSize.y - oSize.y) / 2 - 40;

	if (isIE6) cssTop += bodyTop;
	
	// 插入蒙版层
	if (isHover) {
		if (isIE6) {
			mask = new Element('div', {
				'styles': {
					position: 'absolute',
					left: 0,
					top: 0,
					'z-index': 1000,
					width: '100%',
					height: bodyHeight,
					background: '#666',
					filter: 'alpha(opacity=50)',
					opacity: 0.5
				}
			});
		} else {
			mask = new Element('div', {
				'styles': {
					position: 'fixed',
					left: 0,
					top: 0,
					'z-index': 1000,
					width: '100%',
					height: '100%',
					background: '#666',
					filter: 'alpha(opacity=50)',
					opacity: 0.5
				}
			});
		}
		mask.inject($this, 'before');
	}
	
	// 创建动画
	thisFx = new Fx.Morph($this, {
			fps: 200,
			duration: 200,
			transition: Fx.Transitions.Quart.easeIn
		});
	
	// 弹出dialog
	$this.setStyles({
		left: cssLeft,
		top: cssTop,
		opacity: 0
	});
	thisFx.start({
		opacity: 1
	});
	
	// 关闭dialog
	$this.getElement('a[name=close]').addEvent('click', function() {
		if (isHover) mask.dispose();	// 移除蒙版层
		thisFx.cancel().start({
			opacity: 0
		}).addEvent('complete', function() {
			$this.setStyles({
				display: 'none',
				left: '-9999',
				top: '-9999'
			})
		});
		
	});
}