//=========================================================================================
var _SPLITSTR = "#$$#";
var _passport = getCookieValue('username');
var _password=getCookieValue('password');
var _isUserLogin = judgeUserLogin();
if(_passport && ""!=_passport)
   _passport = decodeURI(_passport);
else
  _passport = null;

/**取得cookie的值**/
function getCookieValue(varname)
{
	if(varname)
	{
	  var a=document.cookie.indexOf(varname+"=");
	  if(a!=-1)
	   return document.cookie.substring((a+varname.length+1),document.cookie.length).split(";")[0];
	  else
		  return "";
  }
}

/**判断用户是否登陆**/
function judgeUserLogin()
{
   if(!_password)
         return false;
    else
       return true;
}




//=========================================================================================


//========================显示头部 start===================================================
showTop();
/**为用户参加的公会部分信息**/
function init_myGuildInfo()
{
  if(_isUserLogin==false)
    return;
  var scriptBlock  = document.createElement("script");
  var requrl = "http://admin.gh.duowan.com/ajaxjsp/forshowtop.jsp?type=2";
	scriptBlock.src = "";
	scriptBlock.src = requrl;
	scriptBlock.type = "text/javascript";
	scriptBlock.language = "javascript";
	document.getElementsByTagName("head")[0].appendChild(scriptBlock);
}

function init_useNews()
{
  if(_isUserLogin==false)
    return;
  var scriptBlock  = document.createElement("script");
  var requrl = "http://admin.gh.duowan.com/ajaxjsp/forshowtop.jsp?type=4";
	scriptBlock.src = "";
	scriptBlock.src = requrl;
	scriptBlock.type = "text/javascript";
	scriptBlock.language = "javascript";
	document.getElementsByTagName("head")[0].appendChild(scriptBlock);
  
}

