/**
* @author duumoto
* */
$(document).ready(function(){

	$('a.start').click(function(){
		var game=this.id.slice(7);
		var serverlist='<ul id="g1s" class="slist">';
		var i=0,swidth=280;

		$.post(
		"/index.php/home:getServerList",
		{ajax: 1, product: game},
		function(data){
			if(data.msg == null){
				$.each(data.servers, function (k,server){
					var sub_second = sub_opentime(server.opentime),href='';
					if(!server.hidden && sub_second<=0){
						if(server.check){
							serverlist +='<li class="s1-4"><span><a href="'+href+'"><b>[第（'+k+'）服]</b>'+server.name+'</a><em/></span></li>';
						}else{
							href='/index.php/account:login/product/'+game+'/snum/'+k;
							var sclass = server.isnew?('s1-6'):('s1-2');
							if(data.isLogin){
								serverlist +='<li class="'+sclass+'"><span><a href="'+href+'" target="_blank"><b>[第（'+k+'）服]</b>'+server.name+'</a><em/></span></li>';
							} else {
								serverlist +='<li class="'+sclass+'"><span><a href="###" onclick="javascript:playstart(\''+game+'\',\''+k+'\');"><b>[第（'+k+'）服]</b>'+server.name+'</a><em/></span></li>';
							}
						}
						i++;
					}
				});
				serverlist += '</ul>';
				switch(true){
					case i<=6:swidth=300;
					break;
					case i>6 && i<=12:swidth=560;
					break;
					case i>12:swidth=830;
					break;
					default:
					break;
				}
				if (serverlist != '<ul id="g1s" class="slist"></ul>'){
					$('#gameSL').html(serverlist);
					$('#gameSL').dialog({
						bgiframe: true,
						width:swidth,
						minHeight:100,
						resizable: false,
						modal: true,
						stack: true,
						title: '请登录[<font color=#FF8800>'+data.name+'</font>]服务器',
						close: function () {
							if (typeof fn != 'undefined') fn();
							$(this).dialog('destroy');
						}
					});
				}else{
					showMsg('游戏即将开服，您可以先玩其他游戏！','['+data.name+']','#FF8800');
				}
			}
		},
		"json"
		);
	});
	$('a.down').click(function(){
		var game=this.id.slice(7);
		var serverlist='<ul id="g1s" class="slist">';
		var i=0,swidth=280;
		$.post(
		"/index.php/home:getServerList",
		{ajax: 1, product: game},
		function(data){
			if(data.msg == null){
				$.each(data.download, function (k,download){
					serverlist += '<li class="s1-2"><span><a href="'+download.url+'"><b>['+k+']</b>'+download.name+'</a></span></li>';
				});
				serverlist += '</ul>';
				switch(true){
					case i<=6:swidth=300;
					break;
					case i>6 && i<=12:swidth=560;
					break;
					case i>12:swidth=830;
					break;
					default:
					break;
				}
				if (serverlist != '<ul id="g1s" class="slist"></ul>'){
					$('#gameSL').html(serverlist);
					$('#gameSL').dialog({
						bgiframe: true,
						width:swidth,
						minHeight:100,
						resizable: false,
						modal: true,
						stack: true,
						title: '请下载[<font color=#FF8800>'+data.name+'</font>]客户端',
						close: function () {
							if (typeof fn != 'undefined') fn();
							$(this).dialog('destroy');
						}
					});
				}else{
					showMsg('游戏即将开服，您可以先玩其他游戏！','['+data.name+']','#FF8800');
				}
			}
		},
		"json"
		);
	});
});

function sub_opentime(opentime){
	if(opentime){
		endtime = new Date(opentime).getTime();
		endtime =endtime;
		var now=new Date().getTime();
		second = Math.floor((endtime-now)/1000);
	}else{
		second = 0;
	}
	return second;
}
function playstart(game , snum){
	if ($('#loginIframeId').length == 0) {
		$('#loginModal').html("<div id='loginIframeId' class='form_box'><form method='POST' action='/index.php/account:login/product/"+game+"/snum/"+snum+"/fajax/1'><table width='370'><tbody><tr><th>帐号/邮箱:</th><td><input desc='帐号/邮箱' class='form_input' type='text' name='account'/></td></tr><tr><th>密码:</th><td><input class='form_input' type='password' name='password'/></td></tr><tr><th></th><td><input style='background-image:none;' class='common_submit' type='submit' value='登 录'/>&nbsp;&nbsp;<a href='/index.php/account:register/product/"+game+"/snum/"+snum+"' target='_blank'>快速注册</a></td></tr></tbody></table></form></div>");
	}
	$('#loginIframeId').dialog({
		bgiframe: true,
		resizable: true,
		title : '请先登陆',
		modal: false,
		width: 400,
		height: 150,
		close: function () {
			$(this).remove();
		},
		closeOnEscape: false
	})
	$('#loginIframeId').css({width:400 + 'px', height:150 + 'px', 'overflow-x':'hidden'});
	return false;
	$("#gameSL").dialog("close");
}

function count_down(subtime,div)
{
	var Go=function(){
		subtime--;
		if(subtime<0){
			window.location.reload();
		}else{
			var hour = Math.floor(subtime/3600),minute = Math.floor((subtime%3600)/60),second = Math.floor(subtime%60);
			var str=" 还有："+hour+"小时"+minute+"分"+second+"秒 开服",obj=$('#'+div);
			if(obj)obj.html(str);
		}
	}
	Go();
	setInterval(function(){Go();},1000);
}
function showMsg(msg,title,color, fn) {
	if(msg == 'undefined' || msg.trim().length == 0){
		return false;
	}
	if (typeof $('#ajaxMsg')[0] == 'undefined') {
		var ajaxMsg = $('<div id="ajaxMsg"><p class="p_msg"></p></div>').hide();
		$('body').append(ajaxMsg);
	}

	$('#ajaxMsg .p_msg').html(msg);
	$('#ajaxMsg').dialog({
		bgiframe: true,
		width:350,
		minHeight: 10,
		resizable: false,
		modal: true,
		stack: true,
		title: '温馨提示   <font color='+color+'>'+title+'</font>',
		buttons: {
			'确定': function () {
				$(this).dialog('close');
			}
		},
		close: function () {
			if (typeof fn != 'undefined') fn();
			$(this).dialog('destroy');
		}
	})
}