﻿//==============================
// ASBox_信息提示窗口
// Powered By FelixDong
// Last Update: 2008.06.15
//==============================
//所需参数:
//皮肤: 类型: Type[1-图片,2-Html], 标题: Title, 内容(图片的话就是图片地址,内容的话就是隐藏DIV的ID): Content, 高度: Height, 宽度: Width, 链接地址: Url, 效果类型:Effect[1-无效果;2-立即变暗,3-渐变变暗], 是否保持屏幕中央: Center[true-保持中央,false-固定]
function ShowASBox(Type, Title, Content, Height, Width, Url, Effect, Center) {
	//函数初始化开始
	var ASBox_Type		= 2;
	var ASBox_Content	= "正在加载内容,请稍候...";
	var Effect_Type		= 1;
	var isCenter		= false;
	var gotoUrl			= 0;
	var loadCancelled	= false;
	var ASBox_Drag		= false;
	var ASBox_Skin		= "ASBox_Skin_ASBLOG";
	//函数初始化结束
	//构建所需要的层元素开始
	var strHTML = "<div id=\"ASBox_window\" style=\"display:none;\"></div>\n";
	strHTML +=	"<div id=\"ASBox_progress\" style=\"display:none;\"><img src=\""+BlogPath+"Include/ASBox/ASBox_Loading.gif\" alt=\"正在加载内容,请稍候...\" border=\"0\" /></div>\n";
	strHTML +=	"<div id=\"ASBox_body\" class=\""+ASBox_Skin+"\" style=\"display:none;position:absolute;\">\n";
	strHTML +=	"  <div id=\"ASBox_handle\" class=\"ASBox_header\">\n";
	strHTML +=	"    <div id=\"ASBox_header_title\"></div>\n";
	strHTML +=	"    <div class=\"ASBox_header_close\"><img src=\""+BlogPath+"Include/ASBox/ASBox_CloseImg.gif\" id=\"ASBox_closeimg\" /></div>\n";
	strHTML +=	"    <div style=\"clear: both;\"></div>\n";
	strHTML +=	"  </div>\n";
	strHTML +=	"  <div class=\"ASBox_wrap\">\n";
	strHTML +=	"    <div class=\"ASBox_border\">\n";
	strHTML +=	"      <div id=\"ASBox_main\"></div>\n";
	strHTML +=	"    </div>\n";
	strHTML +=	"  </div>\n";
	strHTML +=	"</div>\n";
	var ASBox = document.createElement("div");
	ASBox.setAttribute("id","ASBox");
	ASBox.style.display	= "";
	ASBox.innerHTML		= strHTML;
	document.getElementsByTagName("body")[0].appendChild(ASBox);
	//构建所需要的层元素结束
	//输出底部内容
	var ASBox_body			= getElem("ASBox_body");
	var ASBox_header_title	= getElem("ASBox_header_title");
	var ASBox_openwin		= "<span id=\"ASBox_openwin\">浏览完整图片</span>";
	if (Title != "") {ASBox_header_title.innerHTML = ASBox_openwin + Title;} else {ASBox_header_title.innerHTML = ASBox_openwin;}
	//定制内容类型
	ASBox_Type		= Type;
	ASBox_Content	= Content;
	switch(ASBox_Type) {
		case 1:
			showIndicator();
			imgPreloader = new Image();
			imgPreloader.onload = function() {
				imgPreloader = resizeImageToScreen(imgPreloader);
				hideIndicator();
				var strHTML = "<img src=\""+ASBox_Content+"\" title=\"关闭窗口\" style=\"width:"+imgPreloader.width+"px;height:"+imgPreloader.height+"px;border:0;cursor:hand;margin:0;padding:0;position:absolute;\"/>";

				if (loadCancelled == false) {
					if (imgPreloader.DefaultSize == true) {
						ASBox_body.style.height = "205px";
						ASBox_body.style.width = "425px";
					} else {
						if (Height != 0) {ASBox_body.style.height = Height + "px";}
						else {ASBox_body.style.height = imgPreloader.height + 100 + "px";}
						if (Width != 0) {ASBox_body.style.width = Width + "px";}
						else {ASBox_body.style.width = imgPreloader.width + 100 + "px";}
					}
					ASBox_body.style.display	= "";
					ASBox_body.style.visibility	= "hidden";
					posToCenter(ASBox_body);
					ASBox_body.style.visibility	= "visible";
					setASBoxContent(strHTML);
					if (ASBox_Drag == true) { if (ASBox_Drag == true) { DragBox(getElem("ASBox_handle"),getElem("ASBox_body")); } }
				}

			}
			loadCancelled		= false;
			imgPreloader.src	= ASBox_Content;
			break;

		case 2:
			showIndicator();
			var strHTML = "";

			if (Height != 0) {ASBox_body.style.height = Height + "px";}
			else {ASBox_body.style.height = "425px";}
			if (Width != 0) {ASBox_body.style.width = Width + "px";}
			else {ASBox_body.style.height = "205px";}

			hideIndicator();
			ASBox_body.style.display	= "";
			ASBox_body.style.visibility	= "hidden";
			posToCenter(ASBox_body);
			ASBox_body.style.visibility	= "visible";
			getElem("ASBox_main").style.overflow = "auto";
			setASBoxContent(Content);
			if (ASBox_Drag == true) { DragBox(getElem("ASBox_handle"),getElem("ASBox_body")); }
			break;

		case 3:
			showIndicator();
			var strHTML = "";

			if (Height != 0) {ASBox_body.style.height = Height + "px";}
			else {ASBox_body.style.height = "425px";}
			if (Width != 0) {ASBox_body.style.width = Width + "px";}
			else {ASBox_body.style.height = "205px";}
			hideIndicator();
			ASBox_body.style.display	= "";
			ASBox_body.style.visibility	= "hidden";
			posToCenter(ASBox_body);
			ASBox_body.style.visibility	= "visible";
			getElem("ASBox_main").style.overflow = "auto";

			var elemSrc = getElem(Content);
			if(elemSrc) {strHTML = elemSrc.innerHTML;}
			setASBoxContent(strHTML);
			if (ASBox_Drag == true) { DragBox(getElem("ASBox_handle"),getElem("ASBox_body")); }
			break;

		case 4:
			showIndicator();
			AjaxUrl = Content;
			http.open("GET", AjaxUrl, true);
			http.onreadystatechange = function() {
				if(http.readyState == 4){
					if (Height != 0) {ASBox_body.style.height = Height + "px";}
					else {ASBox_body.style.height = "425px";}
					if (Width != 0) {ASBox_body.style.width = Width + "px";}
					else {ASBox_body.style.height = "205px";}

					hideIndicator();
					ASBox_body.style.display	= "";
					ASBox_body.style.visibility	= "hidden";
					posToCenter(ASBox_body);
					ASBox_body.style.visibility	= "visible";
					getElem("ASBox_main").style.overflow = "auto";

					var response = http.responseText;
					setASBoxContent(response);
					if (ASBox_Drag == true) { DragBox(getElem("ASBox_handle"),getElem("ASBox_body")); }
				}
			}
			http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=GB2312");
			http.send(null);
			break;

		default:
	}
	ASBox_body.style.opacity= 100;
	ASBox_body.style.filter	= "alpha(opacity=100)";

	gotoUrl = Url;
	if (ASBox_Type == 2 || ASBox_Type == 3 || ASBox_Type == 4) {
		ASBox_body.onclick = null;
		getElem("ASBox_openwin").style.display = "none";
		if (gotoUrl != 0) {
			getElem("ASBox_closeimg").onclick = function () {hideASBox();window.location.href = gotoUrl;};
		} else {
			getElem("ASBox_closeimg").onclick = function() {hideASBox();}
		}
	} else {
		ASBox_body.onclick = hideASBox;
		getElem("ASBox_openwin").onclick = function (){window.open(Content, "", "fullscreen,scrollbars");};
		getElem("ASBox_closeimg").onclick = null;
	}
	//定制背景效果.
	Effect_Type = Effect;
	isCenter	= Center;
	showEffect(Effect_Type);
	if (isCenter == true) {
		window.onscroll = maintPos;
		window.onresize = maintPos;
	}
	//屏蔽<select>
	hideSelects("hidden");
}
//显示背景效果
function showEffect(Effect_Type) {
	var box_w = getElem("ASBox_window");
	switch (Effect_Type) {
		case 0 :
			box_w.style.opacity		= 0.8;
			box_w.style.background	= "#FFFFFF";
			box_w.style.filter		= "alpha(opacity=75)";
			break;
		case 1 :
			box_w.style.opacity		= 0;
			box_w.style.filter		= "alpha(opacity=0)";
			break;
		case 2 :
			box_w.style.opacity		= 0.8;
			box_w.style.filter		= "alpha(opacity=80)";
			break;
		case 3 :
			box_w.style.opacity		= 0;
			box_w.style.filter		= "alpha(opacity=0)";
			setBGOpacity			= setOpacity;
			//修改"8"可以改变渐变程度
			for (var i=0; i<=8; i++) { setTimeout("setBGOpacity('ASBox_window',"+i+")",70*i); }
			break;
	}
	box_w.style.display = "";
	var pagesize		= new getPageSize();
	var scrollPos		= new getScrollPos();
	var ua = navigator.userAgent;

	if (ua.indexOf("MSIE ") != -1) {box_w.style.width = pagesize.width+"px";}
	/*else {box_w.style.width = pagesize.width-20+"px";}*/ // scrollbars removed! Hurray!
	//box_w.style.height = pagesize.height+scrollPos.scrollY+"px";
	//box_w.style.height = document.body.clientHeight+scrollPos.scrollY+"px";
	//box_w.style.height = document.body.scrollHeight+scrollPos.scrollY+"px";
	box_w.style.height = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight)+"px";

}
//显示loading的画面
function showIndicator() {
	var ASBox_p				= getElem("ASBox_progress");
	ASBox_p.style.display	= "";
	posToCenter(ASBox_p);
	ASBox_p.onclick = function() {hideASBox();hideIndicator();loadCancelled = true;}
}
//结束loading的画面
function hideIndicator() {
	var ASBox_p				= getElem("ASBox_progress");
	ASBox_p.style.display	= "none";
	ASBox_p.onclick			= null;
}
//设置透明度
function setOpacity(elemid,value) {
	var e = getElem(elemid);
	e.style.opacity = value/10;
	e.style.filter = "alpha(opacity=" + value*10 + ")";
}
//重新规范图片尺寸
function resizeImageToScreen(objImg) {
	var pagesize		= new getPageSize();
	var x				= pagesize.width  - 100;
	var y				= pagesize.height - 100;
	var IsDefaultSize	= false;
	if (objImg.width > x) {
		objImg.height	= objImg.height * (x/objImg.width);
		objImg.width	= x;
		if (objImg.height > y) {
			objImg.width	= objImg.width * (y/objImg.height);
			objImg.height	= y;
		}
	} else if (objImg.height > y) {
		objImg.width	= objImg.width * (y/objImg.height);
		objImg.height	= y;
		if (objImg.width > x) {
			objImg.height	= objImg.height * (x/objImg.width);
			objImg.width	= x;
		}
	}
	if (objImg.width < 375 && objImg.height < 155) {
		//objImg.width	= 375;
		//objImg.height	= 155;
		IsDefaultSize	= true;
	}
	objImg.DefaultSize = IsDefaultSize;
	return objImg;
}
//隐藏ASBox
function hideASBox() {
	var box_w			= getElem("ASBox_window");
	box_w.style.display = "none";
	var ASBox_body		= getElem("ASBox_body");
	ASBox_body.style.display = "none";
	getElem("ASBox_main").innerHTML = "";
	window.onscroll = null;
	//恢复<select>
	hideSelects("visible");
}
//隐藏<select>
function hideSelects(visibility) {
	selects = document.getElementsByTagName("select");
	for (var i=0; i<selects.length; i++) {
		selects[i].style.visibility = visibility;
	}
}
//保证对象随屏幕移动,并居中
function maintPos() {
	var ASBox_body	= getElem("ASBox_body");
	var box_w		= getElem("ASBox_window");
	var pagesize	= new getPageSize();
	var scrollPos	= new getScrollPos();
	var ua			= navigator.userAgent;

	if (ua.indexOf("MSIE ") != -1) {box_w.style.width = pagesize.width+"px";}

	/*else {box_w.style.width = pagesize.width-20+"px";}*/

	if (ua.indexOf("Opera/9") != -1) {box_w.style.height = document.body.scrollHeight+"px";}
	else {box_w.style.height = pagesize.height+scrollPos.scrollY+"px";}

	//box_w.style.height = document.body.scrollHeight+50+"px";

	posToCenter(ASBox_body);
}
//使对象居为屏幕中央
function posToCenter(elem) {
	var scrollPos	= new getScrollPos();
	var pageSize	= new getPageSize();
	var emSize		= new getElementSize(elem);
	var x			= Math.round(pageSize.width/2) - (emSize.width /2) + scrollPos.scrollX;
	var y			= Math.round(pageSize.height/2) - (emSize.height /2) + scrollPos.scrollY;
	elem.style.left	= x+"px";
	elem.style.top	= y+"px";
}
//计算滚动条像素
function getScrollPos() {
	var docElem		= document.documentElement;
	this.scrollX	= self.pageXOffset || (docElem&&docElem.scrollLeft) || document.body.scrollLeft;
	this.scrollY	= self.pageYOffset || (docElem&&docElem.scrollTop)  || document.body.scrollTop;
}
//取得页面尺寸
function getPageSize() {
	var docElem	= document.documentElement
	this.width	= self.innerWidth  || (docElem&&docElem.clientWidth)  || document.body.clientWidth;
	this.height	= self.innerHeight || (docElem&&docElem.clientHeight) || document.body.clientHeight;
}
//取得对象尺寸
function getElementSize(elem) {
	this.width	= elem.offsetWidth  || elem.style.pixelWidth;
	this.height = elem.offsetHeight || elem.style.pixelHeight;
}
//输出内容
function setASBoxContent(str) {
	getElem("ASBox_main").innerHTML			= "";
	getElem("ASBox_main").style.overflow	= "auto";
	getElem("ASBox_main").innerHTML			= str;
}
//documentgetElementById的简写函数
function getElem(elemId) {
	return document.getElementById(elemId);
}
//拖动窗口
function DragBox(o1,o2){
	o1.onselectstart = function() { return(false); };
	o1.onmousedown = function(e) {
		e = e || window.event;
		e.cancelBubble = true;
		var x = e.layerX || e.offsetX;
		var y = e.layerY || e.offsetY;
		document.onmousemove = function(e) {
			e = e || window.event;
			o2.style.top	= (e.clientY - y) + "px";
			o2.style.left	= (e.clientX - x) + "px";
		};
		document.onmouseup = function() { document.onmousemove = null; };
	};
}
//Ajax的简单应用
function createRequestObject() {
	var xmlhttp;
		/*@cc_on
	@if (@_jscript_version>= 5)
			try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
					try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
					catch (E) {xmlhttp = false;}
			}
	@else
		xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != "undefined") {
		try {xmlhttp = new XMLHttpRequest();} catch (e) {xmlhttp = false;}
	}
	return xmlhttp;
}
var http = createRequestObject();
