/**
 * Dialog类
 * 功能：弹出框
 * 
 * 例子：
 * oDialog = new Dialog();
 * <a href="javascript:oDialog.open('http://www.baidu.com', 470, 372, '标题');">弹出窗口</a>
 */	
var ck_dialog_refresh = null;	//为1时关闭后自动刷新
if (typeof (Dialog) != "function") {
	var Dialog = function() {
		Dialog.superclass.constructor.call(this);
		this.url = null;
		this.width = null;
		this.height = null;
		this.title = null;
		this.dialogImgUrl = this.imgUrl+"/Dialog";
		this.imgh460_t = this.dialogImgUrl+'/h460_t_h.gif';
		this.imgh460_b = this.dialogImgUrl+'/h460_b_h.gif';
		this.imgclose = this.dialogImgUrl+'/close2.gif';
		

		/**
		 * 弹出窗口
		 * @param string url     显示内容的url
		 * @param integer width  宽度
		 * @param integer height 高度
		 * @param string title   标题
		 */
		this.open = function(url, width, height, title, autorefresh){		
			this.url = url;
			this.width = width;
			this.height = height;
			this.title = title;
			if(typeof(arguments[4])=='undefined'){
				ck_dialog_refresh = 0;
			}else{
				ck_dialog_refresh = autorefresh;
			}
			init(this);
			openUrl( this );
		};	
		
		/**
		 * 关闭
		 */
		this.close = function(){
			if (window.removeEventListener){
				window.removeEventListener('resize', event_b, false);
				window.removeEventListener('scroll', event_b, false);
			}else if (window.detachEvent){
				try {
					window.detachEvent('onresize', event_b);
					window.detachEvent('onscroll', event_b);
				} catch (e) {}
			}		

			/**/
			if($("#dialogIframBG")){$("#dialogIframBG").css("display","none");}
			$("#dialogBox").css("display","none"); 
			$("#dialogBoxBG").css("display","none"); 
			$("#dialogBoxShadow").css("display","none");
			//document.body.removeChild($('#dialogBox')); 
			/**/
			/*
			if($('#dialogBoxShadow')) {
				$("div").remove("#dialogBoxShadow");
			}
			
			if($('#dialogBoxBG')) {
				$("div").remove("#dialogBoxBG");
			}

			if($('#dialogIframBG')) {
				$("div").remove("#dialogIframBG");
			}

			if($('#dialogBox')) {
				$("div").remove("#dialogBox");
			}
			//document.body.removeChild($('#dialogBox'));
			*/
			if(ck_dialog_refresh==1) document.location.reload();
			/*try {
				$("#updateTextarea").focus();
			} catch (e) {}	*/		
		};
		
		/**
		 * 初始化
		 */	 
		 function init(obj){
			var htmlBox = '\
				<div id="dialogBox" style="display:none;z-index:19999;width:'+obj.width+'px;">\
					<div style="position:absolute;top:0px;width:'+obj.width+'px;filter:alpha(opacity=40); -moz-opacity:0.4;opacity:0.4;height:8px;" class="dialog_borderbg2" ></div>\
					<div style="position:absolute;height:'+obj.height+'px;top:8px;">\
						<table border="0" cellpadding="0" cellspacing="0">\
							<tr style="height:'+obj.height+'px;">\
								<td  class="dialog_borderbg"  style="filter:alpha(opacity=40); -moz-opacity:0.4;opacity:0.4;width:7px;"></td>\
								<td style="width:'+(obj.width-14)+'px;">\
									<div class="dialogBox_border" >\
										<table width="100%" border="0" cellpadding="0" cellspacing="0">\
											<tr >\
												<td class="dialogBox_tit_box">\
													<div  class="dialogBox_tit">\
														<div id="dialogBoxTitle" >'+obj.title+'</div>\
														<div id="dialogClose" style="float:right; padding:2px 3px;"><a href="javascript:oDialog.close();" style="font-size:13px;color:#fff;padding:3px 8px;text-decoration:none;"><!--<img src="'+obj.imgclose+'" alt="关闭" />x-->关闭</a></div>\
													</div>\
												</td>\
											</tr>\
											<tr valign="top">\
												<td id="dialogBody" style="height:'+(obj.height-28)+'px;" ></td>\
											</tr>\
										</table>\
									</div>\
								</td>\
								<td  class="dialog_borderbg"  style="width:7px; filter:alpha(opacity=40); -moz-opacity:0.4;opacity:0.4;"></td>\
							</tr>\
						</table>\
					</div>\
					<div  style="position:absolute;top:'+(obj.height+8)+'px;width:'+obj.width+'px; filter:alpha(opacity=40); -moz-opacity:0.4;opacity:0.4; height:8px;"  class="dialog_borderbg2" ></div>\
				</div>\
				<div id="dialogBoxShadow" style="display:none;z-index:19998;"></div>\
				';
			var htmlIfram = '<iframe id="dialogIframBG" name="dialogIframBG" frameborder="0" marginheight="0" marginwidth="0" hspace="0" vspace="0" scrolling="no" style="position:absolute;z-index:19997;display:none;"></iframe>';
			var htmlBg = '<div id="dialogBoxBG" style="position:absolute;top:0px;left:0px;width:100%;height:100%;"></div>';

			if( obj.exist("#dialogCase") ){
				if($('#dialogBox')) {
					$("div").remove("#dialogBox");
				}

				if($('#dialogBoxShadow')) {
					$("div").remove("#dialogBoxShadow");
				}				

				if($('#dialogCase')) {
					$("div").remove("#dialogCase");
				}

				if($('#dialogBoxBG')) {
					$("div").remove("#dialogBoxBG");
				}

				if($('#dialogIframBG')) {
					$("div").remove("#dialogIframBG");
				}
			}
			var oDiv = document.createElement('div');
			oDiv.id = "dialogCase";
			if (obj.browserName == "opera"){
				oDiv.innerHTML = htmlBg + htmlBox;				
			}else{
				oDiv.innerHTML = htmlBg + htmlIfram + htmlBox;
			}
			document.body.appendChild(oDiv);
		};

		
		/**
		 * 打开
		 */
		function openUrl(obj){
			show(obj);
			$("#dialogBody").html("<iframe width='100%' height='100%' name='iframe_parent' id='iframe_parent' src='" + obj.url + "' frameborder='0' scrolling='no'></iframe>");
		};
		
		/**
		 * 背景阴影
		 */
		function show(obj){
			middle(obj,"#dialogBox");
			if( obj.exist("#dialogIframBG") ){
				$("#dialogIframBG").css("top", $("#dialogBox").css("top"));
				$("#dialogIframBG").css("left", $("#dialogBox").css("left"));
				$("#dialogIframBG").width(obj.width);
				$("#dialogIframBG").height(obj.height);
				$("#dialogIframBG").css("display", "block");
			}
			if( obj.browserName != "opera" ){
				shadow();
			}
		};
		
		/**
		 * 位置居中
		 */
		function middle(obj,id){
			$(id).css("display", "");
			$(id).css("position", "absolute");		
			var bodyWidth = parent.document.body.clientWidth;
			var bodyHeight = parent.document.body.clientHeight;
			var scrollTop = parent.document.body.scrollTop+parent.document.documentElement.scrollTop;
			var left = (bodyWidth - obj.width) / 2;
			if(scrollTop + 80 > 0){
				var top = bodyHeight - obj.height - scrollTop;
			}else{
				var top = 0;
			}
			var top = (scrollTop + 180) > 0 ? (scrollTop + 180) : 0;
			$(id).css("left", left+"px");
			$(id).css("top", top+"px");
		};
		
		function shadow(){
			 event_b_show();
			if (window.attachEvent){
				window.attachEvent('onresize', event_b);
				window.attachEvent('onscroll', event_b);
			}else{
				window.addEventListener('resize', event_b, false);
				window.addEventListener('scroll', event_b, false);
			}
		};
		
		function event_b_show(){
			var oShadow = $('#dialogBoxShadow');
			oShadow.css({position:"absolute",display:"block",opacity:"0.2",filter:"alpha(opacity=20)",background:"#000"});
			var bodyWidth = parent ? parent.document.body.offsetWidth : document.body.offsetWidth;
			var bodyHeight = parent ? parent.document.body.offsetHeight : document.body.offsetHeight;
			var scrollTop = parent ? (parent.document.body.scrollTop+parent.document.documentElement.scrollTop) : (document.body.scrollTop+document.documentElement.scrollTop);
			oShadow.css({top:"0px",left:"0px"});
			oShadow.width(bodyWidth);
			oShadow.height(bodyHeight + scrollTop);
		};
		
		function event_b(){
			if ($('#dialogBoxShadow').css("display") != "none"){
				event_b_show();
			}
		};
	}
	extend(Dialog, Base);	
}