function showTop(){
	if(_need_initMyGuildInfos()==true)
	{
	   init_myGuildInfo();

	     init_useNews();
	}
	 
	   
	 var unread_msgnums= getCookieValue('gh_newmsgnums');
	if(""==	unread_msgnums)
		unread_msgnums=0;
	var showstr;
	var mid = _passport;//通行证
	_passport=encodeURI(_passport);
	var ghmyinfourl = getCookieValue('gh_pphash');//我的信息页面地址
	if(ghmyinfourl && ghmyinfourl!=""){
	 ghmyinfourl = "http://gh.duowan.com/my/"+ghmyinfourl+"/";
	
	}
	var str0 =  '多玩通行证:<input class="linput" name="username"/>密码:<input type="password" name="passwd" class="linput"/>'+
	            '<input type="submit" value="" class="lbtn"/><a href="http://udb.duowan.com/register.jsp" target="_blank">注册多玩通行证</a></span>';
	var str1 =  mid + ' |<a href="http://admin.gh.duowan.com/ajaxjsp/logout.jsp?url=http://gh.duowan.com/">退出</a>|'+
	 					  '<a href="javascript:;" class="amg" id="amg">我的公会</a>|'+
							'<a id="mypagehref" href="http://admin.gh.duowan.com/my/index.jsp?passport='+_passport+'">个人信息</a>'+
							'|<a id="msghref" href="http://admin.gh.duowan.com/msg/inbox.jsp" target="_blank">('+unread_msgnums+')条消息未读</a>';
	if (_isUserLogin)
	{
	  showstr = str1;
	}
	else 
	{
	  showstr = str0;
	}
	var topstr =  '<div id="myguilds" style="display: none;"></div>'+
	              '<form name="form1" action="http://udb.duowan.com/login.jsp" method="post" target="_self">'+
					      '<div class="toplogin">'+
	              '<div class="login">'+
	              '<a href="http://gh.duowan.com/" target="_blank"><h2 class="ghlogo">'+
	              '多玩公会系统'+
	              '</h2></a>'+
	              '<div class="login_l" id="login_l">'+
	              showstr+
	              '</div>'+
	              '<div class="clearfix"> </div>'+
	              '</div>'+
	              '</div>'+
	              '<input id="backurl" type="hidden" value="'+location.href+'" name="backurl"></form>';
	document.write( topstr );
	var amg = document.getElementById("amg");
	var myguilds = document.getElementById("myguilds");
	var myguild = getCookieValue('gh_myghids').split(_SPLITSTR);//我参加的公会的名称
	var myghnames = getCookieValue('gh_myghnames').split(_SPLITSTR);//我参加的公会的名称
  if( myguild && myguild.length>0 && myguild[0]!="")
  {
	  myguilds.innerHTML = "" ;
	  for( var i=0; i<myguild.length; i++ )
	  {
	     var tempgs = myguild[i].split(":");
	     if(tempgs[1]=="2")
	        continue;
	     myguilds.innerHTML += '<a href="http://gh.duowan.com/'+tempgs[0]+'">'+decodeURI(myghnames[i])+'</a>';
	  }
	  for( var i=0; i<myguild.length; i++ ){
	     var tempgs = myguild[i].split(":");
	     if(tempgs[1]=="1")
	        continue;
	     myguilds.innerHTML += '<a href="http://gh.duowan.com/'+tempgs[0]+'">'+decodeURI(myghnames[i])+'  待批中</a>';
	  }	  
	  
	  if(amg)amg.style.visibility="visible";	
	}
	else
	{
	  if(amg)amg.style.visibility="hidden";	
	}
	var timer = null;
	window.onresize = init_myguilds;
	if(amg){
	  amg.onmouseover = init_myguilds;
	  amg.onmouseout = hide_myguilds;
	  document.getElementById("login_l").style.margin = "3px 0 0 0";
	}
	
	myguilds.onmouseout= function(){
	  clearTimer();
	  hide_myguilds();
	}
	
	myguilds.onmouseover = function(){
		clearTimer();
	}
  
	function clearTimer(){
	   if( timer ) window.clearInterval(timer);
		 timer = null;
	}

	function hide_myguilds(){
	   clearTimer();
		 timer = window.setInterval(hide_myguild,100)
	}
	
	function hide_myguild(){
	   clearTimer();
		myguilds.style.display = "none";
	}

	function init_myguilds(){
		clearTimer();
		myguilds.style.display = "block";
		myguilds.style.left = amg.parentNode.offsetLeft +  amg.offsetLeft + "px";
	}
	
	function _need_initMyGuildInfos(){
	   var _myguilds = getCookieValue('gh_myghnames');
	   if(!_myguilds || _myguilds=="")
	   {
	      return true;
	   }
	   else
	   {
	      return false;
	   }
	}
	
}

  /**显示我参加的公会**/
	function showMyGuilds() 
	{
		if(_isUserLogin==false)
		  return;
		var amg = document.getElementById("amg");
		var myguilds = document.getElementById("myguilds");
		
		var myguild = getCookieValue('gh_myghids').split(_SPLITSTR);//我参加的公会的名称
		var myghnames = getCookieValue('gh_myghnames').split(_SPLITSTR);//我参加的公会的名称
	  if( myguild && myguild.length>0 && myguild[0]!="")
	  {
		  if(myguilds)myguilds.innerHTML = "";	
		  for( var i=0; i<myguild.length; i++ )
		  {
		     var tempgs = myguild[i].split(":");
		     if(tempgs[1]=="2")
		        continue;
		     myguilds.innerHTML += '<a href="http://gh.duowan.com/'+tempgs[0]+'">'+decodeURI(myghnames[i])+'</a>';
		  }
		  for( var i=0; i<myguild.length; i++ )
		  {
		     var tempgs = myguild[i].split(":");
		     if(tempgs[1]=="1")
		        continue;
		     myguilds.innerHTML += '<a href="http://gh.duowan.com/'+tempgs[0]+'">'+decodeURI(myghnames[i])+'  待批中</a>';
		  }	  
		  if(amg) 
		     amg.style.visibility = "visible";	
		}
		else
		{
		  if(amg)amg.style.visibility="hidden";	
		}
		var mypagehref = document.getElementById('mypagehref');
		var ghmyinfourl = getCookieValue('gh_pphash');//我的信息页面地址
		if(mypagehref && ghmyinfourl && ghmyinfourl!="" )
		{
	     ghmyinfourl = "http://gh.duowan.com/my/"+ghmyinfourl+"/";
	     mypagehref.href = ghmyinfourl;
		}
	}
//========================显示头部   end ====================================================

