var oAreaId = '';	//游戏区列表
var oServerId = '';	//服务器列表
var sMessage = '';	//存储转盘相关的信息
var bStats = 'finish';	//用来检测Flash是否按顺序调用

//随机字段，测试用
var r1 = parseInt(Math.random() * 13) + 1;

//点击开始后触发的事件
function start()
{

	/*
		//alert(r1);
		if(window.flashvars) window.document["flashvars"].SetVariable("flashonid", r1);
		if(document.flashvars) document.flashvars.SetVariable("flashonid", r1);
	*/	
	
	if (bStats == 'start')
	{
		alert('请的操作太过频繁，请每次抽奖完成后再进行第二次抽奖，您每次点击都会使用您的一次抽奖机会！');
		top.location.reload();
		return false;
	}
	
    $.getJSON
    (
        'index.php?do=chagre', 
        function(data)
        {
            if (data.ware != '0' && data.text != '0')
			{
				$('#ChargeInfo').hide();
				
				//flashchange(data.ware, data.text);
				
				if(window.flashvars) window.document["flashvars"].SetVariable("flashonid", data.ware);
				if(document.flashvars) document.flashvars.SetVariable("flashonid", data.ware);	
						
				sMessage = data.text;
				
				bStats = 'start';	
			}
			else
			{
				$('#ChargeInfo').html(data['innerHTML']);
				$('#ChargeInfo').show();
				flashchange();
			}
			
			if ( data['login'] == '0')
				initServerList2();
        } 
    );
		
}



//点击结束的时间
function end()
{
	if (sMessage.length > 5)		//视为存在信息
	{
		$('#ChargeInfo').html(sMessage);
		$('#ChargeInfo').show();	
		
		sMessage = '';	
		
		bStats = 'finish';
	}
}

//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);
}


//更换Flash的内容做为抽奖用
function flashchange()
{
	var flashinner ="<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0\" width=\"659\" height=\"611\"  id=\"flashvars\">" ;
	flashinner = flashinner + "<param name=\"movie\" value=\"flash/flash.swf\" id=\"flashvars\"><param name=\"quality\" value=\"high\" id=\"flashvars\">";
	flashinner = flashinner + "<param name=\"wmode\" value=\"transparent\" id=\"flashvars\"/> ";
	flashinner = flashinner + "<embed src=\"flash/flash.swf\" quality=\"high\" pluginspage=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"659\" height=\"611\" wmode=\"transparent\" id=\"flashvars\" name=\"flashvars\">";
	flashinner = flashinner + "</embed></object>";

    _('flash').innerHTML = flashinner;
}


//处理兑换物品相关的操作
function exchange()
{
	var prizeId = 0;
	
	if (arguments.length == 1)
		 prizeId = arguments[0];
		 
	if (arguments.length != 1 || (arguments.length == 1  && confirm('您确认要兑换吗？兑换后系统将自动扣除兑换该物品所需要的文字'))) 
	{
		$('#ChargeInfo').hide();
		
		$.getJSON
		(
		   	'index.php?do=exchange',
		  	{prize: prizeId},			
		   	function(data)
		   	{				
		   		$('#ChargeInfo').html(data['innerHTML']);
				$('#ChargeInfo').show();
				
				if ( data['login'] == '0')
					initServerList2();
			} 
		);			
	}
}

//重新登录处理
function reLogin()
{
    $.getJSON
    (
        'index.php?do=reLogin', 
        function(data){window.location.reload();}
    );	
}

//初始化转盘中的游戏服务器列表
function initServerList2()
{
	oAreaId = _('area_id_2');
	oServerId = _('server_id_2');
	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, 1);	
	
}

//响应游戏区列表更改
function areaChanged(value, type) 
{
	if (type == 0)
	{
		oAreaId = _('area_id');
		oServerId = _('server_id');
	}
	else
	{
		oAreaId = _('area_id_2');
		oServerId = _('server_id_2');		
	}
	
    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;
        }
    }
}
	 

//初始化游戏列表
window.onload = function()
{
	
	
	
	flashchange(0,' ');
}

 
//抽奖登录函数实现
function doChargeLogin()
{
	if (_('code_2').value == '') 
    {
		alert('请填写验证码。');
		return false;
	} 
	
	$.getJSON
	(
	   	'index.php?do=ChargeLogin',
	  	{ 
	   		
			code: _('code_2').value,
			area_id: _('area_id_2').value,
			server_id: _('server_id_2').value
	   	},
	   	function(data) 
	   	{
			if (data.innerHTML != '0')
			{
				$('#ChargeInfo').html(data.innerHTML);
				$('#ChargeInfo').show();				
			}
	   		else
				alert(data.text);
		} 
	);	
}

//获取100元礼包
function getPrize()
{
	if (  _('code').value == '') 
    {
		alert('验证码是必须填写的。');
		return false;
	} 
	
	$.get
	(
	   'index.php?do=prize',
	   { 
	   	
		code: _('code').value,
		area_id: _('area_id').value,
		server_id: _('server_id').value
	   },
	   function(data) {_('code').value = ""; _('_code').src='index.php?do=code&'+Math.random();alert(data);} 
	);	
}
//登录
function getPrize2()
{
	if (  _('code').value == '') 
    {
		alert('验证码是必须填写的。');
		return false;
	} 
	
	$.get
	(
	   'index.php?do=prize2',
	   { 
	   	
		code: _('code').value,
		area_id: _('area_id').value,
		server_id: _('server_id').value
	   },
	   function(data) {_('code').value = ""; _('_code').src='index.php?do=code&'+Math.random();alert(data);location.reload();} 
	);	
}
