﻿function addfriend(he,allow,fid)
{
    if(user_id=="None"){
        alert("请先登录");
        return null;
    }
    var e=document.getElementById('reqmsg');
    var f=document.getElementById(fid);
    if(allow==0)
    {
        alert("这个用户不允许任何人加为好友");
        return null;
    }
    else if(allow==2)
    {
        var showstr="用户要求验证<br/><textarea id='msgcontent' name='content' rows='2' cols='31'></textarea><br/>";
        showstr+="<a href='javascript:void(0)' onclick=addfriend("+he+",3,'"+fid+"')>发送请求</a> <a href='javascript:void(0)' onclick=\'$(\"#reqmsg\").fadeOut(600)\'>关闭</a>";
		e.innerHTML=showstr;
	    displayposition(e,f);
		return null;
    }
    else if(allow==1 || allow==3)
    {
        var data={he:he,me:user_id};
        if(allow==3)data.content=document.getElementById("msgcontent").value;
		e.innerHTML="<img src='/site_media/images/load.gif' width='14' /> 正在发送请求...";
	    displayposition(e,f);
        $.post("/addtofriends/",data,function(){
		    if(allow==1)$("#"+f.id).replaceWith("已加为好友");
		    else{
		        $("#"+f.id).replaceWith("好友请求已发送");
		        e.innerHTML='请求已发送';
		    }
		    $("#reqmsg").fadeOut("slow");
        });
    }
}

function talk(me,heid)
{
    if(me==""){
        alert("请先登录");
        return null;
    }
    var randomUrl;
    var g=document.getElementById("textarea");
    //var h=document.getElementById("checkcode");
    if(g.value=="")
    {
        alert("不能发送空消息!");
        return null;
    }
    var p=document.getElementById("private");
    var r=document.getElementById("receiver");
    if(heid>0)randomUrl="/msg/"+heid+"/talk/";
    else randomUrl="/msg//talk/";
    var content=g.value.replace(/\r\n/g, "<br/>");
    var querystring ={content:content,private:p.checked,namelist:r.value};
    //querystring+='&checkcode='+h.value;
    var item=document.getElementById("friend");
    for(var i=0;i<item.options.length;i++)
    {
        if(item.options[i].selected)querystring.namelist+=" "+item.options[i].value;
    }
    var theme='无';
    if(document.getElementById("theme"))theme=document.getElementById("theme").value;
    querystring.name=theme;
	var e=document.getElementById('sendbutton');
	e.value="正在发送...";
	$.post(randomUrl,querystring,function(showstr){
	    if(showstr=='&q%')alert('防灌水系统已打开，10秒内不能重复提交');
		else if(showstr=='&p%')alert('验证码错误');
		else
		{
		    showstr=showstr.slice(0,-7);
		    if(heid>0)
		    {
		        //document.getElementById("checkimg").src='/get_check_code_image/?nocache='+Math.random();
		        alert('消息已发送!');
		    }
		    else if(document.getElementById("pagetype").value==0)
		    {
		        if(theme=="无")theme="";
			    he.innerHTML ='<li>'+theme+' <a href="/msg/'+me+'/">'+me+'</a> '+showstr+'</li>'+he.innerHTML;
			    //document.getElementById("checkimg").src='/get_check_code_image/?nocache='+Math.random();
			}
			else if(document.getElementById("pagetype").value==1)window.location.reload();
			else if(document.getElementById("pagetype").value==2)alert("消息发送成功！");
			g.value='';
			//h.value='';
			if(document.getElementById("theme"))document.getElementById("theme").value='';
			r.value='';
			p.checked=false;
			e.value='发送';
        }
	});
}

function initajaxprofile()
{
    $("#ajaxprofile").mouseover(function(){
        this.style.display="block";}).mouseout(function(){
            this.style.display="none";});
    $('ul.picul img.lbimg[id]').mouseout(function(){
        $('#ajaxprofile').css("display","none")}).mouseover(function(){
            showprofile(this.id.slice(3))});
}

function change_gender(value)
{
    if(value == 'M')return '男'
    else if(value=='F')return '女'
    else if(value=='S')return '保密'
}

function change_race(value)
{
    if(value == 'H')return '人类'
    else if(value=='N')return '精灵'
    else if(value=='O')return '兽人'
    else if(value=='U')return '亡灵'
    else if(value=='T')return '人族'
    else if(value=='P')return '神族'
    else if(value=='Z')return '虫族'
    else if(value=='R')return '随机'
}
        
function change_time(value)
{
    if(value)
        return Math.floor(value/1440)+'天'+Math.floor(value%1440/60)+'小时'+value%60+'分钟'
    else return '未知'
}