//========================设置会员登陆时间 start==============================================
setLastLoginTime();
function getGuildIdFromHref(){
	nowurl = location.href;
	var result1 = nowurl.match(/^(http:\/\/)?gh\.duowan\.cn\/(\w{4,20})(\/.*)?$/i);
	var guildid = "";
	var specialstr = ",css,image,imgs,js,msg,my,simg,skin,attachment,logo,news,lajigonghui,";
	if(result1)
	{
	  guildid = result1[2];
	}
	else
	{
	  var result2 = nowurl.match(/^(http:\/\/)?admin\.gh\.duowan\.cn\/(.*)?$/i);
	  if(!result2)return;
	  var gpart = result2[2];
	  var result3 = gpart.match(/guildid=(\w{4,20})/i);
	  if(result3)
	    guildid = result3[1];
	}
	if(specialstr.indexOf(","+guildid+",")==-1)
	{
	  return guildid;
	}
	
}

function permitSetLastLoginTime(guildid){
  if(_isUserLogin==false)return false;
	if(!guildid || guildid=="" )
    return false;
  var hasSets = getCookieValue("gh_setguilds");
  var partguilds = getCookieValue("gh_myghids");
  hasSets = ","+hasSets+",";
  partguilds  = ","+partguilds+",";
  if(!partguilds || partguilds.indexOf(","+guildid+":1")==-1)
  {
   return false;
  }
  if(hasSets && hasSets.indexOf(","+guildid+",")!=-1)
  {
    return false;
  }
  return true;
}

function setLastLoginTime(){
	var guildid = getGuildIdFromHref();
	if(permitSetLastLoginTime(guildid)==false)
	  return;
  var requrl = "http://admin.gh.duowan.com/ajaxjsp/forshowtop.jsp?type=3&guildid="+guildid;
  requrl += "&setguildids="+encodeURI(getCookieValue("gh_setguilds"));
  var scriptBlock  = document.createElement("script");
	scriptBlock.src = "";
	scriptBlock.src = requrl;
	scriptBlock.type = "text/javascript";
	scriptBlock.language = "javascript";
	document.getElementsByTagName("head")[0].appendChild(scriptBlock);
}
//========================设置会员登陆时间 end  =============================================


//========================显示公会主要信息左侧按钮 start =====================================

function showButtonHref(guildid){
  //alert('====guildid:'+guildid);
  if(!guildid || guildid=="" || _isUserLogin==false)
    return ;
  var jbtn = document.getElementById("ajoinguild");
  var ebtn = document.getElementById("aexitguild");
  var isMember = getIsGuildMember(guildid);
	if(isMember && ebtn){
	   //alert('==== show ebtn ');
	   ebtn.style.display = "block";
	   if(jbtn)
	     jbtn.style.display = "none";
	}
	else if(!isMember && jbtn){
	   //alert('==== show jbtn ');
	   jbtn.style.display = "block";
     if(ebtn)	   
       ebtn.style.display = "none";
	} 
}

function exitGuild(guildid){
	if(_isUserLogin==false){
		 alert('抱歉,您还没有登陆不能进行退会操作！');
	   reutrn;
	}
  else if(!confirm("确定要退出本公会么？"))
    return;
  else if(!guildid || guildid==""){
    alert("没有提供要退出的公会域名id！");
    return;
  }
  var tfobj = document.createElement("form");
  var tinput = document.createElement("input");
  tinput.type = "text";
  tinput.name = "guildid";
  tinput.value = guildid;
  tfobj.action = "http://admin.gh.duowan.com/quit_guild.jsp?guildid="+guildid;
  tfobj.target = "_self";
  tfobj.appendChild(tinput);
  document.body.appendChild(tfobj);
  tfobj.submit();
}

function getIsGuildMember(guildid){
	if(_isUserLogin==false)
	   return false;
  var isMember="no";
  var myguild = getCookieValue('gh_myghids').split(_SPLITSTR);//我参加的公会的名称
  var tempguildid = guildid+":1";
  for(var i=0;myguild!=null && i<myguild.length;i++)
    if(tempguildid == myguild[i]){
       isMember="yes";
       break;
    }
  if(isMember=="yes")
    return true;
  else
    return false;
}
//========================显示公会主要信息左侧按钮 start =====================================
