// Dialog组件
// 制作：八月
// 修改：FlashSoft
// 070613
var sDialog = Class.create();
sDialog.prototype	= {
	loading:	'<div style="text-align:center;font-size:12px;">正在读取数据...</div>',
	config:
	{
		'box1':	{
					'type':			'div',
					'id':			'sDialogBox',
					'style':		'position:absolute;width:1px;height:1px;overflow:hidden;display:none;z-index:9999999;'
				},
		'box2':	{
					'type':			'iframe',
					'id':			'sDialogPage',
					'name':			'sDialogPage',
					'scrolling':	'no',
					'frameBorder':	'0',
					'style':		'width:100%;height:100%;z-index:9999999;display:none;'
				},
		'box3':	{
					'type':			'iframe',
					'id':			'sDialogHtml',
					'name':			'sDialogHtml',
					'scrolling':	'no',
					'frameBorder':	'0',
					'style':		'width:100%;height:100%;z-index:9999999;display:none;'
				},
		'box4':	{
					'type':			'div',
					'id':			'sDialogEasy',
					'style':		'width:100%;height:100%;z-index:9999999;display:none;'
				},
		'box5':	{
					'type':			'div',
					'id':			'sDialogShadow',
					'style':		'position:absolute;background:#3e811a;top:0px;left:0px;filter:alpha(opacity=60);-moz-opacity:0.6;opacity: 0.6;width:100%;height:100%;z-index:8888888;display:none;'
				},
		'box6':	{
					'type':			'iframe',
					'id':			'sDialogShadowBg',
					'scrolling':	'no',
					'frameBorder':	'0',
					'style':		'width:100%;height:100%;z-index:7777777;filter="progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";'
				}
	},
	// 自动调整高宽
	autoSize:			true,
	// 是否居中
	isMiddle:			true,
	// 是否显示阴影
	isShowShadow:		true,
	// 自动事件关闭
	isClose:			false,
	// 位置偏移
	xy:					{
		'x':			0,
		'y':			0
	},
	// 延时
	sleep:				{},
	// 定位
	goto:				{},
	// 内容
	page:				{},
	// HTML
	html:				{},
	// 标志
	mark:				'',
	// 阴影
	shadow:				{},
	// 默认对象
	module:				{},
	// 得到 iframe 内容
	$Frame:				function(_s){
		return document.frames ? document.frames[_s] : document.getElementById(_s).contentWindow;
	},
	// 创建组件
	createModule:		function(){
		var init	= {};
		var aModule	= [];
		for(module in this.config){
			init			= this.config[module];
			aModule[module]	= document.createElement(init['type']);
			for(field in init){
				if(field != 'type'){
					// IE style BUG
					if(field != 'style')
						aModule[module].setAttribute(field, init[field]);
					else
						aModule[module]['style']['cssText']	= init[field];
				}
			}
		}

		aModule['box1'].appendChild(aModule['box2']);
		aModule['box1'].appendChild(aModule['box3']);
		aModule['box1'].appendChild(aModule['box4']);
		aModule['box5'].appendChild(aModule['box6']);

		this.goto			= aModule['box1'];
		this.page			= aModule['box2'];
		this.html			= aModule['box3'];
		this.easy			= aModule['box4'];
		this.shadow			= aModule['box5'];

		if(document.body != null){
			// 添加基本组件
			document.body.appendChild(aModule['box1']);
			document.body.appendChild(aModule['box5']);
			// 点击灰色部分关闭对话框

			this.$Frame('sDialogShadowBg').document.open("text/html","replace");
			this.$Frame('sDialogShadowBg').document.write('<html><head></head><body style="padding:0px;margin:0px;" onclick="parent.dialog.reset();"></body></html>');
			this.$Frame('sDialogShadowBg').document.close();

			// 改变窗口大小调整对话框位置
			Event.observe(window, 'resize', this.dialogResize);
			// 增加 ONLOAD
			Event.observe('sDialogPage', 'load', this.pageOnload);

		}else{
			sDialog.sleep	= setInterval(function(){
				if(document.body != null){
					// 添加基本组件
					document.body.appendChild(aModule['box1']);
					document.body.appendChild(aModule['box5']);
					// 定时器清除
					clearInterval(sDialog.sleep);
					// 点击灰色部分关闭对话框

					dialog.$Frame('sDialogShadowBg').document.open("text/html","replace");
					dialog.$Frame('sDialogShadowBg').document.write('<html><head></head><body style="padding:0px;margin:0px;" onclick="parent.dialog.reset();"></body></html>');
					dialog.$Frame('sDialogShadowBg').document.close();

					// 改变窗口大小调整对话框位置
					Event.observe(window, 'resize', dialog.dialogResize);
					// 增加 ONLOAD
					Event.observe('sDialogPage', 'load', dialog.pageOnload);
				}
			}, 1);
		}
	},
	// 调整对话框位置
	dialogResize:		function(){
		if($('sDialogBox') != null && dialog.isMiddle){
			dialog.middle($('sDialogBox'), dialog.xy);
		}
	},
	initialize:			function(){
		this.createModule();
	},
	// 增加 ONLOAD 处理
	pageOnload:			function(){
		//特殊标识
		var mark = {
			'close':	'070424011',
			'cancel':	'070424014',
			'confirm':	'070424013'
		};
		var reFraem   = document.frames ? document.frames['sDialogPage'] : document.getElementById('sDialogPage').contentWindow;
		if (reFraem){
			try{

				var childs	= reFraem.document.getElementsByTagName('body')[0].childNodes;
				for(var i = 0; i < childs.length; i++){
					if(childs[i].tagName == 'TABLE' || childs[i].tagName == 'DIV') break;
				}

				var sHeight	= childs[i].offsetHeight.toString();
				var sWidth	= childs[i].offsetWidth.toString();

				if(sWidth != '0' && sHeight != '0'){
					$('sDialogBox').style.width		= sWidth	+ 'px';
					$('sDialogBox').style.height	= sHeight	+ 'px';
					$('sDialogPage').style.width	= sWidth	+ 'px';
					$('sDialogPage').style.height	= sHeight	+ 'px';

					parent.dialog.middle($('sDialogBox'), parent.dialog.xy);
				}

			}catch(e){}
		}
		
		if(this.isClose){
			// 增加点击关闭
			var ctrl	= reFraem.document.getElementsByTagName('*');
			for(var i = 0; i < ctrl.length; i++){
				for(var key in mark){
					if(ctrl[i].src && ctrl[i].src.indexOf(mark[key]) > -1){
						Event.observe(ctrl[i], 'click', 
							function(){
								parent.dialog.reset();
							}
						);
						if(key == 'confirm'){
							ctrl[i].focus();
						}
					}
				}
			}
		}
	},
	// 以 iframe 方式显示一网址
	showPage:			function(){

		var i		= 0;
		var oArg	= {
			'url':		'',
			'width':	'',
			'height':	'',
			'left':		'',
			'top':		''
		};

		for(var key in oArg){
			oArg[key]	= arguments[i++];
		}

		if(oArg['width'])
			this.goto.style.width		= oArg['width'] + 'px';

		if(oArg['height'])
			this.goto.style.height		= oArg['height'] + 'px';

		if(oArg['left']){
			this.goto.style.left		= oArg['left'] + 'px';
			this.isMiddle				= false;
		}

		if(oArg['top']){
			this.goto.style.top			= oArg['top'] + 'px';
			this.isMiddle				= false;
		}
		
		Element.hide(this.easy);
		Element.hide(this.html);
		Element.show(this.page);
		Element.show(this.goto);

		this.goto.style.filter		= 'alpha(opacity=0)';
		this.goto.style.MozOpacity	= '0.0';
		this.goto.style.opacity		= '0.0';
		
		/*
		this.$Frame(this.page.id).document.open();
		this.$Frame(this.page.id).document.write(this.loading);
		this.$Frame(this.page.id).document.close();
		*/
		if(oArg['url'].indexOf('?') > -1){
			this.$Frame(this.page.id).location.replace(oArg['url'] + '&QWERTYUIOP=' + Math.random());
		}else{
			this.$Frame(this.page.id).location.replace(oArg['url'] + '?QWERTYUIOP=' + Math.random());
		}

		//this.middle(this.goto, this.xy);
		this.mark	= 'page';
	},
	getPageSize:		function() {
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		}
		else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		}
		else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth - 17;
			windowHeight = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		}
		else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		}
		else { 
			pageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		}
		else {
			pageWidth = xScroll;
		}

		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	},
	middle:				function(_oThis, _iXY){
		if(!this.isMiddle)
			return false;
		

		var WH = this.getPageSize();

		_oThis.style.left	= (parseInt(WH[2] - _oThis.offsetWidth)/2).toString() + 'px';
		_oThis.style.top	= (Position.realOffset(document.body)[1] + (parseInt(WH[3] - _oThis.offsetHeight) / 2)).toString() + 'px';

		this.showShadow();
	},
	showShadow:			function(){

		if(!this.isShowShadow)
			return false;
		
		if($('sDialogBox').style.display == 'none')
			return false;

		var WH = this.getPageSize();
		var HEIGHT = document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight;

		this.shadow.style.width			= WH[2] + 'px';
		this.shadow.style.height		= HEIGHT + 'px';

		// 渐显
		this.shadowShow();
		
		// moz 显示 FLASH
		if(!Prototype.Browser.IE){
			var flash	= document.getElementsByTagName('object');
			for(var i = 0; i < flash.length; i++){
				flash[i].parentNode.style.visibility	= "hidden";
			}
		}

	},
	// 渐显
	shadowShow:			function(){

		var n		= 1;

		$('sDialogShadow').style.filter		= 'alpha(opacity=0)';
		$('sDialogShadow').style.MozOpacity = '0.0';
		$('sDialogShadow').style.opacity	= '0.0';

		Element.show('sDialogShadow');
		
		var step	= setInterval(
			function (){
				if(n > 1){
					clearInterval(step);
					$('sDialogShadow').style.filter		= 'alpha(opacity=100)';
					$('sDialogShadow').style.MozOpacity = '1.0';
					$('sDialogShadow').style.opacity	= '1.0';

					$('sDialogBox').style.filter		= 'alpha(opacity=100)';
					$('sDialogBox').style.MozOpacity	= '1.0';
					$('sDialogBox').style.opacity		= '1.0';
				}
				$('sDialogShadow').style.filter		= 'alpha(opacity=' + (n * 10) + ')';
				$('sDialogShadow').style.MozOpacity = '0.' + n;
				$('sDialogShadow').style.opacity	= '0.' + n;
				n = n + 1;
			},1 
		);
	},
	// 渐隐
	shadowHide:			function(){

                // 隐藏相关
                Element.hide('sDialogShadow');
                Element.hide('sDialogPage');
                Element.hide('sDialogHtml');
                Element.hide('sDialogBox');

                // 清空阴影
                $('sDialogShadow').style.width	= '0px';
                $('sDialogShadow').style.height	= '0px';
                // 隐
                $('sDialogShadow').style.filter		= 'alpha(opacity=0)';
                $('sDialogShadow').style.MozOpacity = '0.0';
                $('sDialogShadow').style.opacity	= '0.0';
	},
	reset:				function(){
		
		// moz 隐藏 FLASH, 要求 有父级对象
		if(!Prototype.Browser.IE){
			var flash	= document.getElementsByTagName('object');
			for(var i = 0; i < flash.length; i++){
				if(flash[i].parentNode.style.visibility == "hidden")
					flash[i].parentNode.style.visibility	= "visible";
			}
		}
		
		this.shadowHide();

		//Event.stopObserving(window, 'resize', dialog.dialogResize);
	}
};
function ms(id){
	for(i=0;i<6;i++){
		document.getElementById("st"+i).className = "";
	};
	if(id>0)
	{
		document.getElementById("st"+(id-1)).className ="bgnone"
		
	}
	document.getElementById("st5").className ="bgnone"
	document.getElementById("st"+id).className = "active";	
	var v=new Array("wiki","photo","ask","user","event","group");
	document.getElementById("v").value= v[id];

};
function s(s,v)
{
	if(v=="wiki"){window.open("/wiki/search.aspx?k="+s);}
}
