var username="";
function init()
{
    var fdata =
            {
                username: document.form1.username.value,
                password: document.form1.password.value,
                verify: document.form1.verify.value
            }; 

    req("login",dojo.toJson(fdata),this._submit_CB);
	
    username = document.form1.username.value;

    document.form1.username.value="";
    document.form1.password.value="";
    document.form1.verify.value="";
    return false;
}

function _submit_CB(result,ioArgs)
{
    if(result[0]<0)
    {
        ShowError(result);
    }
    else
    {
	//替换表单
	var node = dojo.byId("form1")
	var pare = node.parentNode;

	pare.removeChild(node);

	var e   = dojo.doc.createElement("div");	

	dojo.addClass(e,"login-success-label");

	var btn = dojo.doc.createElement("button");	

	dojo.addClass(btn,"selectserver");

	var btn_logout = dojo.doc.createElement("button");	

	dojo.addClass(btn_logout,"logout-btn");

	var br = dojo.doc.createElement("br");	

	pare.appendChild(e);
	pare.appendChild(btn);
	pare.appendChild(br);
	pare.appendChild(btn_logout);

	e.innerHTML="欢迎你" + username;
	btn.onclick = dojo.hitch(null,"ShowServer");
	btn_logout.onclick = dojo.hitch(null,"logout");
	
        ShowServer();
    }
}

function refresh_img()
{
     var auth_img = document.getElementById('authimg');
     auth_img.src= "authimg.php?q="+ Math.random();
     document.getElementById('verify').value="";
}

function hiddenDiv()
{
    dojo.style("errordiv","display","none");
   // dojo.style("serverdiv","display","none");
    dojo.style("serverdiv",{zIndex:-1});
}

function ShowError(rs)
{
        alert(rs[1]);
	//重置验证码
	refresh_img();
	
}

function logout()
{
	window.location="logout.php";
}

function gotogame(s_id)
{
	window.location="game.php?server_id="+s_id;
}

function getViewportInfo() 
{ 
   var w = (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth; 
   var h = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight; 
	return {w:w,h:h}; 
}

function ShowServer()
{
	var position = getViewportInfo();

	var toppx ="340px";
	var leftpx = (position.w/2 - 300 ) +"px";

	//定位
	//dojo.style("serverdiv","display","block");
    	dojo.style("serverdiv",{zIndex:999});
	dojo.style("serverdiv","top",toppx);
	dojo.style("serverdiv","left",leftpx);
}

function _error_callback(result,ioArgs)
{
	alert("服务器超时！请重试!");
//    dojo.byId("errormsg").innerHTML="服务器超时!";
//    dojo.style("errordiv","display","block");
} 

function req(_cmd,data,cb)
{
    return dojo.xhrPost({
       url: 'login_ajax2.php',
       handleAs: 'json',
       load: cb,
       error:_error_callback,
       timeout:6000,
       content: {cmd:_cmd,params:data}
    });
}


var $ = function(i){return document.getElementById(i);}
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);

/*tag 切换 */
function setTab(m,n){
var tli=document.getElementById("tab_menu"+m).getElementsByTagName("li");
var mli=document.getElementById("tab_main"+m).getElementsByTagName("ol");
for(i=0;i<tli.length;i++){
   try { mli[i].style.display= i==n?"block":"none";} catch(err) { }
   tli[i].className= i==n?'on':'none';
}
}

/*头部二级菜单 */
mySlideOutMenu.Registry = []
mySlideOutMenu.aniLen = 250
mySlideOutMenu.hideDelay = 500
mySlideOutMenu.minCPUResolution = 10

