//显示隐藏信息
function disableHide(id)
{
	try
	{
		$(id).style.display = '';
	}
	catch (e)
	{
	}
}

//隐藏隐藏信息
function enableHide(id)
{
	try
	{
		$(id).style.display = 'none';
	}
	catch (e)
	{
	}
}

//删除左右两端的空格
function trim(str)
{
	return str.replace(/(^\s*)|(\s*$)/g, "");
}

function reportError(request)
{
	alert('服务器正忙，请稍后！');
}

//更改显示图像
function replaceMenuImg(id, flag)
{
	var split = '';
	if (flag == 1)
	{
		split = '_';
	}
	$('menu_'+id).src = 'http://img.xwg.cc/www/Images/nav_'+id+split+'.gif';
}

//检测手机号码
function isMobile(strMobile){
	var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
	if(!myreg.test(strMobile))
	{
		var myreg2 = /^0(([1-9]\d)|([3-9]\d{2}))\d{8}$/;
		if (!myreg2.test(strMobile))
			return false;
		else
			return true;
	}
	else
	{
		return true;
	}
}

function checkLogin()
{
	var mobile = $F('mobile');
	if (!isMobile(mobile))
	{
		showMsg('请输入11位手机号', false, '');
		return false;
	}

	var passwd = $F('passwd');
	if (passwd.length <= 0)
	{
		showMsg('请输入密码', false, '');
		return false;
	}

	//loading init
	disableHide('loading_field');
	enableHide('submit_field');

	//Login values
	var rem = '';
	if ($F('remember'))
	{
		rem = 1;
	}

	var refer = '';
	try
	{
		if ($F('refer')	)
		{
			refer = $F('refer');
		}
	}
	catch (e)
	{
	}

	var url = '/Public/signin/';
	var pars = 'mobile='+mobile+'&passwd='+passwd+'&remember='+rem+'&refer='+refer;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onSuccess: checkLoginResponse, onFailure: reportError});
}

function checkLoginResponse(originalRequest)
{
	enableHide('loading_field');
	disableHide('submit_field');

	var retcode = originalRequest.responseText.evalJSON(true);

	if (retcode['result'] == '1')
	{
		window.location = 'http://i.xwg.cc'+retcode['url'];
	}
	else
	{
		if (retcode['url'])
		{
			if (confirm(retcode['message']))
			{
				window.location = retcode['url'];
			}
		}
		else
		{
			showMsg(retcode['message'], false, '');
		}
	}
}

function showMsg(content, isOK, func)
{
	var pic = 'err.gif';
	if (isOK)
	{
		pic = 'ok.gif'
	}

	$('system_msg_p').innerHTML = '<img src="http://img.xwg.cc/t/Images/'+pic+'" style="vertical-align:middle;" />&nbsp;'+content;

	envelop('system_msg_div');

	window.setTimeout("closeEnvelop('system_msg_div');", 2000);
	if (func)
	{
		window.setTimeout(eval(func), 3000);
	}
}

//弹出层
function envelop(_id)
{	
	//mask遮罩层

	var newMask = document.createElement("div");
	newMask.id = "mask";
	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);
	
	envelopPlace(_id);
}

function envelopPlace(_id)//弹出层定位
{
	disableHide(_id);//显示层
	var mainDiv = $(_id);
	mainDiv.style.zIndex = 500;
	var mainDivWidth = (mainDiv.clientWidth || mainDiv.offsetWidth);
	var mainDivheight = (mainDiv.clientHeight || mainDiv.offsetHeight);

	defaultInitLeft = (window.screen.availWidth - mainDivWidth)/2;
	defaultInitTop = document.documentElement.scrollTop + (window.screen.availHeight - mainDivheight)/2;

	mainDiv.style.top = defaultInitTop + "px";
	mainDiv.style.left = defaultInitLeft + "px";
}

function closeEnvelop(id)//关闭新图层和mask遮罩层
{
	enableHide(id);
	try
	{
		document.body.removeChild($('mask'));
	}
	catch (e)
	{
	}
}

var login_flag = 0;
function showLogin(obj)
{
	if (!login_flag)
	{
		var url = obj.href;
		url = url.split('http://');
		url = url[1];
		url = url.split('/');
		url = url[0];
		if (url == 'i.xwg.cc')
		{
			var refer = obj.href;
			refer = refer.substring(15, refer.length);
			refer = Base64.encode(refer);
			toURL = 'http://www.xwg.cc/Index/index/refer/'+refer;
			showMsg('你还没有登录哦～请登录后再试！', false, '');
		}
		else
		{
			window.location = obj.href;
		}
	}
	else
	{
		window.location = obj.href;
	}
}

function showFaces()
{
	for (var i=1; i<=60; i++)
	{
		loadImage(i, uids[i-1], titles[i-1]);
	}

	var int_temp = window.setInterval("appearFaces()",500)
}

function loadImage(id, uid, title)
{
	var url = 'http://upload.xwg.cc/uploadfiles/face/48/'+uid+'.gif';
	var img = new Image();
	img.src = url;

	if (img.complete)
	{
		faceHtml(id, uid, title);
		return;
	}

	img.onload = function()
	{
		faceHtml(id, uid, title);
	};
}

function faceHtml(id, uid, title)
{
	$('face_'+id).innerHTML = '<a href="http://i.xwg.cc/Space/index/uid/'+uid+'.html" title="'+title+'" target="_blank"><img src="http://upload.xwg.cc/uploadfiles/face/48/'+uid+'.gif" /></a>';
}

function appearFaces()
{
	var rand_num = parseInt(Math.random()*60+1);
	new Effect.Opacity('face_'+rand_num, { from: 0, to: 1 });
}

var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
