// 浏览器版本检测
var IsIE = false;
var IsFirefox = false;
var IsOpera = false;
var IsDialogComplete = false;


var DialogFrameName = "";
var DialogURL = "";
var DialogWidth = "";
var DialogHeight = "";
var DialogTitle = "";
var DialogTitleBGStyle = "";
var DialogTitleStyle = "";
var DialogScrollType = "no";


if (window.navigator.userAgent.indexOf("Opera")>=1)
	{ IsOpera = true;	}
else
	{
		if (window.navigator.userAgent.indexOf("MSIE")>=1)
			{ IsIE = true;	}
		else
			{
				if (window.navigator.userAgent.indexOf("Firefox")>=1)
					{ IsFirefox = true ;	}
				else
					{ IsOpera = true; 	}
			}
	}


function $(v){
	return document.getElementById(v);
}


function exist(s)
{
	return $(s)!=null;
}

function myInnerHTML(idname, html)
{
	if (exist(idname))
	{
		$(idname).innerHTML = html;
	}
}

function getFilePathDepth(){
var syspath = location.href; 
syspath = syspath.toLowerCase();      //把路径名称转换成小写

myPositionEnd = syspath.lastIndexOf("/");  // 获取文件路径中的最后一个"/"
myPositionBegin = syspath.lastIndexOf("script/");  // 获取文件路径中的"script/"

syspath = syspath.substring(myPositionBegin,parseInt(myPositionEnd)+1); // 使用substring函数 截取"/"之前的字符串，就得到当前目录的路径 
syspath = syspath.replace("file:///","");   //这里要把file:///替换为空，否则会报错
syspath = syspath.replace(new RegExp("%20","gm")," ");   // 如果文件名中含有空格，则要还原空格，替换所有的 %20 为 " "

returnPath = "";

for(var i=1; i<syspath.split("/").length; i++){  
	returnPath += "../" ;
}  

return returnPath;
}










/*
跨框架弹出窗口
inObj:框架对象名
inURL:弹出的文件网址
inWidth:宽度
inHeight:高度
inTitle:标题
inTitleBGStyle:标题背景风格
inTitleStyle:标题文字风格
inScrollType:滚动条风格
		auto:自动显示滚动条
		默认隐藏滚动条
*/

function transDialog(inObj , inURL , inWidth , inHeight , inTitle , inTitleBGStyle , inTitleStyle , inScrollType){

	DialogFrameName = inObj ;
	DialogURL = inURL;
	DialogWidth = inWidth;
	DialogHeight = inHeight;
	DialogTitle = inTitle;
	DialogTitleBGStyle = inTitleBGStyle;
	DialogTitleStyle = inTitleStyle;
	DialogScrollType = inScrollType;


	var inFileNamePath = getFilePathDepth();
	var inFileName = inFileNamePath + "js/openDialog.js";

	//传送弹窗的JS文件
	createScript( inObj , inFileName);

	//执行弹窗
	transDialogAction();

}



function transDialogAction(){
	//判断弹窗的JS文件是否完成
	if (DialogFrameName.IsDialogComplete){
		DialogFrameName.openDialog(DialogURL , DialogWidth, DialogHeight, DialogTitle, DialogTitleBGStyle , DialogTitleStyle , null , DialogScrollType);
	}
	else{
		window.setTimeout('transDialogAction()', 1000 );
	}
}



function createScript( ObjName , FileName){
	var myscript = ObjName.document.createElement("script");  
	myscript.src = FileName;
	myscript.defer = true;
	ObjName.document.body.appendChild(myscript);
}



/*
功能说明:
_sUrl:调用的文件网址
_sWidth:宽度
_sHeight:高度
_sTitle:标题文字
_sTitleBGStyle:标题背景风格
_sTitleStyle:标题方案风格
_sCloseFun:自定义关闭窗口时执行的函数
_sScrollType:窗口里面的滚动条类型
		auto:自动显示滚动条
		默认隐藏滚动条
*/