// constructor
function mySlideOutMenu(id, dir, left, top, width, height)
{
	this.ie  = document.all ? 1 : 0
	this.ns4 = document.layers ? 1 : 0
	this.dom = document.getElementById ? 1 : 0
	this.css = "";
	if (this.ie || this.ns4 || this.dom) {
		this.id			 = id
		this.dir		 = dir
		this.orientation = dir == "left" || dir == "right" ? "h" : "v"
		this.dirType	 = dir == "right" || dir == "down" ? "-" : "+"
		this.dim		 = this.orientation == "h" ? width : height
		this.hideTimer	 = false
		this.aniTimer	 = false
		this.open		 = false
		this.over		 = false
		this.startTime	 = 0
		// global reference to this object
		this.gRef = "mySlideOutMenu_"+id
		eval(this.gRef+"=this")
		// add this menu object to an internal list of all menus
		mySlideOutMenu.Registry[id] = this
		var d = document
		var strCSS = "";
		strCSS += '#' + this.id + 'Container { visibility:hidden; '
		strCSS += 'left:' + left + 'px; '
		strCSS += 'top:' + top + 'px; '
		strCSS += 'width:' + width + 'px; '
		strCSS += 'height:' + height + 'px; '		
		strCSS += 'overflow:hidden; z-index:500; }'
		strCSS += '#' + this.id + 'Container, #' + this.id + 'Content { position:absolute; '
		strCSS += '}'
		this.css = strCSS;
		this.load()
	}
}
mySlideOutMenu.writeCSS = function() {
	document.writeln('<style type="text/css">');
	for (var id in mySlideOutMenu.Registry) {
		document.writeln(mySlideOutMenu.Registry[id].css);
	}
	document.writeln('</style>');
}
mySlideOutMenu.prototype.load = function() {
	var d = document
	var lyrId1 = this.id + "Container"
	var lyrId2 = this.id + "Content"
	var obj1 = this.dom ? d.getElementById(lyrId1) : this.ie ? d.all[lyrId1] : d.layers[lyrId1]
	if (obj1) var obj2 = this.ns4 ? obj1.layers[lyrId2] : this.ie ? d.all[lyrId2] : d.getElementById(lyrId2)
	var temp
	if (!obj1 || !obj2) window.setTimeout(this.gRef + ".load()", 100)
	else {
		this.container	= obj1
		this.menu		= obj2
		this.style		= this.ns4 ? this.menu : this.menu.style
		this.homePos	= eval("0" + this.dirType + this.dim)
		this.outPos		= 0
		this.accelConst	= (this.outPos - this.homePos) / mySlideOutMenu.aniLen / mySlideOutMenu.aniLen 
		// set event handlers.
		if (this.ns4) this.menu.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
		this.menu.onmouseover = new Function("mySlideOutMenu.showMenu('" + this.id + "')")
		this.menu.onmouseout = new Function("mySlideOutMenu.hideMenu('" + this.id + "')")
		//set initial state
		this.endSlide()
	}
}
	
mySlideOutMenu.showMenu = function(id)
{
	var reg = mySlideOutMenu.Registry
	var obj = mySlideOutMenu.Registry[id]	
	if (obj.container) {
		obj.over = true
		// close other menus.
		for (menu in reg) if (id != menu) mySlideOutMenu.hide(menu)
		// if this menu is scheduled to close, cancel it.
		if (obj.hideTimer) { reg[id].hideTimer = window.clearTimeout(reg[id].hideTimer) }
		// if this menu is closed, open it.
		if (!obj.open && !obj.aniTimer) reg[id].startSlide(true)
	}
}

mySlideOutMenu.hideMenu = function(id)
{
	// schedules the menu to close after <hideDelay> ms, which
	// gives the user time to cancel the action if they accidentally moused out
	var obj = mySlideOutMenu.Registry[id]
	if (obj.container) {
		if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
		obj.hideTimer = window.setTimeout("mySlideOutMenu.hide('" + id + "')", mySlideOutMenu.hideDelay);
	}
}

mySlideOutMenu.hideAll = function()
{
	var reg = mySlideOutMenu.Registry
	for (menu in reg) {
		mySlideOutMenu.hide(menu);
		if (menu.hideTimer) window.clearTimeout(menu.hideTimer);
	}
}

mySlideOutMenu.hide = function(id)
{
	var obj = mySlideOutMenu.Registry[id]
	obj.over = false
	if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
	// flag that this scheduled event has occured.
	obj.hideTimer = 0
	// if this menu is open, close it.
	if (obj.open && !obj.aniTimer) obj.startSlide(false)
}

mySlideOutMenu.prototype.startSlide = function(open) {
	this[open ? "onactivate" : "ondeactivate"]()
	this.open = open
	if (open) this.setVisibility(true)
	this.startTime = (new Date()).getTime()	
	this.aniTimer = window.setInterval(this.gRef + ".slide()", mySlideOutMenu.minCPUResolution)
}

