/////////////////////////////////////////////////////////////

/////////////// example code for pop win  ///////////////////
//<div id='popWin_TEST' class='popWin'>
//	<div id='popHeader_TEST' class='popWin_head'>
//	<a class='popWin_control' onclick="closePopWin('TEST');return false;" href='javascript:void(0);'> </a>
//	</div>
//	<div style='position: relative'>
//		<iframe id='popFrame_TEST' name='TEST' class='popFrame' frameborder='0' scrolling='no' src=''>
//		</iframe>
//    </div>
//</div>
/////////////// example code for pop win  ///////////////////

////// pop win function for parent //////////////////////////

//1.openPopWin(name, url, title, cssClass)  打开Pop窗口, cssClass=small|medium|large
//2.closePopWin(name) 关闭Pop窗口
//3.getPopDocument(name) 获取Pop窗口Document，暂不提供
//4.afterClosePopWin(name) pop关闭时自动回调此方法

////// pop win function for child  /////////////////////////

//1.getPopName()  如果为pop返回name，否则为undefined
//2.closePopWin() 关闭当前pop窗口
//3.getParentDocument() 获取Pop的父窗口
//4.autoFitPopWin()  调用调整窗口方法

////////////////////////////////////////////////////////////
function getPopName() {
	try {
		if (__name_pop_win) {
			return __name_pop_win;
		}
	}
	catch (e) {
	}
}
function getPopParentName() {
	try {
		if (__name_pop_parent) {
			return __name_pop_parent;
		}
	}
	catch (e) {
	}
}
function getParentWin() {
	var parentPopName = getPopParentName();
	if (parentPopName != undefined) {
		var parentPop = getPopWin(parentPopName, window.parent);
		if (parentPop != undefined) {
			return parentPop.ownerDocument.parentWindow;
		}
	}
	return parent;
}
function getPopWin(name, win) {
	if (win == undefined) {
		win = window;
	}
	if (name == undefined) {
		name = getPopName();
	}
	var openPop = win.document.getElementById("popWin_" + name);
	if (openPop == undefined && win.parent != win) {
		openPop = getPopWin(name, win.parent);
	}
	return openPop;
}
function getPopFrm(popWin) {
	var name = popWin.getAttribute("popWin");
	return popWin.ownerDocument.getElementById("popFrame_" + name);
}
function getPopDocument(popWin) {
	return getPopFrm().contentWindow.document;
}
function getPopHeader(popWin) {
	var name = popWin.getAttribute("popWin");
	var popHeader = popWin.ownerDocument.getElementById("popHeader_" + name);
	return popHeader;
}
function openPopWin2(name, url, cssClass) {
	openPopWin(name, url, cssClass, "pop");
}
function openPopWin(name, url, cssClass, title) {
	var win = window;  
	//var win = top;
	if(name!=undefined && name.indexOf("popw_")==-1){
	  name = "popw_".concat(name);
	}
	
	var openPop = getPopWin(name, win);
	var frmObj;
	var parentName = getPopName();
	if (openPop == undefined) {
		openPop = newPopWin(name, win);
		frmObj = getPopFrm(openPop);
		parentName = getPopName();
	} else {
		frmObj = getPopFrm(openPop);
		if (frmObj.contentWindow.getPopParentName) {
			parentName = frmObj.contentWindow.getPopParentName();
		}
	}
	frmObj.setAttribute("popWin", name);
	if (cssClass != undefined) {
		if (cssClass == "small") {
			cssClass = "popsmall";
		} else {
			if (cssClass == "medium") {
				cssClass = "popmedium";
			} else {
				if (cssClass == "large") {
					cssClass = "poplarge";
				}
			}
		}
		jscss("add", openPop, cssClass);
		jscss("add", frmObj, cssClass);
	}
	if (url != undefined) {
		var appendUrl = "decorator=pop&POP_WIN=" + name;
		if (parentName != undefined) {
			appendUrl += "&POP_PARENT=" + parentName;
		}
		if (url.lastIndexOf("?") > 0) {
			url += "&" + appendUrl;
		} else {
			url += "?" + appendUrl;
		}
		frmObj.src = url;
	}
	
	// show
	drag(openPop, getPopHeader(openPop));
	openPop.style.visibility = "visible";
	autofixPopPosition(openPop);
	openPop.focus();
}
function autofixPopPosition(openPop, win) {
	if (win == undefined) {
		win = window;
	}
	if (openPop && !openPop.getAttribute("hasdrag")) {
		var pageSize = getPageXY(win);
		openPop.style.left = (pageSize[0] - openPop.offsetWidth) / 2 + "px";
		var popTop = (win.document.documentElement.scrollTop ? win.document.documentElement.scrollTop : win.document.body.scrollTop + (pageSize[3] - openPop.offsetHeight) / 2);
		if (popTop < 0) {
			popTop = 0;
		}
		openPop.style.top = popTop + "px";
	}
}
function setPopTitle(title) {
	var openPop = getPopWin();
	if (openPop == undefined || openPop == null) {
		return;
	}
	var popHerder = getPopHeader(openPop);
	popHerder.getElementsByTagName("div")[0].innerHTML = "<h1>" + title + "</h1>";
}
 

