// Popup code
var gPopOpend = false;
var gPopupMask = null;
var gPopupContainer = null;
var gReturnFunc;
 
var gCancelFunc;
var gPopupIsShown = false;
var gHideSelects = false;
var gValue=0;
var gHideMsg = true;
var gArgv = new Array(); 

var Drag = {
	obj : null,
	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper) {
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e) {
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e){
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function(){
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e){
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

/**
 * Initializes popup code on load.	
 */
function initPopUp(callReturnFunc) {
	// Add the HTML to the body
	//callReturnFunc =('undefined' != typeof(callReturnFunc))?true:false;
	body = document.getElementsByTagName('body')[0];
	popmask = document.createElement('div');
	popmask.id = 'popupMask';
	popcont = document.createElement('div');
	popcont.id = 'popupContainer';
	popcont.innerHTML = '' +
		'<div id="popupInner">' +
			'<div id="popupTitleBar">' +
				'<div id="popupTitle"></div>' +
				'<div id="popupControls">' +
					'<img src="/templets/images/close.gif" onclick="hidePopWin('+callReturnFunc+',true);" />' +
				'</div>' +
			'</div>' +
			'<div id=popupMsg style="display:none"></div><div id=popupCont></div>' +
		'</div>';
	body.appendChild(popmask);
	body.appendChild(popcont);
	gPopupIsShown = true;
	gPopupMask = $("popupMask");
	gPopupContainer = $("popupContainer");
	
	// check to see if this is IE version 6 or lower. hide select boxes if so
	// maybe they'll fix this in version 7?
	var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
	if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
		gHideSelects = true;
	}
	// drag drop popup
	var theHandle = $("popupTitleBar");
	var theRoot   = $("popupContainer");
	Drag.init(theHandle, theRoot);
}

/**
 * @argument width - int in pixels
 * @argument height - int in pixels
 * @argument title - title to display
 * @argument txt - txt to display
 * @argument returnFunc - function to call when returning true from the window.
 * @argument callReturnFunc - 是否在关闭窗口里调用回调函数
*/
function showPopWin(width, height, title, txt, returnFunc, cancelFunc, argv, callReturnFunc, dofocus)
{
	if (gPopOpend==true)
	{
		return false;
	}
	else
	{
		gPopOpend = true;
	}
	gValue=0;
	if(gPopupIsShown) return false;
	if(title==null) title="系统消息";
	if(txt==null) txt="系统内容";
	initPopUp(callReturnFunc);
	$("popupMsg").style.display="none";

	gArgv=argv;
	var ti=$("popupTitle");
	ti.innerHTML='<b class="other_all oth_ico_1"></b>&nbsp;'+title+'';
	var cont=$("popupCont");
	cont.innerHTML=txt;

	gPopupMask.style.display = "block";
	gPopupContainer.style.display = "block";

	centerPopWin(width, height);
	
	gReturnFunc = returnFunc;
 
	gCancelFunc = cancelFunc;
	// for IE
	if (gHideSelects == true) {
		hideSelectBoxes();
	}

	if (dofocus)
	{
		$(dofocus).focus();
		var input_strlen = $(dofocus).value.length;
		var input_sel = $(dofocus).createTextRange();
		input_sel.move("character",input_strlen);
		input_sel.select();
	}
	else
	{
		var obj=$("popupCont").getElementsByTagName("INPUT");
		for(var k=0; k<obj.length; k++) {
			if(obj[k].value=="确定" || obj[k].value=="关闭") {
				obj[k].focus();
				break;
			}
		}
	}
}

// 窗口居中
function centerPopWin(width, height) {
	if (gPopupIsShown == true) {
		if (width == null || isNaN(width)) {
			width = gPopupContainer.offsetWidth;
		}
		if (height == null) {
			height = gPopupContainer.offsetHeight;
		}
		var contentWidth, contentHeight;
		var theBody = document.documentElement||document.body;
		var left,top;
		if(typeof(window.innerWidth)=='number')
		{fullWidth=window.innerWidth;fullHeight=window.innerHeight;}
		else if(theBody&&(theBody.clientWidth||theBody.clientHeight))
		{fullWidth=theBody.clientWidth;fullHeight=theBody.clientHeight;}
		
		if(theBody&&(theBody.scrollHeight||theBody.offsetHeight))
		{
			if(theBody.scrollHeight>theBody.offsetHeight)
			{contentWidth=theBody.scrollWidth;contentHeight=theBody.scrollHeight;}
			else
			{contentWidth=theBody.offsetWidth;contentHeight=theBody.offsetHeight;}
		}
		else 
		{contentWidth=fullWidth;contentHeight=fullHeight;}
		
		if(fullWidth<contentWidth) fullWidth=contentWidth;
		if(fullHeight<contentHeight) fullHeight=contentHeight;
		gPopupMask.style.height = fullHeight + "px";
		gPopupMask.style.width = fullWidth + "px";
        left=theBody.scrollLeft;
        if(left<theBody.clientLeft) left=theBody.clientLeft;
        top=theBody.scrollTop;

        if(height<150 || height>800) height=200;
        var centerTop, centerLeft;
        if(window.screen.availHeight>theBody.clientHeight) {
            centerTop=top+(theBody.offsetHeight-height)/2-50 + "px";
        }
        else centerTop=top+(window.screen.availHeight-height)/2-50 +"px";

		
        gPopupContainer.style.top = centerTop;
        gPopupContainer.style.left = left + (theBody.offsetWidth - width)/2+ "px";
        
        gPopupContainer.style.width = width + "px";
        $("popupMsg").style.width=$("popupTitle").style.width;
	}
}
 

/**
 * @关闭窗口
 * @argument callReturnFunc - bool - determines if we call the return function specified
 * @argument returnVal - anything - return value 
 */
function hidePopWin(callReturnFunc,gHideMsg) {
	gPopupIsShown = false;
	gPopOpend = false;
	
	if(gHideMsg == undefined) gHideMsg = true;
	if (gPopupMask == null) {
		return;
	}
	if (gHideMsg===false)
	{
		gPopupMask.style.display = "block";
		gPopupContainer.style.display = "block";
	}
	else
	{
		gPopupMask.style.display = "none";
		gPopupContainer.style.display = "none";
	}
	if (gHideSelects == true && gHideMsg== true) {
		displaySelectBoxes();
	}
	
	if (callReturnFunc == true && gReturnFunc != null) {
		gReturnFunc(gArgv);
	}
	
	if (callReturnFunc == false && gCancelFunc != null) {
		gCancelFunc(gArgv);
	}
}

function hideSelectBoxes() {
	var obj=document.getElementsByTagName("SELECT");
	for(var i=0; i<obj.length; i++) {
		obj[i].style.visibility="hidden";
	}
	var show=$("popupCont").getElementsByTagName("SELECT");
	for(var j=0; j<show.length; j++) {
		show[j].style.visibility="visible";
	}
}

function displaySelectBoxes() {
	var obj=document.getElementsByTagName("SELECT");
	for(var i=0; i<obj.length; i++) {
		obj[i].style.visibility="visible";
	}
}