function openDialog(_sUrl, _sWidth, _sHeight, _sTitle , _sTitleBGStyle , _sTitleStyle , _sCloseFun , _sScrollType ){

	if(typeof(arguments[4]) != "undefined")
	{
		var oEdit = new dialog(_sWidth, _sHeight, _sTitle, _sTitleBGStyle , _sTitleStyle , _sCloseFun , _sScrollType);
	}
	else
	{
		var oEdit = new dialog(_sWidth, _sHeight, _sTitle);
	}
	
	oEdit.init();
	oEdit.open(_sUrl);
	
}

function closeDialog(){
	new dialog().close();
}


function showme(object) //显示指定OBJECT下的所有属性
{
	for (var prop in object)
	{	document.write(prop+"="+object[prop]+"<br>"); }
}



function dialog(v_w, v_h, v_title, v_title_bg_style , v_title_style , v_close_function , v_scroll_type)
{
	var width = v_w;
	var height = v_h;
	var title = v_title;
	var title_bg_style = v_title_bg_style;
	var title_style = v_title_style;
	var scroll_type = v_scroll_type;	
	var imgFilePath = getFilePathDepth();
	
	var sClose = '<img src="' + imgFilePath + 'images/page_images/close.gif" style="cursor:pointer;" alt="关闭" title="关闭" onclick="javascript:new dialog().close('+v_close_function+');">';

	var sBox = '\
		<div id="dialogBox" style="display:none;z-index:19999;width:'+width+'px;">\
		<div style="width:460px; filter:alpha(opacity=40); opacity: 0.4; -moz-opacity:0.4;position:absolute;top:0px;"><img src="' + imgFilePath + 'images/page_images/dialog_box_t.gif" width="'+width+'" height="8" /></div>\
		<div style="position:absolute;width:'+width+'px;height:'+height+'px;top:8px;" >\
		<center><iframe frameborder="0" style="width:'+(width-14)+'px; height:'+height+'px;z-index:-1"></iframe></center>\
		<div style="position:absolute;left:0px;top:0px;" >\
		<table border="0" cellpadding="0" cellspacing="0">\
		<tr style="height:'+(height)+'px;" valign="top">\
		<td style="background:#000000;width:7px;filter:alpha(opacity=40);opacity: 0.4; -moz-opacity:0.4;"></td>\
		<td style="width:'+(width-14)+'px;">\
		<table border="0" cellpadding="10" cellspacing="0" style="border:1px solid #565656;width:100%;height:' + (height) + 'px;">\
		<tr style="height:30px;" bgcolor="#FFFFFF">\
		<td style="border-bottom:1px solid #565656;'+title_bg_style+'">\
		<table width="100%" border="0" cellspacing="0" cellpadding="0">\
		<tr>\
		<td style="'+title_style+'">'+title+'</td>\
		<td align="right">'+sClose+'</td>\
		</tr>\
		</table>\
		</td>\
		</tr>\
		<tr valign="top">\
		<td id="dialogBody" bgcolor="#E7E7E7"></td>\
		</tr>\
		</table>\
		</td>\
		<td style="background:#000000;width:7px;filter:alpha(opacity=40);opacity: 0.4; -moz-opacity:0.4;"></td></tr>\
		</table>\
		</div>\
		</div>\
		<div style="width:460px; filter:alpha(opacity=40);opacity: 0.4; -moz-opacity:0.4;position:absolute;top:'+(height+8)+'px;"><img src="' + imgFilePath + 'images/page_images/dialog_box_b.gif" width="'+width+'" height="8" /></div>\
		</div>\
		<div id="dialogBoxShadow" style="display:none;z-index:19998;"></div>\
	';

	
	var sIfram = '\
		<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 sBG = '\
		<div id="dialogBoxBG" style="position:absolute;top:0px;left:0px;width:100%;height:100%;"></div>\
	';
	

	switch (scroll_type){
		case "auto":
				scroll_type = "auto";	//自动显示滚动条	
				break;	
					
		default:
				scroll_type = "no";	//隐藏滚动条
	}

	this.init = function()
	{
	
		$('dialogCase') ? $('dialogCase').parentNode.removeChild($('dialogCase')) : function(){};
		var oDiv = document.createElement('span');
		oDiv.id = "dialogCase";
		if (!IsOpera)
		{
			oDiv.innerHTML = sBG + sIfram + sBox;
		}
		else
		{
			oDiv.innerHTML = sBG + sBox;
		}
		document.body.appendChild(oDiv);
	}

	this.open = function(_sUrl)
	{		
		this.show();
		var openIframe = "<iframe width='100%' height='100%' name='iframe_parent' id='iframe_parent' src='" + _sUrl + "' frameborder='0' scrolling='" + scroll_type + "' allowTransparency='true'></iframe>";
		myInnerHTML('dialogBody', openIframe);
		
		$('iframe_parent').src = _sUrl;
		
	}

	this.show = function()
	{
		this.middle('dialogBox');
		
		if ($('dialogIframBG'))
		{
			$('dialogIframBG').style.top = $('dialogBox').style.top;
			$('dialogIframBG').style.left = $('dialogBox').style.left;
			$('dialogIframBG').style.width = $('dialogBox').offsetWidth + "px";
			$('dialogIframBG').style.height = $('dialogBox').offsetHeight + "px";
			$('dialogIframBG').style.display = 'block';
		}
		if (!IsOpera) {
			this.shadow();
		}

	}
	
	this.reset = function()
	{
		this.close();
	}

	this.close = function(iCloseFunction)
	{
		
		if (window.removeEventListener) 
		{
			window.removeEventListener('resize', this.event_b, false);
			window.removeEventListener('scroll', this.event_b, false);
		} 
		else if (window.detachEvent) 
		{
			try {
				window.detachEvent('onresize', this.event_b);
				window.detachEvent('onscroll', this.event_b);
			} catch (e) {}
		}
		if ($('dialogIframBG')) {
			$('dialogIframBG').style.display = 'none';
		}
		$('dialogBox').style.display='none';
		$('dialogBoxBG').style.display='none';
		$('dialogBoxShadow').style.display = 'none';

		if (typeof(iCloseFunction) == "function"){
			iCloseFunction();	
		}

	}

	this.shadow = function()
	{
		this.event_b_show();
		if (window.attachEvent)
		{
			window.attachEvent('onresize', this.event_b);
			window.attachEvent('onscroll', this.event_b);
		}
		else
		{
			window.addEventListener('resize', this.event_b, false);
			window.addEventListener('scroll', this.event_b, false);
		}
	}
	
	this.event_b = function()
	{
		var oShadow = $('dialogBoxShadow');
		
		if (oShadow.style.display != "none")
		{
			if (this.event_b_show)
			{
				this.event_b_show();
			}
		}
	}
	
	this.event_b_show = function()
	{
		var oShadow = $('dialogBoxShadow');
		oShadow['style']['position'] = "absolute";
		oShadow['style']['display']	= "";		
		oShadow['style']['opacity']	= "0.2";
		oShadow['style']['filter'] = "alpha(opacity=20)";
		oShadow['style']['background']	= "#000";
		oShadow['style']['top'] = "0px";
		oShadow['style']['left'] = "0px";

		if (IsIE == false){
			oShadow['style']['width'] = "100%";
		}
		else{
			oShadow['style']['width'] = this.getPageSize("windowWidth") + "px";
		}

		oShadow['style']['height'] = this.getPageSize("pageHeight") + "px";

	}

	this.middle = function(_sId)
	{

		$(_sId)['style']['display'] = '';
		$(_sId)['style']['position'] = "absolute";

		var sLeft = (this.getPageSize("pageWidth") - 20 - $(_sId).clientWidth) / 2;
		var sTop = this.getPageSize("PageScroll") + 80;

		$(_sId)['style']['left'] = sLeft + "px";
		$(_sId)['style']['top'] = sTop + "px";
		
	}

	this.getPageSize = function(_sId)
	{
			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 PageScroll;
			if (self.pageYOffset) {
				PageScroll = self.pageYOffset;
			} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
				PageScroll = document.documentElement.scrollTop;
			} else if (document.body) {// all other Explorers
				PageScroll = document.body.scrollTop;
			}
	
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				windowWidth = self.innerWidth;
				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;
			}

			switch (_sId){
				case "pageWidth":
						return pageWidth;
						break;
				case "pageHeight":
						return pageHeight;
						break;
				case "windowWidth":
						return windowWidth;
						break;
				case "windowHeight":
						return windowHeight;
						break;																		
				case "PageScroll":
						return PageScroll;
						break;
				default:
						return 0;
				}
	}

}

//脚本读取完成
IsDialogComplete = true;
