//登录右侧闪动图片
function showFaces()
{
	for (var i=1; i<=36; i++)
	{
		loadImage(i, urls[i-1], titles[i-1], cids[i-1]);
	}

	var int_temp = window.setInterval("appearFaces()",500)
}

function loadImage(id, url, title, cid)
{
	var n_url = 'http://upload.xwg.cc/uploadfiles/'+url;
	var img = new Image();
	img.src = n_url;

	if (img.complete)
	{
		faceHtml(id, url, title, cid);
		return;
	}

	img.onload = function()
	{
		faceHtml(id, url, title, cid);
	};
}

function faceHtml(id,  url, title, cid)
{
	$('face_'+id).innerHTML = '<a href="/Space/index/cid/'+cid+'/" title="'+title+'" target="_blank"><img src="http://upload.xwg.cc/uploadfiles/'+url+'" width="85" height="56" /></a>';
}

function appearFaces()
{
	var rand_num = parseInt(Math.random()*36+1);
	new Effect.Opacity('face_'+rand_num, { from: 0, to: 1 });
}

//登录
var login_type = '';
function checkLogin()
{
	if (!checkMT())
	{
		return false;
	}

	if (!checkPasswd())
	{
		return false;
	}

	//loading init
	disableHide('loading_field');
	enableHide('submit_field');

	var refer = $F('refer');

	var url = '/Public/doLogin/';
	var pars = 'type='+login_type+'&mobile='+$F('mobile')+'&passwd='+$F('passwd')+'&refer='+refer;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onSuccess: checkLoginResponse, onFailure: reportError});
}

//访客登录
function guestLogin()
{
	var refer = '';
	try
	{
		if ($F('refer')	)
		{
			refer = $F('refer');
		}
	}
	catch (e)
	{
	}
	var url = '/Public/doLogin/';
	var pars = 'type=1&mobile=13912345678&passwd=xwgfangkemima&refer='+refer;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onSuccess: checkLoginResponse, onFailure: reportError});
}

//登录回应
var current_uniqid = '';
var current_uid = '';
function checkLoginResponse(originalRequest)
{
	enableHide('loading_field');
	disableHide('submit_field');

	var retcode = originalRequest.responseText.evalJSON(true);

	if (retcode['result'] == '1')
	{
		if (retcode['url'])
		{
			window.location = retcode['url'];
		}
		else
		{
			var info = retcode['classinfo'].evalJSON(true);
			var html = '<select id="cid"><option value="">--- 请选择班级 ---</option>';

			for (var i=0; i<info.length; i++)
			{
				html += '<option value="'+info[i].cid+'" />['+info[i].school+']'+info[i].grade+'年级'+info[i].classnum+'班</option>';
			}

			html += '</select>';

			current_uniqid = retcode['uniqid'];
			current_uid = retcode['uid'];
			$('msg_content').innerHTML = html;
			envelop('login_msg', 446, 460, 40);
		}
	}
	else
	{
		if (retcode['url'])
		{
			if (confirm(retcode['message']))
			{
				window.location = retcode['url'];
			}
		}
		else
		{
			alert(retcode['message']);
		}
	}
}

//访客登录
function teacherLogin(val)
{
	if (confirm('确定要转换班级吗?')==true)
	{
		var url = '/Public/teacherLogin/';
		var pars = 'cid='+val;
		var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onSuccess: teacherLoginResponse, onFailure: reportError});
	}
	else
	{
//		window.location = 'http://bj.xwg.cc/Home';
		window.location.reload();
	}
}

//任课老师登录
function teacherCid()
{
	if ($F('cid'))
	{
		msg_Hide();
		var url = '/Public/selectCid/';
		var pars = 'cid='+$F('cid')+'&uniqid='+current_uniqid+'&uid='+current_uid;
		var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onSuccess: teacherLoginResponse, onFailure: reportError});
	}
	else
	{
		alert('请选择您所任课的班级！');
	}
}

function teacherLoginResponse(originalRequest)
{
	var retcode = originalRequest.responseText.evalJSON(true);

	if (retcode['result'] == '1')
	{
			window.location = retcode['url'];
	}
	else
	{
			alert(retcode['message']);
			window.location = 'http://bj.xwg.cc/Home';
	}
}

//任课老师登录
function selectCid()
{
	if ($F('cid'))
	{
		msg_Hide();
		var url = '/Public/selectCid/';
		var pars = 'cid='+$F('cid')+'&uniqid='+current_uniqid+'&uid='+current_uid;
		var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onSuccess: checkLoginResponse, onFailure: reportError});
	}
	else
	{
		alert('请选择您所任课的班级！');
	}
}

//判断登录类型
function checkMT()
{
	login_type = '';

	try
	{
		if ($('type_1').checked == true)
		{
			login_type = 1;
		}
	}
	catch (e)
	{
	}

	try
	{
		if ($('type_2').checked == true)
		{
			login_type = 2;
		}
	}
	catch (e)
	{
	}

	try
	{
		if ($('type_3').checked == true)
		{
			login_type = 3;
		}
	}
	catch (e)
	{
	}

	if (!login_type)
	{
		$('type_error').innerHTML = '请选择登录类型';
		disableHide('type_li');
		new Effect.Highlight('type_string', { startcolor: '#ffff99', endcolor: '#ffffff' });

		return false;
	}

	var mobile_string = $F('mobile');

	if (mobile_string != '')
	{
		if (isMobile(mobile_string))
		{
			enableHide('mobile_li');
			return true;
		}
		else
		{
			$('mobile_error').innerHTML = '您输入的手机号码格式不正确';
			disableHide('mobile_li');
			new Effect.Highlight('mobile', { startcolor: '#ffff99', endcolor: '#ffffff' });
		}
	}
	else
	{
		$('mobile_error').innerHTML = '请输入手机号';
		disableHide('mobile_li');
	}

	return false;
}

function msg_Hide()
{
	enableHide('login_msg');
	closeEnvelop();
}

//遮盖层
function getMask()
{
	return "mask";//遮罩的id
}
function envelop(_id, divWidth, divHeight, plusHeight)
{	
	//mask遮罩层

	var newMask = document.createElement("div");
	newMask.id = getMask();
	newMask.style.position = "absolute";
	newMask.style.zIndex = "2";
	_scrollWidth = Math.min(document.body.scrollWidth,document.documentElement.scrollWidth);
	_scrollHeight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
	newMask.style.width = _scrollWidth + "px";
	newMask.style.height = _scrollHeight + "px";
	newMask.style.top = "0px";
	newMask.style.left = "0px";
	newMask.style.background = "#33393C";
	newMask.style.filter = "alpha(opacity=27)";
	newMask.style.opacity = "0.27";
	document.body.appendChild(newMask);
	
	disableHide(_id);//显示层
	envelopPlace(_id, divWidth, divHeight, plusHeight);
}
function envelopPlace(_id, divWidth, divHeight, plusHeight)//弹出层定位
{
	var newDivWidth = divWidth;
	var newDivHeight = divHeight;

	document.getElementById(_id).style.top = (document.documentElement.scrollTop + (window.screen.availHeight - newDivHeight)/2 - plusHeight) + "px";
	document.getElementById(_id).style.left = (document.body.scrollLeft + document.body.clientWidth/2 - newDivWidth/2) + "px";
}
function closeEnvelop()//关闭新图层和mask遮罩层
{
	document.body.removeChild(document.getElementById(getMask()));
}
