/**
 * 这个对象用于操作弹出层风格的对话框，对话框的内容是打开一个iframe来显示指定的网页。
 * 常见的用法：
 *
 * 最简约的写法
 * iframeDialog.open('http://www.google.com/');
 * 加上参数的写法
 * iframeDialog.open('http://www.google.com/',{
 *     width	:500,
 *     height	:400,
 *     title	:'Google~~'
 * });
 * js内置的alert(),confirm()的替代方案
 * iframeDialog.alert('你好');
 * iframeDialog.confirm('你好');
 * 带执行函数的写法
 * iframeDialog.alert('你好',{
 *     width	:500,
 *     height	:400,
 *     title	:'好个屁',
 *     action	:function(){
 *         location.href='http://www.baidu.com/';
 *     }
 * });
 *
 */
var iframeDialog={
	titleBgImgSrc	:'/Plug/images/lightbox_tit_bg.gif',
	sureButtonBgImgSrc	:'/Plug/images/bg.gif',
	cancelButtonBgImgSrc	:'/Plug/images/bg.gif',
	inited	:false,
	openCount:0,
	//初始化对话框，插入dom模型。
	init	:function(){
		var wrapperHtml='<div id="iframeDialog-wrapper">\
				<style type="text/css">#iframeDialog-box {position:absolute;}</style>\
				<div id="iframeDialog-bg" style="background:#000; filter: alpha(opacity=20); -moz-opacity: 0.2; position:absolute; z-index:109; top:0; left:0; margin:0; padding:0"></div>\
				<div id="iframeDialog-box" style="width:300px; height:200px; background:#f4fcff; border:5px #808080 solid; font-size:12px; z-index:110; left:0; top:0;">\
					<div id="iframeDialog-boxTitle" style="color:#fff; height:26px; font-size:12px; font-weight:normal; line-height:26px; border:1px solid #fff; padding-left:5px;background:url('+this.titleBgImgSrc+') repeat-x 0 0;background-color:#666666;">\
						<span id="iframeDialog-boxClose" style="float:right; margin-right:5px;">\
						<a href="javascript:;" onclick="iframeDialog.close();" style="display:block; color:#fff; text-decoration:none; width:10px; height:10px;">×</a>\
						</span>\
						<span id="iframeDialog-title" style="float:left;">请稍等...</span>\
					</div>\
					<div id="iframeDialog-body" style="width:100%; height:174px;"></div>\
				</div>\
			</div>';
		$('#iframeDialog-wrapper').remove();
		$('body').append(wrapperHtml);
		//$('#iframeDialog-box').css('position','absolute');
		this.inited=true;
		return this;
	},
	//显示对话框。
	show	:function(){
		$('#iframeDialog-wrapper').show();//.hide().show();
		this.middle();
		this.background();
		$(window).resize(this.middle).resize(this.background);//.scroll(this.middle);
		return this;
	},
	//使对话框居中。
	middle :function(){
		var elem=$('#iframeDialog-box');
		var docElem=$(document.documentElement);
		var left=docElem.attr('scrollLeft')+(docElem.attr('clientWidth')-elem.attr('offsetWidth'))/2;
		elem.css('left',left);
		if(elem.height()+docElem.attr('scrollTop')+150<$('body').height()-20){
			elem.css('top',docElem.attr('scrollTop')+150);
		}
	},
	//设置灰色背景。
	background :function(){
		$('#iframeDialog-bg').height($('body').height()).width($('body').width()).bgiframe();
	},
	//这个方法是打开某个url。
	openUrl	:function(url){
		this.inited ? this.show() : this.init().show();
		$('#'+this.frameName).remove();
		this.frameName = "iframeDialog-iframe_" + this.openCount++;
		var openIframe = '<iframe id="'+this.frameName+'" name="'+this.frameName+'" src="about:blank" frameborder="0" width="100%" height="100%" scrolling="no"></iframe>';
		$('#iframeDialog-body').append(openIframe);
		var iframe = window.frames[this.frameName];
		if(iframe.document){
			iframe.document.open();
			iframe.document.write('<div style="height:100px;padding-top:50px;" align="center"><img align src="/Theme/Default/images/loading.gif" /></div>');
			iframe.document.close();
		}
		$('#'+this.frameName).attr('src',url);
		return this;
	},
	/*
	 * 打开某个地址的方法，这个是最主要的方法。
	 */
	open:function(url,width,height,title){
		this.openUrl(url);
		this.title(title==undefined ? '请稍等...':title);
		this.resize(width,height);
		return this;
	},
	//关闭对话框。
	close	:function(){
		$('#iframeDialog-wrapper').hide();
		$(window).unbind('resize',this.middle).unbind('resize',this.background).unbind('scroll',this.middle);
		return this;
	},
	//重新设置对话框的宽和高。
	resize	:function(w,h){
		//if(!w&&!h){
		//	var $contentsWrapper=$(window.frames[this.frameName].document).find('#iframeDialog-contentsWrapper');
		//	var w=$contentsWrapper.width(),h=$contentsWrapper.height();
		//	alert('f-h:'+h+'\n'+$contentsWrapper.size());
		//	w&&h ? this.resize(w,h+100) : {};
		//}
		//$('#iframeDialog-box').css({width:w?w:300,height:h?h:200});
		$('#iframeDialog-box').width(w?w:300);
		//在ie6里，必须减30px才能得到和其他浏览器一致的高度。
		$('#iframeDialog-box').height((h?h:200) - ($.browser.msie?($.browser.version>6?0:30):0));
		//为了兼容ie6的一个bug，如果不加这句，高度可能不会及时更新。
		$('#iframeDialog-body').css('height','99%').css('height',$('#iframeDialog-box').height()-27);
		this.middle();
		return this;
	},
	//设置或获取iframeDialog的标题。
	title	:function(title){
		if(title!=undefined){
			$('#iframeDialog-title').text(title);
			return this;
		}else{
			return $('#iframeDialog-title').text();
		}
	},
	//这个方法由confirm()和alert()内部调用，用于把指定的html写入到对话框内。
	openHtml	:function(htmlContent){
		this.openUrl('about:blank');
		var iframe = window.frames[this.frameName];
		var iframeHtml='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\
		<html xmlns="http://www.w3.org/1999/xhtml">\
		<head>\
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\
		<style type="text/css">\
		* { padding:0; margin:0; list-style:none; }\
		body { background-color:#fff; margin:10px; width:100%; color:#333; font-family:Arial, sans-serif; font-size:12px; line-height:160%; position:relative; }\
		.sure { width:95px; height:31px; background:url('+this.sureButtonBgImgSrc+') -154px -169px no-repeat; }\
		.cancel { width:72px; height:28px; background:url('+this.cancelButtonBgImgSrc+') -261px -169px no-repeat; margin:3px 0 0 3px; }\
		.but { margin-top:10px; border:0px;background-color:#666666; }\
		</style>\
		</head>\
		<body>'+ htmlContent + '</body>\
		</html>';
		if(iframe.document){
			iframe.document.open();
			iframe.document.write(iframeHtml);
			iframe.document.close();
		}
		return this;
	},
	/*
	 * 和js内置的alert()类似，用于弹出一个对话框。
	 * params可用的参数有：title,action
	 */
	alert	:function(content,width,height,action,title){
		var html = '<div>\
				<div>\
					 '+content+'\
				</div>\
				<div style="text-align:center">\
					<input class="but sure" type="button" value="" onclick="javascript:parent.iframeDialog.action();" />\
				</div>\
			   </div>';
		this.openHtml(html);
		this.title(title==undefined ? '提示':title);
		this.resize(width,height);
		this.action=$.isFunction(action)?action:function(){iframeDialog.close();};
		return this;
	},
	/*
	 * 和js内置的confirm()类似，用于弹出一个确认的对话框。
	 * params可用的参数有：title,action
	 */
	confirm	:function(content,width,height,action,title){
		var html = '<div>\
				<div>\
					 '+content+'\
				</div>\
				<div style="text-align:center">\
					<input class="but sure" type="button" value="" onclick="javascript:parent.iframeDialog.action();" /> <input class="but cancel" type="button" value="" onclick="javascript:parent.iframeDialog.close();"/>\
				</div>\
			   </div>';
		this.openHtml(html);
		this.title(title==undefined ? '请确认':title);
		this.resize(width,height);
		this.action=$.isFunction(action)?action:function(){iframeDialog.close();};
		return this;
	},
	/**
	 * 绑定回调函数
	 */
	setAction	:function(action){
		$.isFunction(action)?this.action=action:{};
	},
	/**
	 * 执行回调函数
	 */
	executeAction	:function(args){
		this.action?this.action(args):{};
	}
}

 




