var ua = navigator.userAgent.toLowerCase();
var isIE = (ua.indexOf("msie") > -1),isIE7 = (ua.indexOf("msie 7") > -1),isOpera = (ua.indexOf("opera") > -1),isSafari = (ua.indexOf("webkit") != -1 || ua.indexOf("khtml") != -1),isGecko = (!isSafari && ua.indexOf("gecko") > -1);
var integral = '';
var gameType = '';
var games = ['dan'];

//错误信息定义
var error = 
{
    'NOT_LOGIN': 'Please login first.',
    'BAD_WARE': 'Input incorrect. Please check it and try again.',
    'SELL_OUT': 'The prize is run out. Please try others.',
    'OUT_POINT': 'Sorry, your point is not enough to exchange for this prize. Please try others.',
    'DB_ERROR': 'Sorry, the exchange cannot be completed. Please contact our Customer Service.'
};

function mousePosition(e)
{
    var posx=0,posy=0;
    if(e==null) e=window.event;
    if(e.pageX || e.pageY)
    {
        posx=e.pageX; posy=e.pageY;
        }
    else if(e.clientX || e.clientY)
    {
        if(document.documentElement.scrollTop)
        {
            posx=e.clientX+document.documentElement.scrollLeft;
            posy=e.clientY+document.documentElement.scrollTop;
        }
        else
        {
            posx=e.clientX+document.body.scrollLeft;
            posy=e.clientY+document.body.scrollTop;
        }
    }
	
	return {
	 		x:posx,         
	   		y:posy		
		};
}


function mouseMove(ev)
{
    ev = ev || window.event;
    mousePos = mousePosition(ev);
}

document.onmousemove = mouseMove;


//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);
}

window.onload = function ()
{
	try{_('_flash_').SetVariable('cookies',unescape(document.cookie));}catch(e){}	
}


//换用户进行登录
function changeLogin()
{
	$.get
    (
        'integral.php?do=changeLogin',
        function(data)
        {
			if ( data.length <= 150 )
				alert(data);
			else
			{
				//$('#information').html(data);
				//initServerList();
				//$('#information').fadeIn();	
				var width = document.body.scrollWidth;
				$('#login_info').html(data);
				initServerList();
				$('#login_info').css({top:'500px',left:width/2-100}).fadeIn();
			}
        } 
    );	
}


//用户玩游戏积分处理
function doIntegral(type)
{
	if (type == 'login')
	{
	     var param = 
	     {
	          account: _('account').value,
	          passwd: _('passwd').value,
	          code: _('code').value,
	          server_id: _('server_id').value,
	          area_id: _('area_id').value,
			  type:type,
			  gameType:gameType,
			  integral:integral
	     } 
	        
	     if ( param['account'] == '' || param['code'] == '' || param['passwd'] == '')
	     {
	         alert('Please input all required information for us to send the prizes.');
	         return false;
	     }
	 }
	 else
	 {
	 	var param = 
		{
			type:type,
			integral:integral,
			gameType:gameType
		};
	 }
	 	 	 	
     
    $.post
    (
        'integral.php?do=integral', 
        param,
        function(data)
        {
			if (data == 'OK')	//积分提交成功
			{
				alert('Points recorded successfully!');
				top.location.reload();	
			}
			else if ( data.length <= 150 )
			{
				if('LaJi Data' == data)
				{
					alert('Server error, please try again later.');
					location.reload();
					return;
				}
				else
				{
					alert(data);				
					try
					{
						_('code').value = '';
						_('_code').src='integral.php?do=code&'+Math.random();					
					}catch(e){}
				}
			}
			else
			{
				//$('#information').html(data);
				//initServerList();
				//$('#information').fadeIn();	
				var width = document.body.scrollWidth;
				var height = document.body.scrollHeight;
				$('#bg').width(width).height(height).css('opacity','0.3').show();
				$('#login_info').html(data);
				initServerList();
				$('#login_info').css({top:'500px',left:width/2-100}).fadeIn();
			}
        } 
    );
}

//供Flash调用的函数
function end_game(num)
{
	integral = num;
	doIntegral('show');
}


//重置Flash
function flashout(swf)
{
	if (swf == 'dan')
		gameType = 0;
				
	_('flasharea').innerHTML = "<object name=\"_flash_\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=9,0,28,0\" width=\"630\" height=\"485\"><param name=\"movie\" value=\"images\/" + swf + ".swf\"><param name=\"wmode\" value=\"opaque\" \/><param name=\"quality\" value=\"high\"><embed src=\"images\/" + swf + ".swf\" quality=\"high\" pluginspage=\"http:\/\/www.adobe.com\/shockwave\/download\/download.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application\/x-shockwave-flash\" width=\"630\" height=\"485\" wmode=\"transparent\" id=\"_flash_\" name=\"_flash_\" /><\/object>";
}


//初始化服务器列表
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){}	
}


//init the server list
function areaChanged(value, type) 
{
	try
	{
		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) 
	    {
	    	var oOptions = oAreaId.options;
	    	for (var i = 0; i < oOptions.length; i++)
	    	{
	    		if (oOptions[i].value == value)
	    			oOptions[i].selected = true;
	    	}
	    	
	    	
	        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 (type == area.sub[i].id) 
	        		oServerId.options[i].selected = true;
	        }
	    }		
	}catch(e){}
}
function closeDiv()
{
	$('#login_info').hide();
	$('#bg').fadeOut();
}