﻿/**
实现弹出可拖动窗体，限制背景的操作
2007-10-12日 李峰
*/
var gPopupMask = null;
var gPopupContainer = null;
var gPopFrame = null;
var gShadowBox = null;
var gReturnFunc;
var gPopupIsShown = false;

var gHideSelects = false;

var gpopBodyBox = null;

var gTitleName = "&nbsp;";
var gDefaultLogin="/js/loading.html";


var gTabIndexes = new Array();

var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");	


if (!document.all) {
	document.onkeypress = keyDownHandler;
}



/**
弹出窗口初始化	
 */
function initPopUp() {

	theBody = document.getElementsByTagName('BODY')[0];

	popmask = document.createElement('div');
	popmask.id = 'popupMask';
	popcont = document.createElement('div');
	popcont.id = 'popupContainer';
	var DivShow='<div id=ShadowBox style="display: block; z-index: -1; filter: progid:DXImageTransform.Microsoft.alpha(opacity=20) progid:DXImageTransform.Microsoft.Blur(pixelradius=6);">';

	popcont.innerHTML = '' +
		'<div id=popupInner >' +
			'<div id=popupBoxAll>'+
			    '<div id=popupTitleBar>' +
				    '<div id=popupTitle>'+gTitleName+'</div>' +
			    '</div>' +
			'</div>'+
			'<div id=popBodyBox>'+
			    '<iframe src='+gDefaultLogin+' style="width:100%;height:100%;background-color:transparent;" scrolling=no frameborder=0 allowtransparency=true id=popupFrame name=popupFrame width=100% height=100%></iframe>' +
			'</div>'+
			'<div  onclick="hidePopWin(false);" id=popCloseBox>' +
			'</div>' +
		'</div>'+DivShow;
	theBody.appendChild(popmask);
	theBody.appendChild(popcont);
	
	gPopupMask = document.getElementById("popupMask");
	
	gPopupContainer = document.getElementById("popupContainer");
	gPopFrame = document.getElementById("popupFrame");	
	gShadowBox = document.getElementById("ShadowBox");	
	
	gpopBodyBox= document.getElementById("popBodyBox");	
	

	var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
	if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
		gHideSelects = true;
	}
	
	var elms = document.getElementsByTagName('a');
	for (i = 0; i < elms.length; i++) {
		if (elms[i].className.indexOf("submodal") == 0) { 
            //为页面上的似如“<a class="submodal-200-200" href=http://www.google.com>google</a>”的a注册事件
			elms[i].onclick = function()
			{
				var width = 400;
				var height = 200;

				params = this.className.split('-');
				if (params.length == 3) {
					width = parseInt(params[1]);
					height = parseInt(params[2]);
				}
				showPopWin(this.href,this.href,width,height,null); return false;
			}
		}
	}
}
addEvent(window, "load", initPopUp);

 /**
    显示弹出窗口
	* 参数： width - 窗口宽度
	* 参数： height - 窗口高度
	* 参数： url - 要跳转的页面
	* 参数： returnFunc - 当窗口提交成功后要执行的，可给NULL值
	* 参数： showCloseBox - 是否显示关闭按键
	*/
function showPopWin(Title,url, width, height, returnFunc, showCloseBox) {
    
    
    document.getElementById("popupTitle").innerHTML = Title;

	if (showCloseBox == null || showCloseBox == true) {
		document.getElementById("popCloseBox").style.display = "block";
	} else {
		document.getElementById("popCloseBox").style.display = "none";
	}
	gPopupIsShown = true;
	disableTabIndexes();
	gPopupMask.style.display = "block";
	gPopupContainer.style.display = "block";

	centerPopWin(width, height);
	
	var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);

        
	gPopupContainer.style.width = width + "px";
	gPopupContainer.style.height = (height+titleBarHeight) + "px";
	
	
	gpopBodyBox.style.width = width + "px";
	
	gShadowBox.style.height = (height+titleBarHeight+2) + "px";
	gShadowBox.style.width = (width+8)+"px"
	
		
	setMaskSize();


	gPopFrame.style.width = parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";
	gPopFrame.style.height = (height) + "px";

	gPopFrame.src = url;
	
	gReturnFunc = returnFunc;

//	if (gHideSelects == true) {
//		hideSelectBoxes();
//	}
	
	window.setTimeout("setPopTitle();", 600);
	
	drag("popupContainer");
}

var gi = 0;
function centerPopWin(width, height) {
	if (gPopupIsShown == true) {
		if (width == null || isNaN(width)) {
			width = gPopupContainer.offsetWidth;
		}
		if (height == null) {
			height = gPopupContainer.offsetHeight;
		}

		var theBody = document.getElementsByTagName("BODY")[0];
		theBody.style.overflow = "hidden";
		
		var scTop = parseInt(theBody.scrollTop,10);
		var scLeft = parseInt(theBody.scrollLeft,10);
		
		gPopupMask.style.top = scTop + "px";
		gPopupMask.style.left = scLeft + "px";
		
		gShadowBox.style.top = (scTop-4) + "px";
		gShadowBox.style.left = (scLeft-10) + "px";
	
		setMaskSize();
		
		var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
		
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		
		gPopupContainer.style.top = (scTop + ((fullHeight - (height+titleBarHeight)) / 2)) + "px";
		gPopupContainer.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";

	}
}
addEvent(window, "resize", centerPopWin);
//addEvent(window, "scroll", centerPopWin);
window.onscroll = centerPopWin;

