/** 用于在当前页面用Div打开一个新页面                           **/
/** 打开页面的方法为divWin.open(arg0,arg1,...);                **/
/** 参数详见方法说明，关闭页面的方法为divWin.close();            **/
/** 如果是在被打开的页面来关闭自己方法为parent.divWin.close();   **/
/** author:jzhou                                              **/
/** date:2009-1-6                                             **/

var DivWindow = Class.create();
DivWindow.IE = /MSIE/.test(window.navigator.userAgent);

DivWindow.prototype = {

	initialize : function () {
		var divWin = this;
		divWin.moving = false;
		divWin.pX = 0;
		divWin.pY = 0;
		divWin.draging = false;
		divWin.height = 300;
		divWin.width = 500;
		divWin.dpX = 0;
		divWin.dpY = 0;
		divWin.isRefreshParent = false;
		divWin.mainDiv = document.createElement("div");
		with (divWin.mainDiv.style) {
			display = "none";
			zIndex = 9998;
			top = 0;
			left = 0;
			position = "absolute";
			backgroundColor = "#000000";
			filter = "alpha(opacity=15)";
			opacity = "0.15";
		}
		document.body.appendChild(divWin.mainDiv);
		
		divWin.contentDiv = document.createElement("div");
		with (divWin.contentDiv.style) {
			display = "none";
			zIndex = 9999;
			position = "absolute";
			backgroundColor = "#FFFFFF";
		}
		document.body.appendChild(divWin.contentDiv);
		
		var strFrame = '<TABLE class="divwinmaintable" cellpadding="0" cellspacing="1" width="100%" height="100%">';
		strFrame += '		<TR>';
		strFrame += '			<TD height="25" class="divwintitbg">';
		strFrame += '				<TABLE cellSpacing=0 cellPadding=0 width="100%" height="100%" border="0">';
		strFrame += '					<TR>';
		strFrame += '						<TD width="9"></TD><TD width="12" height="12" class="divwintitarrow"></TD><TD width="9"></TD>';
		strFrame += '						<TD id="divWinTitle" class="divwintitle"></TD>';
		strFrame += '						<TD  id="divWinCloseButton" width="12" height="12" title="&#20851;&#38381;" class="divwinclose"></TD><TD width="9"></TD>';
		strFrame += '					</TR>';
		strFrame += '				</TABLE>';
		strFrame += '			</TD>';
		strFrame += '		</TR>';
		strFrame += '		<TR>';
		strFrame += '			<TD>';
		strFrame += '				<TABLE cellSpacing=0 cellPadding=0 width="100%" height="100%" border=0>';
		strFrame += '					<TR>';
		strFrame += '						<TD><iframe src="" style="width:100%;height:100%" frameborder="0" id="divWinPageFrame"></iframe></TD>';
		strFrame += '					</TR>';
		strFrame += '					<TR>';
		strFrame += '						<TD height="7px" class="divwinstatebar">';
		strFrame += '							<TABLE cellSpacing=0 cellPadding=0 width="7px" height="100%" border="0" align="right">';
		strFrame += '								<TR>';
		strFrame += '									<TD id="divWinStateBar" class="divwinls"></TD>';
		strFrame += '								</TR>';
		strFrame += '							</TABLE>';
		strFrame += '						</TD>';
		strFrame += '					</TR>';
		strFrame += '				</TABLE>';
		strFrame += '			</TD>';
		strFrame += '		</TR>';
		strFrame += '	</TABLE>';

		divWin.contentDiv.innerHTML = strFrame;
		divWin.closeButton = document.getElementById("divWinCloseButton");
		Object.addEvent(divWin.closeButton, ["onclick"], divWin.close.bind(divWin));
		divWin.title = document.getElementById("divWinTitle");
		Object.addEvent(divWin.title, ["onmousedown"], divWin.beginMove.bind(divWin));
		Object.addEvent(divWin.title, ["onmousemove"], divWin.move.bind(divWin));
		Object.addEvent(divWin.title, ["onmouseup"], divWin.endMove.bind(divWin));
		divWin.pageFrame = document.getElementById("divWinPageFrame");
		divWin.stateBar = document.getElementById("divWinStateBar");
		Object.addEvent(divWin.stateBar, ["onmousedown"], divWin.beginDrag.bind(divWin));
		Object.addEvent(divWin.stateBar, ["onmousemove"], divWin.drag.bind(divWin));
		Object.addEvent(divWin.stateBar, ["onmouseup"], divWin.endDrag.bind(divWin));
	},
	/**
	 * 参数说明
	 * url 要打开的页面的地址
	 * title 页面的标题，默认为“新页面”
	 * height 打开页面的高度，默认为 300
	 * width 打开页面的宽度，默认为500
         * refreshParent 是否在关闭时刷新父页面,默认不刷新
	 * keepParentAlive 是否保持父页面能活动，默认不能活动
	 * position 窗口的位置0中间 1左上 2 右上 3左下 4右下 5中上 6中下，默认为0
	 */
	open : function (url, title, height, width, refreshParent, keepParentAlive, position) {
		var divWin = this;
		var winPosition = 0;
		if (title) {
			divWin.title.innerHTML = title;
		} else {
			divWin.title.innerHTML = "&#26032;&#39029;&#38754;";
		}
		if (height) {
			divWin.height = parseInt(height);
		}
		if (width) {
			divWin.width = parseInt(width);
		}
		divWin.contentDiv.style.height = divWin.height;
		divWin.contentDiv.style.width = divWin.width;
		if (position) {
			winPosition = position;
		} 
		switch (position) {
			case 0 :
				divWin.contentDiv.style.top = (window.document.body.clientHeight - divWin.height) / 2 - 30;
				divWin.contentDiv.style.left = (window.document.body.clientWidth - divWin.width) / 2;
				break;
			case 1 :
				divWin.contentDiv.style.top = 0;
				divWin.contentDiv.style.left = 0;
				break;
			case 2 :
				divWin.contentDiv.style.top = 0;
				divWin.contentDiv.style.left = window.document.body.clientWidth - divWin.width;
				break;
			case 3 :
				divWin.contentDiv.style.top = window.document.body.clientHeight - divWin.height;
				divWin.contentDiv.style.left = 0;
				break;
			case 4 :
				divWin.contentDiv.style.top = window.document.body.clientHeight - divWin.height;
				divWin.contentDiv.style.left = window.document.body.clientWidth - divWin.width;
				break;
			case 5:
				divWin.contentDiv.style.top = 0;
				divWin.contentDiv.style.left = (window.document.body.clientWidth - divWin.width) / 2;
				break;
			case 6:
				divWin.contentDiv.style.top = window.document.body.clientHeight - divWin.height;
				divWin.contentDiv.style.left = (window.document.body.clientWidth - divWin.width) / 2;
				break;
			default :
				divWin.contentDiv.style.top = (window.document.body.clientHeight - divWin.height) / 2 - 30;
				divWin.contentDiv.style.left = (window.document.body.clientWidth - divWin.width) / 2;
		} 
		if (parseInt(divWin.contentDiv.style.top) < 0){
			divWin.contentDiv.style.top = 0;
		}
		divWin.mainDiv.style.width = document.body.scrollWidth + "px";
		if (document.body.clientHeight > document.body.scrollHeight) {
			divWin.mainDiv.style.height = document.body.clientHeight + "px";
		} else {
			divWin.mainDiv.style.height = document.body.scrollHeight + "px";
		}
		divWin.pageFrame.src = url + (url.indexOf("?") == -1 ? "?" : "&") + new Date().getTime() + (10000 + parseInt(Math.random() * 10000));
		if (!keepParentAlive) {
			divWin.mainDiv.style.display = "";
		}
                if (refreshParent) {
                    divWin.isRefreshParent = true;
                }
		divWin.contentDiv.style.display = "";
	},
	
	/**
	 * 关闭窗口，注意：如果是在被打开的页面上关闭自己需要调用parent.divWin.close();
	 */
	close : function () {
		var divWin = this ;
		divWin.mainDiv.style.display = "none";
		divWin.contentDiv.style.display = "none";
		divWin.pageFrame.src = "";
                if (divWin.isRefreshParent) {
                    parent.document.execCommand("Refresh");
                }
	},

	/**
	 * 移动开始
	 */
	beginMove : function (e) {
		var divWin = this ;
		if (DivWindow.IE) {
			divWin.title.setCapture();
			divWin.pX = event.x - divWin.contentDiv.style.pixelLeft;
			divWin.pY = event.y - divWin.contentDiv.style.pixelTop; 
		} else {
			document.addEventListener("mousemove", divWin.move.bind(divWin), true);
			divWin.pX = e.clientX - parseInt(divWin.contentDiv.style.left);
			divWin.pY = e.clientY - parseInt(divWin.contentDiv.style.top); 
		}
		divWin.moving = true;
	},
	
	/**
	 * 移动中
	 */
	move : function (e) {
		var divWin = this ;
		if (divWin.moving) {
			if (DivWindow.IE) {
				divWin.contentDiv.style.left = event.x - divWin.pX;
				if (event.y - divWin.pY > 0){
					divWin.contentDiv.style.top = event.y - divWin.pY;
				}else{
					divWin.contentDiv.style.top = 0;
				}
			} else {
				divWin.contentDiv.style.left = e.clientX - divWin.pX;
				if (e.clientY - divWin.pY > 0){
					divWin.contentDiv.style.top = e.clientY - divWin.pY;
				}else{
					divWin.contentDiv.style.top = 0;
				}
			}
		}
	},
	
	/**
	 * 移动结束
	 */
	endMove : function (e) {
		var divWin = this ;
		if (DivWindow.IE) {
			divWin.title.releaseCapture();
		} else {
  			document.removeEventListener("mousemove", divWin.move.bind(divWin), true);
                }
		divWin.moving = false;
	},

	beginDrag : function (e) {
		var divWin = this ;
		if (DivWindow.IE) {
			divWin.stateBar.setCapture();
			divWin.dpX = event.x;
			divWin.dpY = event.y; 
		} else {
			document.addEventListener("mousemove", divWin.drag.bind(divWin), true);
			document.addEventListener("mouseup", divWin.endDrag.bind(divWin), true);
			divWin.dpX = e.clientX;
			divWin.dpY = e.clientY; 
		}
		divWin.draging = true;
	},
	
	drag : function (e) {
		var divWin = this ;
		if (divWin.draging) {
			if (DivWindow.IE) {
			    try{
                                    divWin.contentDiv.style.width = divWin.width + event.x - divWin.dpX;
                                    divWin.contentDiv.style.height = divWin.height + event.y - divWin.dpY;
				} catch (e) {
				}
			} else {
				divWin.contentDiv.style.width = divWin.width + e.clientX - divWin.dpX;
				divWin.contentDiv.style.height = divWin.height + e.clientY - divWin.dpY;
			}
		}
	},
	
	endDrag : function (e) {
		var divWin = this ;
		if (DivWindow.IE) {
			divWin.stateBar.releaseCapture();
		} else {
			document.removeEventListener("mousemove", divWin.drag.bind(divWin), true);
			document.removeEventListener("mouseup", divWin.endDrag.bind(divWin), true);
		}
		divWin.width = parseInt(divWin.contentDiv.style.width);
		divWin.height = parseInt(divWin.contentDiv.style.height);
		divWin.draging = false;
	}
}

var divWin;

Object.addEvent(window, ["onload"], function () {
	divWin = new DivWindow;
});


