var win=this;
var mousePos;
var preClubIdx = '';
var clubIdx = 'default';
var menuInitColor ='';
var linkID = '';
var browser_version = '';

if (window.XMLHttpRequest) { //Mozilla,IE7 
	 if(!window.ActiveXObject){ // Mozilla
  		browser_version='Mozilla';
 	} else
  		browser_version = 'IE7';
} else
 	browser_version = 'IE6';
 	
 function loadPerm(){
 	var url = '/checkPerm.action?rId='+curRoleId;
	new Ajax.Request(url,
	{
		method:'GET',
		onComplete:checkPermCallback,
		asynchronous:true		
	});		
}

function checkPermCallback(o){
	if(o.responseText=='true')
		document.location = '/officerManage.action?rId='+curRoleId;
	else
		alert('此角色未具有任何管理权限！');
}

window.onload=function(){
/*
	initClubTree();
	
	$('searchTxt').onkeydown = function(){
		if(event.keyCode != 13) return;
		searchCall();
	};
*/
	
	/**
	 * handle select element for IE_6 bug
	 * repaint select element
	 */
/*	 
	 if(browser_version =='IE6'){
		window.setInterval(function(){
			$('gameId').style.visibility = 'hidden';
			$('gameId').style.visibility = 'visible';
			
			$('gameAreaId').style.visibility = 'hidden';
			$('gameAreaId').style.visibility = 'visible';
			
			if($('gameServerId').style.visibility != 'hidden'){
				$('gameServerId').style.visibility = 'hidden';
				$('gameServerId').style.visibility = 'visible';
			}
			
			if($('clubName').style.visibility != 'hidden'){
				$('clubName').style.visibility = 'hidden';
				$('clubName').style.visibility = 'visible';
			}
			if($('clubName').style.visibility != 'hidden'){
				$('roleName').style.visibility = 'hidden';
				$('roleName').style.visibility = 'visible';
			}
		},5000);
		
		$('belongTreeDiv').style.width='425px';
		$('ownTreeDiv').style.width='425px';
	}
	
	if(browser_version=='Mozilla'){
		$('searchTxt').style.marginRight='78px';
	}
	
	//dns
	var locationHref=window.location.href;
	
	if(locationHref.indexOf('www.ngamebar.com')>0)
		$('icpDiv').style.display='block';
	
	var j = locationHref.indexOf("club.ngamebar.com");
	
	if(j>0){
		var clubId = getQuery("clubId",locationHref);
		var roleId = getQuery("roleId",locationHref);
		var clubroleId = getQuery("clubroleId",locationHref);
				
		var t = getQuery("t");
		
		switch(t)	{
			case "1": window.location.href='/clubLeader.action?clubId='+clubId+"&roleId="+roleId+"&clubroleId="+clubroleId;clubIdx='leader';break;
			case "2":window.location.href='/clubLeader.action?clubId='+clubId+"&clubroleId="+clubroleId;clubIdx='leader';break;
			case "3": window.location.href='/clubGuest.action?clubId='+clubId;clubIdx='guest';break;
			default: $('ifr').src='/clubDefault.action';clubIdx='default';break; 
		}
	}else{
		$('ifr').src='/clubDefault.action';
		clubIdx='default';
		
	}
*/	
}

function searchCall(){
	if($('searchTxt').value.length==0) return;	

	var url = '/clubSearch.action?key='+encodeURI($('searchTxt').value);
	new Ajax.Request(url,
	{
		method:'GET',
		onComplete:showSearch,
		asynchronous:true		
	});		
}

function toggleGameList(){

	var door = $('gameList').style.display;
	if(door!='block'){

		$('gameList').style.display='block';
		initClubTree();
	}else{
		$('gameList').style.display='none';
	}
	
}	
function fill(){
	var obj = window.event.srcElement;
	$('gameBoard').value=obj.innerText;
	$('gameBoard').style.background='rgb(149,106,97)';
	$('gameBoard').style.color='white';
	linkID = obj.id;
	fixGameList();
}

function fixGameList(){
	$('gameServerId').style.visibility='visible';
	$('clubName').style.visibility='visible';
	$('roleName').style.visibility='visible';
	$('gameList').style.display='none';
}

function itemM(index){
	var obj = window.event.srcElement;
	if(index==0){
		obj.style.textDecoration="underline";
		obj.style.color="red";
	}else{
		obj.style.textDecoration="none";
		obj.style.color="#935e32";
	}
}

function showMsg(id,width,height,o){
    var msgurl="/cwbbs/message/message.jsp?msgId="+id;
  	var url = namespace+'cancelNote.action';
  	var params = "msgId="+id;
	try{o.parentNode.removeChild(o);}catch(e){};
	window.open(msgurl,"_blank","toolbar=no,scroll=auto,location=no,directories=no,status=no,menubar=no,top=120,left=120,width="+width+",height="+height);
}