mySlideOutMenu.prototype.slide = function() {
	var elapsed = (new Date()).getTime() - this.startTime
	if (elapsed > mySlideOutMenu.aniLen) this.endSlide()
	else {
		var d = Math.round(Math.pow(mySlideOutMenu.aniLen-elapsed, 2) * this.accelConst)
		if (this.open && this.dirType == "-")		d = -d
		else if (this.open && this.dirType == "+")	d = -d
		else if (!this.open && this.dirType == "-")	d = -this.dim + d
		else										d = this.dim + d
		this.moveTo(d)
	}
}

mySlideOutMenu.prototype.endSlide = function() {
	this.aniTimer = window.clearTimeout(this.aniTimer)
	this.moveTo(this.open ? this.outPos : this.homePos)
	if (!this.open) this.setVisibility(false)
	if ((this.open && !this.over) || (!this.open && this.over)) {
		this.startSlide(this.over)
	}
}

mySlideOutMenu.prototype.setVisibility = function(bShow) { 
	var s = this.ns4 ? this.container : this.container.style
	s.visibility = bShow ? "visible" : "hidden"
}
mySlideOutMenu.prototype.moveTo = function(p) { 
	this.style[this.orientation == "h" ? "left" : "top"] = this.ns4 ? p : p + "px";
}
mySlideOutMenu.prototype.getPos = function(c) {
	return parseInt(this.style[c]);
}

// events
mySlideOutMenu.prototype.onactivate		= function() { };
mySlideOutMenu.prototype.ondeactivate	= function() { };



new mySlideOutMenu("menu1", "down",250,0,500,41)//数值表示: (下拉菜单距离左侧位置, 0, 下拉菜单宽度, 41)注意:距离左侧位置与宽度相加不得超过1002px
	new mySlideOutMenu("menu2", "down",260,0,600,41)
	new mySlideOutMenu("menu3", "down",300,0,600,41)
	new mySlideOutMenu("menu4", "down",340,0,600,41)
	new mySlideOutMenu("menu5", "down",680,0,200,41)
	new mySlideOutMenu("menu6", "down",640,0,300,41)
	new mySlideOutMenu("menu7", "down",670,0,300,41)
    mySlideOutMenu.writeCSS();

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}




window.servers = 
{
//    't2': ['http://t2.17war.com/', 'login.php', 'authimg.php'],
    't1': ['http://s1.17war.com/', 'login.php', 'authimg.php']
};
window.curr_server_id = '';

function get_current_server()
{
    var server_id = window.curr_server_id;
    if (! server_id in window.servers)
        return null;
    else
        return window.servers[server_id];
}

function refresh_auth_code()
{
    var server = get_current_server();
    if (! server)
        return;
    var img_node = document.getElementById("authimg");
    if (img_node)
    {
        var auth_url = server[0] + server[2];
        img_node.src = auth_url + '?q=' + Math.random();
    }
}

function change_server(obj)
{
    if (obj.selectedIndex != -1)
    {
        var server_id = obj.options[obj.selectedIndex].value;
        window.curr_server_id = server_id;
        var server = get_current_server();
        if (server)
        {
            var login_frm = document.getElementById("form1");
            if (login_frm)
                login_frm.action = server[0] + server[1];
            var node = document.getElementById("imgid");
            if (node) 
            {
                node.innerHTML = '<img id="authimg" alt="点击刷新验证码" style="cursor:pointer;border:0;vertical-align:middle;height:18px;" />';
                var img_node = document.getElementById("authimg");
                if (img_node)
                    img_node.onclick = refresh_auth_code;
                refresh_auth_code();
            }
        }
        else
        {
            var login_frm = document.getElementById("form1");
            if (login_frm)
                login_frm.action = '';
            var node = document.getElementById("imgid");
            if (node) 
                node.innerHTML = '';
        }
    }
}

function login()
{
	//alert("heh");
	if (window.navigator.userAgent.toLowerCase().indexOf("msie 8")>=1)
	{
		alert("游戏暂时不支持ie8浏览器，请使用firefox 3.0或ie6/ie7登录游戏。\n我们将尽快提供ie8的支持，敬请期待!");
		return false;
	}
    var login_frm = document.getElementById("form1");
    if (login_frm && login_frm.action != '')
        return true;
    else
        return false;        
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