function userinfo(showhtml,obj,needbr)
{
    if(obj=="")showhtml+='<span class="username">游客</span>';
    else{
        if(needbr)showhtml+='<br/>';
        var level=(obj.time+obj.hot)/(24*60);
        var small=Math.ceil((1-level+Math.floor(level))*82);
        showhtml+='<span class="username">'+obj.name+'('+obj.firstname+' '+obj.lastname+')</span> ';
        if(needbr)showhtml+='<br/>';
        showhtml+='<span style="background-position:-'+small+'px 50%;" class="level">等级'+obj.level+'</span> <a class="blog" href="/blog/'+obj.id+'/">进入博客</a> ';
        if(user_id!=String(obj.id))showhtml+='<a class="friend" id="isfriend'+Math.pow(10,18)*Math.random()+'" href="javascript:void(0)" onclick="addfriend('+obj.id+','+obj.allowfriend+',this.id)">加为好友</a> ';
        if(needbr)showhtml+='<br/>';
        showhtml+='<span class="gender">性别</span><span class="time">'+change_gender(obj.gender)+'</span> <span class="race">种族</span><span class="time">'+change_race(obj.race)+'</span>';
        if(needbr)showhtml+='<br/>';
        showhtml+=' <span class="arti">文章</span><span class="time">'+obj.arti+'</span>';
        showhtml+=' <span class="rep">录像</span><span class="time">'+obj.rep+'</span>';
        showhtml+=' <span class="cmt">评论</span><span class="time">'+obj.cmt+'</span>';
        if(needbr)showhtml+='<br/>';
        showhtml+=' <span class="rp">人品</span><span class="time">'+obj.rp+'</span> <span class="money">财富</span><span class="time">'+obj.money+'</span>';
        showhtml+=' <span class="date">注册时间</span><span class="time">'+obj.reg+'</span> <span class="date">在线时间</span>';
        showhtml+='<span class="time">'+change_time(obj.time)+'</span> <span class="date">上次登录</span><span class="time">'+obj.login+'</span>';
        if(needbr)showhtml+='<br/>';
	    showhtml+=' <span class="bn">战网ID</span><a href="/bn/'+obj.bn+'/" target="_blank">'+obj.bn+'</a> ';
        showhtml+=' <span class="bn">VSID</span><a href="http://www.vsa.com.cn/user/user_warrpg/check/check_user_situation.jsp?userName='+obj.vs+'" target="_blank">'+obj.vs+'</a>';
    }
    return showhtml;
}

function userinfo2(showhtml,obj,ajax)
{
    var headstr="";
    if(ajax)headstr="ajax";
    else headstr="";
    showhtml+='<div class="tab" style="width:100%"><ul><li class="tabcurrent"><a title="#'+headstr+'art'+obj.id+'">热门文章</a></li>';
    showhtml+='<li><a title="#'+headstr+'rep'+obj.id+'">热门录像</a></li><li><a title="#'+headstr+'cmt'+obj.id+'">热门评论</a></li></ul></div>';
    showhtml+='<div style="clear:both" class="tabcc" id="'+headstr+'art'+obj.id+'">';
    for(var i=0;i<obj.hotarti.length;i++)showhtml+='<a href="/sns/article/'+obj.hotarti[i].id+'">'+obj.hotarti[i].name+'</a><br/>';
    showhtml+='</div><div id="'+headstr+'rep'+obj.id+'" style="display:none;clear:both" class="tabcc">';
    for(var i=0;i<obj.hotrep.length;i++)showhtml+='<a href="/sns/rep/'+obj.hotrep[i].id+'">'+obj.hotrep[i].name+'</a><br/>';
    showhtml+='</div><div id="'+headstr+'cmt'+obj.id+'" style="display:none;clear:both" class="tabcc">';
    for(var i=0;i<obj.hotcmt.length;i++)showhtml+='<a href="/sns/cmt/'+obj.hotcmt[i].id+'">'+obj.hotcmt[i].name+'</a><br/>';
    showhtml+='</div>';
    return showhtml;
}

function inittab()
{
    $("div.tab a").click(function(){
        $(this).parent().siblings().removeClass("tabcurrent");
        $(this).parent().addClass("tabcurrent");
        $(this.title).siblings('.tabcc').css("display","none");
        $(this.title).fadeIn(600);
    }).attr("href","javascript:void(0)");
}

function showprofile(id)
{
    var e=document.getElementById('ajaxprofile');
    e.innerHTML="<img src='/site_media/images/load.gif' width='14' /> 正在加载...";
    var f=document.getElementById('man'+id);
    displayposition(e,f);
    $.getJSON("/ajaxprofile/"+id+"/?nocache="+Math.random(),function(obj){
        var showhtml="";
        e.style.display="none";
        showhtml=userinfo(showhtml,obj,true);
        showhtml+='<br/>签名档<div style="width:100%">'+obj.qmd+'</div>';
        showhtml=userinfo2(showhtml,obj,true);
		showhtml+='<a href="javascript:void(0)" onclick="$(\'#ajaxprofile\').fadeOut(600);">关闭</a>';
		e.innerHTML=showhtml;
		if(obj.id)inittab();
		$("#ajaxprofile").fadeIn(600);
    });
}

function msgreply()
{
    document.getElementById('receiver').value=document.getElementById('sender').innerHTML;
    var showhtml='\n\n回复：\n'+document.getElementById("msgcontent").innerHTML+'\n'+document.getElementById("msgtime").innerHTML;
    document.getElementById('textarea').value=showhtml.substr(0,500);
    document.getElementById("theme").value='回复：'+document.getElementById("msgtheme").innerHTML;
    document.getElementById('ctrlMsg').style.display='none';
    document.getElementById('sendMsg').style.display='block';
}

function msgtr()
{
    var showhtml='\n\n回复：\n'+document.getElementById("msgcontent").innerHTML+'\n'+document.getElementById("msgtime").innerHTML;
    document.getElementById('textarea').value=showhtml.substr(0,500);
    document.getElementById("theme").value='转发：'+document.getElementById("msgtheme").innerHTML;
    document.getElementById('receiver').value='';
    document.getElementById('ctrlMsg').style.display='none';
    document.getElementById('sendMsg').style.display='block';
}