/**
设置屏蔽层大小
 */
function setMaskSize() {
	var theBody = document.getElementsByTagName("BODY")[0];
			
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();
	
	if (fullHeight > theBody.scrollHeight) {
		popHeight = fullHeight;
	} else {
		popHeight = theBody.scrollHeight;
	}
	
	if (fullWidth > theBody.scrollWidth) {
		popWidth = fullWidth;
	} else {
		popWidth = theBody.scrollWidth;
	}
	
	gPopupMask.style.height = popHeight + "px";
	gPopupMask.style.width = popWidth + "px";
	

}

/**
隐藏弹出窗口 
 */
function hidePopWin(callReturnFunc) {
	gPopupIsShown = false;
	var theBody = document.getElementsByTagName("BODY")[0];
	theBody.style.overflow = "";
	restoreTabIndexes();
	if (gPopupMask == null) {
		return;
	}
	gPopupMask.style.display = "none";
	gPopupContainer.style.display = "none";
	if (callReturnFunc == true && gReturnFunc != null) {
		gReturnFunc(window.frames["popupFrame"].returnVal);
	}
	gPopFrame.src = gDefaultLogin;
//	if (gHideSelects == false) {
//		displaySelectBoxes();alert(gHideSelects);
//	}
}

/**
设置弹出窗口标题
 */
function setPopTitle() {
	return;
	if (window.frames["popupFrame"].document.title == null) {
		window.setTimeout("setPopTitle();", 10);
	} else {
		document.getElementById("popupTitle").innerHTML = window.frames["popupFrame"].document.title;
	}
}

// 按键事件处理
function keyDownHandler(e) {
    if (gPopupIsShown && e.keyCode == 9)  return false;
}

// 禁止Tab建切换，适用IE
function disableTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				gTabIndexes[i] = tagElements[k].tabIndex;
				tagElements[k].tabIndex="-1";
				i++;
			}
		}
	}
}
// 恢复tab建切换，适用IE
function restoreTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				tagElements[k].tabIndex = gTabIndexes[i];
				tagElements[k].tabEnabled = true;
				i++;
			}
		}
	}
}


/**隐藏select控件*/
function hideSelectBoxes() {
	for(var i = 0; i < document.forms.length; i++) {
		for(var e = 0; e < document.forms[i].length; e++){
			if(document.forms[i].elements[e].tagName == "SELECT") {
				document.forms[i].elements[e].style.visibility="hidden";
			}
		}
	}
	return false;
}
/**显示select控件*/
function displaySelectBoxes() {
	for(var i = 0; i < document.forms.length; i++) {
		for(var e = 0; e < document.forms[i].length; e++){
			if(document.forms[i].elements[e].tagName == "SELECT") {
			document.forms[i].elements[e].style.visibility="visible";//alert('displaySelectBoxes '+document.forms[i].elements[e].style.visibility);
			}
		}
	}
}


function drag(o,s)  
{ 
    if (typeof o == "string") o = document.getElementById(o);  
    o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;  
    o.orig_y = parseInt(o.style.top) - document.body.scrollTop;  
    o.orig_index = o.style.zIndex;  
          
    o.onmousedown = function(a)
    {
        this.style.cursor = "move";  
        this.style.zIndex = 10000;  
        
        var d=document;  
        if(!a)a=window.event;  
        var x = a.clientX+d.body.scrollLeft-o.offsetLeft;  
        var y = a.clientY+d.body.scrollTop-o.offsetTop;  
        //author: www.longbill.cn  
        d.ondragstart = "return false;"  
        d.onselectstart = "return false;"  
        d.onselect = "document.selection.empty();"  
                  
        if(o.setCapture)  
            o.setCapture();  
        else if(window.captureEvents)  
            window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);  

        d.onmousemove = function(a) 
        {
            if(!a)a=window.event;  
            o.style.left = a.clientX+document.body.scrollLeft-x;  
            o.style.top = a.clientY+document.body.scrollTop-y;  
            o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;  
            o.orig_y = parseInt(o.style.top) - document.body.scrollTop;
        }  

        d.onmouseup = function()
        {   //alert("d.onmouseup");
            if(o.releaseCapture)  
                o.releaseCapture();  
            else if(window.captureEvents)  
                window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);  
            d.onmousemove = null;  
            d.onmouseup = null;  
            d.ondragstart = null;  
            d.onselectstart = null;  
//            d.onselect = null;  
            o.style.cursor = "normal";  
//            o.style.zIndex = o.orig_index;   
            hideSelectBoxes(); 
            return false;          
        }   
        hideSelectBoxes();
    }  
      
    if (s)  
    {  
        var orig_scroll = window.onscroll?window.onscroll:function (){};  
        window.onscroll = function ()  
        {  
            orig_scroll();  
            o.style.left = o.orig_x + document.body.scrollLeft;  
            o.style.top = o.orig_y + document.body.scrollTop;  
        }  
    } 
    hideSelectBoxes(); 
}  