// create new pop
function newPopWin(name, win) {
	var html = getPopWinHtmlTemplate();
	html = html.replace(new RegExp("#POP_NAME", "g"), name);
	if (win == undefined) {
		win = window;
	}
	var el = win.document.getElementById("mainXX");
	if (el == undefined) {
		el = win.document.body;
	}
	insertHtml("beforeend", el, html);
	var popWin = win.document.getElementById("popWin_" + name);
	popWin.setAttribute("popWin", name);
	return popWin;
}
function getPopWinHtmlTemplate() {
	var html;
   //html = "<div id='popWin_#POP_NAME' class='popWin'><table border='0' cellspacing='0' cellpadding='0'><tbody><tr><td class='tl'></td><td class='tm'><div id='popHeader_#POP_NAME' class='popWin_head'><div id='popTitle_#POP_NAME' class='popWin_title'></div></div></td><td class='tr'><a class='popWin_control' onclick="closePopWin('#POP_NAME');return false;" href='javascript:void(0);'></a></td></tr><tr><td class='ml'></td><td class='mm'><div style='position: relative'><iframe id='popFrame_#POP_NAME' name='#POP_NAME' class='popFrame' frameborder='0' scrolling='no' src=''></iframe></div></td><td class='mr'></td></tr><tr><td class='bl'></td><td class='bm'></td><td class='br'></td></tr></tbody></table></div>";
	html = "<div id='popWin_#POP_NAME' class='popWin'><table border='0' cellspacing='0' cellpadding='0'><tbody><tr><td class='tl'></td><td class='tm'></td><td class='tr'></td></tr><tr><td class='ml'></td><td class='mm'><div id='popHeader_#POP_NAME' class='popWin_head'><a class='popWin_control' onclick=\"closePopWin('#POP_NAME');return false;\" href='javascript:void(0);'></a><div id='popTitle_#POP_NAME' class='popWin_title'></div></div><div style='position: relative'><iframe id='popFrame_#POP_NAME' name='#POP_NAME' class='popFrame' frameborder='0' scrolling='no' src=''></iframe></div></td><td class='mr'></td></tr><tr><td class='bl'></td><td class='bm'></td><td class='br'></td></tr></tbody></table></div>";
   //html = "<div id='popWin_#POP_NAME' class='popWin'><div id='popHeader_#POP_NAME' class='popWin_head'> <span class='pop_fillet_left'></span><span class='pop_close'><input type='button' class='popClose' onclick='closePopWin("#POP_NAME");' /></span><span class='pop_fillet_right'></span></div><table width='100%' border='0' cellpadding='0' cellspacing='0'>  <tr>    <td class='pop_left_bor'></td>    <td><div style='position: relative'>    <iframe id='popFrame_#POP_NAME' name='#POP_NAME' class='popFrame' frameborder='0' scrolling='no' src=''></iframe>  </div>      </td>    <td class='pop_right_bor'></td>  </tr></table><div class='popFooter'><span class='popFooter_fillet_left'></span><span class='popFooter_fillet_right'></span></div></div>";
   //html = "<div id='popWin_#POP_NAME'  class='popWin'>  <table cellspacing='0' cellpadding='0' border='0'>    <tbody>      <tr>        <td class='pop_fillet_left'></td>        <td class='pop_fillet_con'> <div class='popWin_head' id='popHeader_#POP_NAME'>          <input type='button' onclick='closePopWin("#POP_NAME");' class='popClose'/>        </div></td>        <td class='pop_fillet_right'></td>      </tr>      <tr>        <td class='pop_left_bor'></td>        <td >         <div style='position: relative;'>          <iframe frameborder='0' scrolling='no' src='' class='popFrame' name='#POP_NAME' id='popFrame_#POP_NAME' style='height: 195px;'>          </iframe>        </div></td>        <td class='pop_right_bor'></td>      </tr>      <tr>        <td class='popFooter_fillet_left'></td>        <td class='popFooter_fillet_con'></td>        <td class='popFooter_fillet_right'></td>      </tr>    </tbody>  </table></div>";
	return html;
}



