﻿// 初始化引用窗口
var AUTH_OPEN_MODE= { };
AUTH_OPEN_MODE.WINDOW = 1;
AUTH_OPEN_MODE.FRAME = 2;

function initUserRefWindow()
{
	// 登录窗口回调, 非用户方法, 用来清理现场, 还原调用前数据
	// 参数:
	//		errorCode: 返回的错误代码
	// 返回值:
	//		无
	function logonComplete(errorCode)
	{
		if (user.authWindow.onClose != null)
			user.authWindow.onClose(errorCode);

		if (user.authWindow.window != null)
			user.authWindow.window.close();

		if (window.frames['authWindow'] != null)
			document.all['authWindow'].parentElement.removeChild(openObj);
	}

	// ------------------------------------------------------------------------
	// 授权弹出窗口对象
	function authWindow()
	{
		// 弹出验证窗口
		// 参数:
		// 		url:	要打开的 url
		// 		mode:	打开方式
		// 返回值:
		// 		mode 为 AUTH_OPEN_MODE.WINDOW 时为打开的窗口对象;
		// 		mode 为 AUTH_OPEN_MODE.FRAME 时为一个 iframe 对象
		this.open = function (url, callback, mode)
		{
			if (mode == null)
				mode = AUTH_OPEN_MODE.WINDOW;

			var obj;
			switch (mode)
			{
				case AUTH_OPEN_MODE.WINDOW:
					obj = document.createElement('a');
					obj.href = url;
					obj.target = 'authWindow';
					if (obj.click != null)
					{
						obj.style['display'] = 'block';
						document.body.appendChild(obj);
						obj.click();
					}
					else
					{
						var dispatch = document.createEvent("HTMLEvents")
						dispatch.initEvent("click", true, true);
						obj.dispatchEvent(dispatch);
					}
					var authWindow = window.open('', 'authWindow');
					window['user']['authWindow']['window'] = authWindow;

					break;

				case AUTH_OPEN_MODE.FRAME:
					obj = window.all['authWindow'];
					if (obj == null)
					{
						obj = document.createElement("iframe");
						obj.id = obj.name = 'authWindow';
					}
					obj.src = url;

					break;
			}
			this.onClose = callback;
			return obj;
		}

		// onClose 回调
		// 传入一个接受 1 个参数的方法
		// 如 loggedOn(errorCode);
		// errorCode 为返回的错误代码, 0 为登录成功
		this.onClose = null;
	}

	window['user'] = {};
	window['user']['logonComplete'] = logonComplete;
	window['user']['authWindow'] = new authWindow;
	window['user']['authWindow']['window'] = null;
}

// 初始化验证窗口
function initUserAuthWindow()
{
	// 登录窗口回调, 非用户方法
	function logonComplete(errorCode) {
		if (window.opener != null)
		{
			window.opener.user.logonComplete(errorCode);
		}
		else
		{
			if (window.parent !== window)
				window.parent.user.logonComplete(errorCode);
		}
	}

	window['user'] = {}
	window['user']['logonComplete'] = logonComplete;
}

var USER_PANE = {};
USER_PANE.LONGIN = 1;
USER_PANE.REGISTER = 2;

// 显示登录/注册窗口
// 参数:
// 		USER_PANE 枚举
// 返回值:
//		无
function showUserPane(loginOrRegister)
{
	var obj;
	if (loginOrRegister == USER_PANE.LONGIN)
		obj = document.all["loginPane"];
	else if (loginOrRegister == USER_PANE.REGISTER)
		obj = document.all['registerPane'];
	else
		throw new Error('showLoginPane: Invalid argument!');

	closeUserPane();
	adjustUserPanePos();

	if (window.addEventListener != null)
		window.addEventListener('scroll', adjustUserPanePos, false);
	else if (window.attachEvent != null)
		window.attachEvent('onscroll', adjustUserPanePos);

	obj.style["display"] = 'block';
	adjustUserPanePos();
}

// 关闭登录/注册窗口
function closeUserPane()
{
	document.all["loginPane"].style["display"] = 'none';
	document.all['registerPane'].style['display'] = 'none';

	if (window.removeEventListener != null)
		window.removeEventListener('scroll', adjustUserPanePos, false);
	else if (window.detachEvent)
		window.detachEvent('onscroll', adjustUserPanePos);
}

// 滚动后修正登录/注册窗口的位置
function adjustUserPanePos()
{
	// 获取视口宽高
	var windowWidth = 0, windowHeight = 0;
	if (typeof (window.innerWidth) == 'number')
	{
		//Non-IE
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	}
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		//IE 6+ in 'standards compliant mode'
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body && (document.body.clientWidth || document.body.clientHeight))
	{
		//IE 4 compatible
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// 获取滚动偏移
	var scrOfX = 0, scrOfY = 0;
	if (typeof (window.pageYOffset) == 'number')
	{
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	}
	else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
	{
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	}
	else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
	{
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}

	var loginPane = document.all["loginPane"];
	loginPane.style['margin-left'] = (windowWidth - loginPane.offsetWidth) / 2 + scrOfX + "px";
	loginPane.style['margin-top'] = (windowHeight - loginPane.offsetHeight) / 2 + scrOfY + "px";

	var registerPane = document.all['registerPane'];
	registerPane.style['margin-left'] = (windowWidth - registerPane.offsetWidth) / 2 + scrOfX + "px";
	registerPane.style['margin-top'] = (windowHeight - registerPane.offsetHeight) / 2 + scrOfY + "px";
}