function go(){
	if(linkID.length==0)
		return;
	var url = "/cwbbs/forum/f-0-" + linkID + "-1-0.html";
	linkID = '';
	$('gameBoard').innerText='-- 请 选 择 吧 台 --';
	$('gameBoard').style.background='white';
	$('gameBoard').style.color='#373737';
	window.open(url);
}

function showSearch(response){
	var data = response.responseText.split(";");

	if(data.length==0) return;
	var i;
	var html = new Array();
	for(i=0;i<data.length;i++){
		if(data[i].length==0) continue;
		html[html.length] = '<span onmouseover="searchStyle(1)" onmouseout="searchStyle(0)" onclick="visitClub('+data[i].split(',')[1]+')">'+data[i].split(',')[0]+'</span>';
	}
	
	$('searchResult').innerHTML = html.join('');
	$('searchResult').style.visibility='visible';
}

function visitClub(id){
	var url = '/clubDNS.action?clubId='+id;
	new Ajax.Request(url,
	{
		method:'GET',
		onComplete:function(o){
			var dnsName = o.responseText;
			
			if(dnsName!="" && (window.location.href+"").indexOf(dnsName)<=0){
				window.location.href="http://"+dnsName+"/clubGuest.action?t=3&clubId="+id;
			}else{
				$('searchResult').style.visibility='hidden';
				$('ifr').src='/clubGuest.action?clubId='+id;
				preClubIdx = clubIdx;
				clubIdx = 'guest';
			}
		},
		asynchronous:true		
	});
}

function memberClub(id,roleId){
	var url = '/clubDNS.action?clubId='+id;
	new Ajax.Request(url,
	{
		method:'GET',
		onComplete:function(o){
			var dnsName = o.responseText;
			if(dnsName!="" && (window.location.href+"").indexOf(dnsName)<=0){
				window.location.href='http://'+dnsName+'/clubLeader.action?t=1&clubId='+id+'&roleId='+roleId+'&clubroleId='+roleId;
			}else
				$('ifr').src='/clubLeader.action?clubId='+id+'&roleId='+roleId+"&clubroleId="+roleId;
			
			preClubIdx = clubIdx;
			clubIdx = 'member';
		},
		asynchronous:true		
	});
}

function leaderClub(id){
	var url = '/clubDNS.action?clubId='+id;
	new Ajax.Request(url,
	{
		method:'GET',
		onComplete:function(o){
			var dnsName = o.responseText;
			
			if(dnsName!="" && (window.location.href+"").indexOf(dnsName)<=0){
				XMLHttp.onmit("getClubManageRoleId.action","clubId="+id,
				function(o)
				{
					var b=false;
					var roleId=o.responseText;					
					window.location.href="http://"+dnsName+"/clubLeader.action?t=2&clubId="+id+"&clubroleId="+roleId;//修改了：2010-06-03
					//window.location.href="http://localhost/clubIndex.action?t=2&clubId="+id+"&clubroleId="+roleId;
				});
			}else{
				XMLHttp.onmit("getClubManageRoleId.action","clubId="+id,
				function(o){
					var b=false;
					var roleId=o.responseText;
					
					$('ifr').src='/clubLeader.action?clubId='+id+"&clubroleId="+roleId;
				});
			}
			preClubIdx = clubIdx;
			clubIdx = 'leader';
		},
		asynchronous:true		
	});
}
/************************ begin Club Tree *************************/
function initClubTree(){
	loadBelongTree();  //所在的
	loadOwnerTree();   //自己创建的社团
	
	//loadAllTree();发送所有社团请求
}
function loadAllTree(){
    var url='/clubTree.action?randnum='+Math.random();
    new Ajax.Request(url,
    {
         method:'GET',
         onComplete:drawAllTree,
         asynchronous:true
    
    }
    );
function drawAllTree(callback){
var val=callback.responseText;//callback.responseText返回的实际上是异步的页面

//接下来获取页面div的innerHTML,将val值赋给其中.


}



}

function loadBelongTree(){
	var url = '/clubTree.action?type=be'+'&randnum=' + Math.random();
	new Ajax.Request(url,
	{
		method:'GET',
		onComplete:drawBelongTree,
		asynchronous:true		
	});
}

function loadOwnerTree(){
	var url = '/clubTree.action?type=ow'+'&randnum=' + Math.random();
	new Ajax.Request(url,
	{
		method:'GET',
		onComplete:drawOwnerTree,
		asynchronous:true		
	});
}

function drawBelongTree(callback){
	var data = callback.responseText;
	if(data.length>0)
		$('treeMain').style.display='block';
	
	var nodes = JSON.parse(data);
	$('belongTree').innerHTML = AY.drawBelong(nodes);
}

