//document.getElementById
function _(element)
{
    if (arguments.length > 1)
    {
        for (var i = 0, elements = [], length = arguments.length; i < length; i++)
          elements.push($(arguments[i]));
        return elements;
    }
    else
        return document.getElementById(element);
}

//init the server list
function areaChanged(value, type)
{
	oAreaId = _('area_id');
	oServerId = _('server_id');

    var area = serverList[0];
    for (var i=0; i<serverList.length; i++)
    {
        if (serverList[i].id == value)
		{
        	area = serverList[i];
        	break;
        }
    }

    if (area)
    {
        oServerId.options.length = 0;
        for (var i=0; i<area.sub.length; i++)
        {
        	oServerId.options.add(new Option(area.sub[i].name, area.sub[i].id));
        	if ('1' == area.sub[i].id)
        		oServerId.options[i].selected = true;
        }
    }
}
//初始化服务器列表
function initServerList()
{
	try
	{
		oAreaId = _('area_id');
		oServerId = _('server_id');
		oAreaId.options.length = 0;

		for (var i=0; i<serverList.length; i++)
		{
			oAreaId.options.add(new Option(serverList[i].name,serverList[i].id));
			if ('1' == serverList[i].id)
				oAreaId.options[i].selected = true;
		}

		areaChanged(1,0);
	}
	catch(e){}
}
function changeCode()
{
	_('_code').src='index.php?do=code&'+Math.random();
}
$(function()
   {
	   initServerList();

   }
)

function showOrHid(/*boolean*/flag)
{
	var width  = $('body').width();
	var height = $('body').height();

	if (typeof flag == "undefined" || flag == "" || !flag)
		$('#bg2').width(width).height(height + 300).css('opacity','0.3').show();
	else
		$('#bg2').width(width).height(height).css('opacity','0.3').hide();
}

function comm_login()
{
	if (logined == 0)
	{
		alert('请先登录。');
		showlogin(250,280);
		scroll(0,0);
		return false;
	}
	else if (played == 0)
	{
		alert('请提交个人信息');

		showOrHid();
		$('#pop_2').fadeIn();
		scroll(120,120);
		return false;
	}
	return true;
}

function dologin()
{
	$.getJSON
	(
	 	'index.php?do=login',
		function(data)
		{
			if(data['txt']=='0')
			{
				 flag = 0;
				//alert(data['text']);
			}
			else if(data['txt'] == '2')
			{
				flag = 2;
				//alert(data['text']);
				//$("#pop_2").show();
			}else
			{
				flag = 1;
				window.location.href="index.php";
			}
			showPop3(flag,data['text']);
		}
	)
}

//更新玩家提交的个人资料
function dosubmit()
{
	var name = $('#name').val();
	var email = $('#email').val();
	var address = $('#address').val();
	var tel = $('#tel').val();

	if(email =='' || email == null)
	{
		alert('请输入你的邮箱地址！');
		_('email').focus();
		return;
	}
	if(!(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(email)))
	{
		alert('请输入正确电子邮件！');
		_('email').focus();
		return;
	}

	$.getJSON
	(
	 	'index.php?do=submit',
		{name:name,email:email,address:address,tel:tel},
		function(data)
		{
			if (data['txt'] == '1' || data['txt'] == '3')
			{
				showOrHid(true);
				$("#pop_2").hide();
				info = 1;

				$("#btn03_1").click(
					function ()
					{
						window.location.href = "index.php";
					}
				);
			}
			else
			{
				$("#btn03_1").click(
					function ()
					{
						$("#pop_3").fadeOut();
					}
				);
			}
			$("#pop_3").fadeIn();
			$("#pmsg").html(data['text']);
		}
	)
}

/**
 * 玩家领取虚拟礼包的操作
 */
function getPrize()
{
	$.getJSON
	(
		"index.php?do=getPrize",
		function(data)
		{
			if(data['txt']=='1')
			{
				$("#btn03_1").click(
					function ()
					{
						$("#pop_3").fadeOut();
						window.location.href = "index.php";
					}
				);
			}
			else
			{
				$("#btn03_1").click(
					function ()
					{
						$("#pop_3").fadeOut();
					}
				);
			}

			$("#pop_3").fadeIn();
			$("#pmsg").html(data['text']);
		}
	);
}

//取消个人信息
function onCanle()
{
	$.get(
		'index.php?do=setPersonInfoState',
		function (data)
		{
			//掩藏个人信息框资料.
			info = 1;

			showOrHid(true);
			$("#pop_2").fadeOut();
		}
	);
}

function hidePop3(flag)
{
	$('#pop_3').fadeOut();

	if (flag == 1)
		window.location.href="index.php";
	if(flag == 2)
		$("#pop_2").show();
	if(flag == 3)
	{
		$("#pop_2").hide();
		window.location.href="index.php";
	}
}

/**
 * 玩家推广排名
 */
$(function(){
	$('#top').load("index.php?do=top100");
})

function ajaxPage(url)
{
	$('#top').load(url,function (data){;});
}
/**
 * 玩家退出js
 */
function _logout(){
    $.post(
        'index.php?do=Logout',
        function(data){
           top.location.reload();
        }
    )
}