// close pop window
function closePopWin(name, win) {
	if (win == undefined) {
		win = window;
	}
	if (name == undefined) {
		if (__name_pop_win) {
			name = __name_pop_win;
			win = parent;
		} else {
			return;
		}
	}
	if(name!=undefined && name.indexOf("popw_")==-1){
	  name = "popw_".concat(name);
	}
	var openPop = getPopWin(name, win);
	if (openPop) {
		openPop.style.visibility = "hidden";
		var frmObj = getPopFrm(openPop);
		frmObj.src = "";
		try {
			frmObj.contentWindow.parent.afterClosePopWin(name);
		}
		catch (e) {
		}
	}
}
function autoFitPopWin() {
  //alert("iframeAutoFit:"+iframeAutoFit);
	if (iframeAutoFit) {
		iframeAutoFit();
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// p-popWin, h-hoter, r-range
function drag(p, h, r) {
	if (h == undefined) {
		h = p;
	}
	//fix firefox, close button
	h.firstChild.onmousedown = function () {
		return false;
	};
	//fix firefox, title div
	h.lastChild.onmousedown = function () {
		return false;
	};
	h.onmousedown = function (a) {
		var d = document;
		if (!a) {
			a = window.event;
		}
		var x = p.offsetLeft, y = p.offsetTop;
		var x1 = a.pageX ? a.pageX : a.clientX, y1 = a.pageY ? a.pageY : a.clientY;
		if (h.setCapture) {
			h.setCapture();
		} else {
			if (window.captureEvents) {
				window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
			}
		}
		d.onmousemove = function (a) {
			if (!a) {
				a = window.event;
			}
			var x2 = a.pageX ? a.pageX : a.clientX, y2 = a.pageY ? a.pageY : a.clientY;
			var tx = x + x2 - x1, ty = y + y2 - y1;
			if (tx < 0) {
				tx = 0;
			}
			if (ty < 0) {
				ty = 0;
			}
			if (r == undefined) {
				p.style.left = tx + "px";
				p.style.top = ty + "px";
			} else {
				var left = (tx < r[0] ? r[0] : tx > r[1] ? r[1] : tx);
				var top = (ty < r[2] ? r[2] : ty > r[3] ? r[3] : ty);
				if (left > 0) {
					if (left > 800) {
						left = 800;
					}
				} else {
					left = 0;
				}
				if (top < 100) {
					top = 100;
				}
				p.style.left = left + "px";
				p.style.top = top + "px";
			}
			p.setAttribute("hasdrag", "true");
			return true;
		};
		d.onmouseup = function () {
			if (h.releaseCapture) {
				h.releaseCapture();
				//alert("h.releaseCapture");
			} else {
				if (window.captureEvents) {
				    //alert("window.captureEvents");
					window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
				}
			}
			//alert("d.onmouseup");
			d.onmousemove = null;
			d.onmouseup = null;
			return true;
		};
		return false;
	};
}
/**************************************************** 
获取坐标 
*******************************************************/
function getPageXY(win) {
	if (win == undefined) {
		win = window;
	}
	var xScroll, yScroll;
	if (win.innerHeight && win.scrollMaxY) {
		xScroll = win.document.body.scrollWidth;
		yScroll = win.innerHeight + win.scrollMaxY;
	} else {
		if (win.document.body.scrollHeight > win.document.body.offsetHeight) {
			xScroll = win.document.body.scrollWidth;
			yScroll = win.document.body.scrollHeight;
		} else {
			xScroll = win.document.body.offsetWidth;
			yScroll = win.document.body.offsetHeight;
		}
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else {
		if (win.document.documentElement && win.document.documentElement.clientHeight) {
			windowWidth = win.document.documentElement.clientWidth;
			windowHeight = win.document.documentElement.clientHeight;
		} else {
			if (win.document.body) {
				windowWidth = win.document.body.clientWidth;
				windowHeight = win.document.body.clientHeight;
			}
		}
	}
	if (yScroll < windowHeight) {
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	if (xScroll < windowWidth) {
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	var arrayPageXY = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
	return arrayPageXY;
}