function drawOwnerTree(callback){
	var data = callback.responseText;
	if(data.length>0)
		$('treeMain').style.display='block';
	
	var nodes = JSON.parse(data);
	$('ownerTree').innerHTML = AY.drawOwner(nodes);
}
/************************ end Club Tree *************************/

function createClub(){
	document.location.href='/createClub.action';
}

function createGH(){
	document.location.href='/createGH.action';
}

function ghIndex(){
	document.location.href='/ghIndex.action';
}

function createStyle(idx){
	var obj = window.event.srcElement;
	var src = obj.src;
	obj.src=idx==1?src.replace('create','create_over'):src.replace('create_over','create');
}

function closeStyle(idx){
	var obj = window.event.srcElement;
	obj.src=idx==0?'/club/template/default/club_close.gif':'/club/template/default/club_close_over.gif';
}

function menuStyle(idx){
	var obj = window.event.srcElement;
	if(idx==1) menuInitColor=obj.style.color;
	obj.style.color = idx==0?menuInitColor:'white';
}

function searchStyle(idx){
	var obj = window.event.srcElement;
	obj.style.background = idx==0?'':'rgb(204,204,204)';
}

function toggleSearchResult(){
	if($('searchResult').style.visibility=='hidden'){
		$('searchResult').innerHTML = '';
		$('searchResult').style.visibility='visible';
	}else
		$('searchResult').style.visibility='hidden';
}

function focusSearch(idx){
	if(idx==1)
		$('searchTxt').value='';
	else if(idx==0&&$('searchTxt').value.length==0){
		$('searchTxt').value='搜索社团';
	}
}

function clubGo(idx, params){
	XMLHttp.onmit("isJoinClubAudit.action","1=1",
	function(o){
		var b=false;
		var msg=o.responseText;
		
		if(msg=="noAudit"){
			b=true;
			alert("请等待社长审批通过后进入个人中心!");
			return false;
		}
		
		if(b&&idx==0)
			return;
		params=params==undefined?"":params;
		//ngbOpen(idx==1?'clubManage.action'+params:'myClubManage.action'+params);
		document.location.href = idx==1?'/clubManage.action'+params:'/myClubManage.action'+params;
	
	});
}

function ngbOpen(url){
	var openLink = document.createElement("a");
	openLink.href = url;
	openLink.target = '_blank';
	
	document.body.appendChild(openLink);
	
	 if(document.all)   
         openLink.click();           
    else{   
    	var evt = document.createEvent("MouseEvents");   
    	evt.initEvent("click",   true,   true);   
    	openLink.dispatchEvent(evt);   
    }
    
    document.body.removeChild(openLink);   
}

function clubLink(idx){
	switch(idx){
		case 1: document.location.href='http://www.ngamebar.com';break;
		case 2: document.location.href='http://www.ngamebar.com/clubIndex.action';break;
		case 3: 
			var url=window.location.href+"";
			var boardCode = document.getElementById("forumBoardCode");
			if(url.indexOf('www.ngamebar.com')>0)	{
				document.location.href='http://www.ngamebar.com/cwbbs/forum';
			}else if(boardCode!=null && boardCode.value!="") {
				//游戏厂商的论坛boardCode已经存在session里
				document.location.href="/cwbbs/forum/listtopic.jsp?boardcode="+boardCode.value;
			}else{
				port="",
				dnsName=url.split(".")[0].split("//")[1];
				XMLHttp.onmit("onRecords.action","dnsName="+dnsName,function(o){
					//进入社团论坛
					//如果相关社团域名存在直接进入当前域名的社团论坛，如果不存在进入ngamebar论坛
					var str = o.responseText.split(",");
					if(str.length>1){
						var recordsId=str[0];
						document.location.href='http://'+str[1]+port+'/cwbbs/clubforum/frame.jsp?recordsId='+recordsId;
					}else
						document.location.href='http://www.ngamebar.com/cwbbs/forum';
				});
			}	
			break;
		case 4: document.location.href='http://www.ngamebar.com/cwbbs/blog';break;
	}
}

/**fxh*/
function onexit(){
    var url = '/cwbbs/exit.jsp';
	new Ajax.Request(url,
	{
		method:'GET',
		onComplete:function(o){
		},
		asynchronous:true		
	});
		
	var url = '/logUserOut.action';
	new Ajax.Request(url,
	{
		method:'GET',
		onComplete:function(o){
			window.location.href='/clubIndex.action';
		},
		asynchronous:true		
	});
}

function selectGame(){
	var url = 'cascadeGame.action';
	var params = "gameId="+$('gameId').value;
	aj(url,params,function(o){
		var area = $('gameAreaId');
		area.options.length = 0;
	    if(o.responseText.length > 0){ 
			var jsonObj = eval('('+ o.responseText + ')');
      		for(var i = 0;i < jsonObj.option.length;i++){
				area.options[area.options.length]=new Option(jsonObj.option[i].areaName,jsonObj.option[i].areaId);
			}
			selectArea();
			casCadeGameRole();
		}
	});
}

function selectArea(){
	var url = 'cascadeArea.action';
	var params = "gameAreaId="+$('gameAreaId').value;
	aj(url,params,function(o){
		var server = $('gameServerId');
		server.options.length = 0;
	    if(o.responseText.length > 0){ 
			var jsonObj = eval('('+ o.responseText + ')');
	      		for(var i = 0;i < jsonObj.option.length;i++){
					server.options[server.options.length]=new Option(jsonObj.option[i].serverName,jsonObj.option[i].serverId);
				}
		}
		selectClub();
	});
}

function selectClub(){
	var url = 'cascadeClub.action';
	var params = "gameServerId="+$('gameServerId').value;
	aj(url,params,function(o){
		var server = $('clubName');
		server.options.length = 0;
	    if(o.responseText.length > 0){
			var jsonObj = eval('('+ o.responseText + ')');
	      		for(var i = 0;i < jsonObj.option.length;i++){
					server.options[server.options.length]=new Option(jsonObj.option[i].name,jsonObj.option[i].id+"-"+jsonObj.option[i].bid);
				}
			selectRole();
		}
	});
}

function selectRole(){
	var url = 'cascadeRole.action';
	var params = "gameServerId="+$('gameServerId').value;
	aj(url,params,function(o){
		var server = $('roleName');
		server.options.length = 0;
	    if(o.responseText.length > 0){ 
			var jsonObj = eval('('+ o.responseText + ')');
	      		for(var i = 0;i < jsonObj.option.length;i++){
					//server.add(new Option(jsonObj.option[i].name,jsonObj.option[i].id));
					server.options[server.options.length]=new Option(jsonObj.option[i].name,jsonObj.option[i].id);
				}
		}
	});
}

function casCadeGameRole(){
	var url = 'casCadeGameRole.action';
	var params = "gameId="+$('gameId').value;
	aj(url,params,function(o){
		var server = $('roleNames');
		server.options.length = 0;
	    if(o.responseText.length > 0){
			var jsonObj = eval('('+ o.responseText + ')');
	      		for(var i = 0;i < jsonObj.option.length;i++){
					//server.add(new Option(jsonObj.option[i].grName,jsonObj.option[i].grid));
					server.options[server.options.length]=new Option(jsonObj.option[i].grName,jsonObj.option[i].grid);
				}
		}
	});
}

function ontjrole(o){
	$("tjrole").style.display="block";
	$("tjrole").style.left=window.event.x-150;
}

function oncloserole(){
	var a=$("tjrole");
	a.style.display="none";
	$("nickName").value="";
	$("roleNames").value="";
}

function onclubsave(){
	if($("roleNames").value=="")
	{
		alert("请选择职业!");
		return;
	}
	if($("nickName").value=="")
	{
		alert("请输入昵称!");
		return;
	}
	var url = 'clubRole.action';
	XMLHttp.formSubmit("clubForm",url,
	function(o){
		var msg=o.responseText;
		if(msg=="true")
		{
			alert("添加成功!");
			selectRole();
			oncloserole();
		}else
			alert(msg);
	});
}

function oncomeclub(){
	var a=$("clubName");
	if(a.value==""){
		alert("请选择社团!")
		return;
	}
	visitClub(a.value);
	//TODO
	//onopen("comeClub.action?clubId="+a.value);
}

function onjoinclub(){
	var clubValue=$("clubName").value;
	var roleValue=$("roleName").value;
	if(clubValue==""){
		alert("请选择社团!");
		return;
	}
	if(roleValue==""){
		alert("请选人物!");
		return;
	}
	
	var url = 'joinClub.action';
	var params = "clubId="+clubValue.split("-")[0]+"&roleId="+roleValue+"&branchId="+clubValue.split("-")[1];
	aj(url,params,function(o){
		var msg=o.responseText;
		if(msg=="true")
			alert("请等待社长的批准!");
		else
			alert(msg);
	});
}

function parseUrl(){
	var url = document.location+"";
	alert(document.location);
	if(url.indexOf('.club.ngamebar.com')>0){
		var domain = url.substring(7,url.indexOf('.club.ngamebar.com')+18);
		var action = 'clubRedirect.action?domain='+domain;
		new Ajax.Request(action,
		{
			method:'GET',
			onComplete:redirectClub,
			asynchronous:true		
		});		
	}else
		$('ifr').src='clubDefault.action';

}

function redirectClub(res){
	if(res.responseText.length==0) return;
	visitClub(res.responseText);
}
