﻿var datalist = new Object();
var gidlist = new Object();
var zoneobj = {"top":"上路","bottom":"下路","middle":"中路","other":"游/野"};
var team1obj,team2obj;
var showdescription = $("showdescription");
var descriptionbody = $("descriptionbody");
var itemdes = new Object();
function insertplayerdata(id) {
    
    var o = $(id);
    if (!o) return;
    var gid = id.substr(10);
    var hero = new Array();
    var skill = new Array();
    var build = new Array();
    var unit = new Array();
    var item = new Array();
    o = o.childNodes;
    var player = new Object();
    player.playername = o[0].innerHTML;
    player.teamid = o[1].innerHTML;

    var os = o[2].innerHTML.split(",");
    player.id = id;
    player.hero = os[0];
    player.heroname = os[1];
    player.description = convertdata(os[2]);
    player.apm = o[3].innerHTML;
    player.color = o[4].innerHTML;
    player.action = o[5].innerHTML;
    player.location = o[6].innerHTML;
    player.zone = o[7].innerHTML;
    player.kill = o[8].innerHTML.split(",");
    
    
    var li = $C("li");
    li.id = "hero" + id;
    li.innerHTML = "<img src='" + Uimage + os[0] + ".gif' onmouseover='heroimagemove(this)' alt='' />";
    li.onclick = clickplayerhero;

    //li.onmousemove = movedescription;
    $("playerlist" + player.teamid).appendChild(li);
    
    rebuilditem();
    player.money = 0;
    for (var j = 9 ; j < o.length ; j ++) {
        var ss = o[j].innerHTML.split(",");
        if (ss[0] == "h")
            hero[hero.length] = ss;
        else if (ss[0] == "s") {
            skill[skill.length] = ss;
            if (!itemdes[ss[1]]) {
                var itemobj = new Object();
                itemobj.title = ss[2];
                itemdes[ss[1]] = itemobj;
            }
        } else if (ss[0] == "d") {
            if (ss[1] == "1") 
                player.kill[4] = ss[2];
            if (ss[1] == "2") 
                player.enditem = ss[2];
        } else if (ss[0] == "b")
            build[build.length] = ss;
        else if (ss[0] == "u")
            unit[unit.length] = ss;
        else if (ss[0] == "i") {
            item[item.length] = ss;
            var itemobj = new Object();
            player.money += parseInt(ss[4]);
            itemobj.money = parseInt(ss[4]);
            var additem = checkdotaitem(ss);
            itemobj.title = ss[2];
            if (additem.length > 0) {
                item[item.length] = additem;
                player.money += parseInt(additem[4]);
                itemobj.money = parseInt(additem[4]);
            }
            itemdes[ss[1]] = itemobj;
        }
    }
    player.level = skill.length;
    if (player.level > 25) player.level = 25;
    player.skill = skill;
    player.build = build;
    player.unit = unit;
    player.item = item;
    datalist[id] = player;
    gidlist[gid] = player;
    var div = $C("div");
    div.className = "pdataitem";
    div.id = "pd" + player.id;
    var divdata;
    divdata = "<span class='playercolor' style='background-color:" + player.color + "'></span><span class='playername'>" + player.playername + "</span>";
    divdata += "<br />(" + player.heroname + " - Level:<span class='playerlevel'>" + player.level + "</span>)";
    
    os = player.apm.split(",");
    divdata += "<br /><span class='playerapm'>Min:" + os[2] + " | Ave:" + os[1] + " | Max:" + os[0] + "</span><br /><strong>" + zoneobj[player.zone] + "</strong>";
    divdata += "  正:"+ player.kill[0] +" 反:"+ player.kill[1] +" <span id='kill"+gid+"'>杀:"+ player.kill[2] +" 死:"+ player.kill[3]+"</span>";
    if (player.kill.length > 4)
        divdata += " 助:" + player.kill[4];
    
    div.innerHTML = divdata;
    
    if (!$("teamdata" + player.teamid)) {
        var cd = $C("div")
        cd.id = "teamdata" + player.teamid;
        $("playerbody" + player.teamid).appendChild(cd);
    }
    
    $("teamdata" + player.teamid).appendChild(div);
    
}
function parseMessage() {
    var ul = $("chatdata").getElementsByTagName("ul");
    
 
    var team = new Object();
    team["1"] = [0,0,0,0,0,0,0,0,0];
    team["2"] = [0,0,0,0,0,0,0,0,0];
    for (var i = 0 ; i < ul.length ; i ++) {
        
        if (!ul[i].name) continue;
        var ss = ul[i].name.split(':');
        var p = gidlist[ss[1]];
        if (p) p.kill[3]=parseInt(p.kill[3])+1;
        p = gidlist[ss[2]];
        if (p) p.kill[2]=parseInt(p.kill[2])+1;
        
    }
       
    for (var gid in gidlist) {
        var player = gidlist[gid];
        $("kill"+gid).innerHTML = "杀:" + player.kill[2] + " 死:" + player.kill[3];
        
        var ar = team[player.teamid];
        //杀
        ar[2] += parseInt(player.kill[2]);
        //死
        ar[3] += parseInt(player.kill[3]);
        ar[6] ++;

    }
    insertteamdata("杀死",team["1"][2],team["2"][2],ar[6]);
    insertteamdata("死亡",team["1"][3],team["2"][3],ar[6]);
    
}
function initItemDescription() {
    var des = $("itemdescription").getElementsByTagName("li");
    for (var i = 0 ; i < des.length ; i ++) {
        var os = des[i].innerHTML;
        var n = os.substr(0,4);
        if (!itemdes[n]) itemdes[n] = {};
        itemdes[n].des = convertdata(os.substr(5,os.length - 5));
    }
}
function showdotawinner() {
    //parseMessage();
    if (WinTeam == 1)
        alert("Winner is :\r\n\r\n\t近卫军团\t");
    else
        alert("Winner is :\r\n\r\n\t天灾军团\t");
}
function clickplayerhero() {
    
    var herobody = $("body" + this.id);
    player = datalist[this.id.substr(4)];
    var playerbody = $("playerbody" + player.teamid);
    this.className = "clickplayerhero" + player.teamid;
    if (!herobody) {
        //var hero = player.hero.split(",");
        herobody = $C("div");
        herobody.className = "herobody";
        herobody.id = "body" + this.id;
        $("playerbody" + player.teamid).appendChild(herobody);
        
        var hb = "<div class='closeherobody' onclick='closeherodata(\""+ player.teamid +"\")'></div><img src='"+ Uimage + player.hero +".gif' alt='"+ player.heroname +"' class='cphero' />";
        hb += $("pd" + this.id.substr(4)).innerHTML;
        
        var ac = player.action.split(",");
        hb += "<div class='partline'><span>Action</span></div><ul class='txtlist' style='height:60px;'>";
        hb += "<li>基本命令:"+ ac[2] +"</li><li>选择单位:"+ ac[4] +"</li><li>选择编队:"+ ac[8] +"</li><li>攻击指令:"+ ac[13] +"</li><li>右键点击:"+ ac[1] +"</li><li style='color:red'>装备值:"+ player.money +"</li></ul>";
        hb += "<div class='partline'><span>Skill</span></div><ul class='imagelist' style='height:110px;'>";
        var level = 0;
        var skilllevel = new Object();
        for (var i = 0 ; i < player.skill.length ; i ++) {
            var skill = player.skill[i];
            level++;
            if (!skilllevel[skill[1]])
                skilllevel[skill[1]] = 1;
            else
                skilllevel[skill[1]]++;
                
            hb += "<li><img src='" + Simage + skill[1] + ".gif' width='25' height='25' onmouseover='skillimagemove(this,"+ skilllevel[skill[1]] +","+level+",\""+skill[3]+"\")' alt='' /></li>";
        }
        hb += "</ul><div class='partline'><span>Item</span></div><ul class='imagelist itembody'>";
        for (var i = 0 ; i < player.item.length ; i ++) {
            var item = player.item[i];
            if (item[1] == "h085") continue;
            hb += "<li><img src='" + Iimage + item[1] + ".gif' onmouseover='itemimagemove(this,\""+ item[3] +"\")' width='25' height='25' alt='' /></li>";
        }
        hb += "</ul>";
        herobody.innerHTML = hb;
    }
    
    
    if (player.teamid == "1") {
        if (team1obj == "body" + this.id) {
            closeherodata(player.teamid);
            return;
        }
        if (team1obj) {
            $(team1obj).style.display = "none";
            $(team1obj.substr(4)).className = "";
        }
        team1obj = "body" + this.id;
        
    } else {
        if (team2obj == "body" + this.id) {
            closeherodata(player.teamid);
            return;
        }
        if (team2obj) {
            $(team2obj).style.display = "none";
            $(team2obj.substr(4)).className = "";
        }
        team2obj = "body" + this.id;
    }
    $("teamdata" + player.teamid).style.display = "none";
    herobody.style.display = "block";
    
}
function closeherodata(teamid) {
    if (teamid == "1") {
        $(team1obj).style.display = "none";
        $(team1obj.substr(4)).className = "";
        team1obj = "";
    } else {
        $(team2obj).style.display = "none";
        $(team2obj.substr(4)).className = "";
        team2obj = "";
    }
    $("teamdata" + teamid).style.display = "block";

}
var replaymenu = "gameinfo";
function clickreplaymenu(obj, id, data) {
    var os = $("replaymenu").getElementsByTagName("li");
    for (var i = 0 ; i < os.length ; i ++) {
        os[i].className = "";
    }
    obj.className = "clickit";
    $(replaymenu).style.display = "none";
    $(id).style.display = "block";
    replaymenu = id;

    if ($("vodbody")) {
        if (id == "vodbody") {
            insertVOD(data);
        } else {
            $("vodbody").innerHTML = "";
        }
    }
    //if (!$("appendmessage")) appendmessage();
}
function insertVOD(data) {

    if (data.length < 10) {
        $("vodbody").innerHTML = '<div style="text-align:center;padding:20px 0px;">本录像暂无VOD</div>';
        return;
    }
    var d = data.split('|');
    var pstr = "";

    if (d.length > 1) {
        pstr = d[1];
        if (d.length > 2)
            pstr = '<a href="' + d[2] + '" target="_blank" style="color:red">' + d[1] + '</a>';

        pstr = "<br><b>以上视频由 <span style='color:red'>" + pstr + "</span> 提供</b>";
    }
    $("vodbody").innerHTML = '<div style="text-align:center"><embed src="' + d[0] + '" style="margin:0px 0px;" width="610" height="500" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" name="" allowFullScreen="true"></embed>' + pstr + '</div>';
}
function initDotaReplay(replayid) {
    rid = replayid;
    
    var cno = $C("div");
    cno.className = "commendshow";
    cno.id = "commendshow";
    cno.innerHTML = CommendNo;
    cno.title = "已经有" + CommendNo + "位玩家评定该录像很强大.";
    cno.onclick = nicegame;
    cno.onmouseover = function(){this.style.backgroundPosition = "0px 87px"};
    cno.onmouseout = function(){this.style.backgroundPosition = "0px 0px"};
    $("gameinfo").getElementsByTagName("div")[0].appendChild(cno);

    var team = new Object();
    team["1"] = [0,0,0,0,0,0,0,0,0];
    team["2"] = [0,0,0,0,0,0,0,0,0];
    var teamabp = [];
    var teambbp = [];

    var zg = false;
    for (var p in datalist) {
        var player = datalist[p];
        var ar = team[player.teamid];
        if (player.build.length > 0) {
            if (player.teamid == 1)
                teamabp = player.build;
            else
                teambbp = player.build;
        }
        //正
        ar[0] += parseInt(player.kill[0]);
        //反
        ar[1] += parseInt(player.kill[1]);
        //杀
        ar[2] += parseInt(player.kill[2]);
        //死
        ar[3] += parseInt(player.kill[3]);
        //等级
        ar[4] += player.level;
        //金钱
        ar[5] += player.money;
        ar[6] ++;
        //操作数
        ar[7] += parseInt(player.action.split(',')[0]);
        //助
        if (player.kill.length > 4) {
            zg = true;
            ar[8] += parseInt(player.kill[4]);
        }
    }
    insertteamdata("正补",team["1"][0],team["2"][0],ar[6]);
    insertteamdata("反补",team["1"][1],team["2"][1],ar[6]);
    insertteamdata("杀死",team["1"][2],team["2"][2],ar[6]);
    insertteamdata("死亡",team["1"][3],team["2"][3],ar[6]);
    if (zg) insertteamdata("助攻",team["1"][8],team["2"][8],ar[6]);
    insertteamdata("英雄等级",team["1"][4],team["2"][4],ar[6]);
    insertteamdata("金钱",team["1"][5],team["2"][5],ar[6]);
    insertteamdata("操作数",team["1"][7],team["2"][7],ar[6]);
    if (ie) {
        var os = $("teamdata").getElementsByTagName("li");
        for (var i = 0 ; i < os.length ; i ++) os[i].style.backgroundImage = "none";
    }
    if (teamabp.length > 0 && teambbp.length > 0) {
        var li = $C("li");
        li.innerHTML = "<span class=\"left_bt_h1_l\"></span><span class=\"left_bt_h1_r\"></span><strong>Ban / Pick</strong>";
        li.onclick=function(){clickreplaymenu(this,'banpickdata')};
        var ap = $("replaymenu");
        ap.appendChild(li);
        
        div = $C("div");
        div.id = "banpickdata";
        div.style.display = "none";
        
        ap.parentNode.appendChild(div);
        var sta = parseInt(teamabp[0][3].replace(":",""));
        var stb = parseInt(teambbp[0][3].replace(":",""));
        var st = 1;
        if (stb < sta) st = 0;
        var str = "(BAN)禁止选择";
        var t = "<br /><b>近卫 BAN/PICK :</b><hr />";
        for (var i = 0 ; i < teamabp.length ; i ++) {
            t += "<img onmouseover=\"description(this,'"+teamabp[i][2]+"','"+str+"','"+Uimage+teamabp[i][1]+".gif','"+teamabp[i][3]+"')\" src='"+Uimage+teamabp[i][1]+".gif' style='width:50px;";
            if (i % 2 == st) t += "margin-left:20px;";
            t += "' alt='"+teamabp[i][2]+"'>";
            if (i == 3) {
                t += "　　　　　";
                str = "(PICK)选择英雄";
            }
        }
        str = "(BAN)禁止选择";
        t += "<br /><br /><b>天灾 BAN/PICK :</b><hr />";
        if (stb < sta)
            st = 1;
        else
            st = 0;
        for (var i = 0 ; i < teambbp.length ; i ++) {
            t += "<img onmouseover=\"description(this,'"+teambbp[i][2]+"','"+str+"','"+Uimage+teambbp[i][1]+".gif','"+teambbp[i][3]+"')\" src='"+Uimage+teambbp[i][1]+".gif' style='width:50px;";
            if (i % 2 == st) t += "margin-left:20px;";
            t += "' alt='"+teambbp[i][2]+"'>";
            if (i == 3) {
                t += "　　　　　";
                str = "(PICK)选择英雄";
            }
        }
        div.innerHTML = t;
    }
    appendmessage();
}
function insertteamdata(dname,d1,d2,r) {
    if (d1 == 0 || d2 == 0) return;
    var ul = $C("ul");
    ul.className = "tditem";
    ul.onmouseover = function(){this.style.backgroundColor="#FFFF9F";};
    ul.onmouseout = function(){this.style.backgroundColor="#ffffff";};
    
    var li = $C("li");
    li.className = "team1";
    li.innerHTML = dname + ":" + d1 + " [" + d1 / r + "]";
    
    var wd = 595 * (d1 / (d1 + d2));
    li.style.width = wd + "px";
    ul.appendChild(li);
    
    li = $C("li");
    li.className = "team2";
    li.innerHTML = dname + ":" + d2 + " [" + d2 / r + "]";
    li.style.width = 589 - wd + "px";
    ul.appendChild(li);
    
    $("teamdata").appendChild(ul);   
}
function appendmessage() {
    var li = $C("li");
    li.innerHTML = "<span class=\"left_bt_h1_l\"></span><span class=\"left_bt_h1_r\"></span><strong>最后装备</strong>";
    li.onclick=function(){clickreplaymenu(this,'enditemdata')};
    
    
    var div = $C("div");
    div.id = "enditemdata";
    div.style.display = "none";
    //menu.parentNode.appendChild(div);
    var i = 0;
    //$("chatdata").appendChild(div);
    for (var id in datalist) {
        
        var p = datalist[id];
        if (!p.enditem) continue;
        var ul = $C("ul");
        ul.className = "datarow1";
        ul.style.height = "38px";
        
        ul.innerHTML = "<li class='c_p'>(最后装备) "+p.heroname+"</li><li class='c_c'>"+p.enditem+"</li>";
        div.appendChild(ul);
        i++;
    }
    if (i > 0) {
        var menu = $("replaymenu");
        menu.appendChild(li);
        menu.parentNode.appendChild(div);
    }   
    
}
function description(obj,title,content,img,time) {
    showdescription.style.display = "block";

    if (content.charAt(0) == '"') content = content.substr(1);
    if (content.charAt(content.length - 1) == '"') content = content.substr(0,content.length - 1);
    if (!img || !time)
        descriptionbody.innerHTML = "<div class='des_title'>" + title + "</div><div class='des_content'>"+ content + "</div>";
    else
        descriptionbody.innerHTML = "<img class='des_image' src='" + img + "' alt='' /><div class='des_title'>" + title + "</div><div class='des_time'>"+time+"</div><div class='des_content'>"+ content + "</div>";
    obj.onmousemove = movedescription;
    obj.onmouseout = function(){showdescription.style.display="none";};
}
function convertdata(str) {
    if (!str) return str;
    var re = /\|n/g;
    str = str.replace(re,"<br />");
    re = /\|c([0f]{2})([0123456789abcdef]{6})/gi;
    str = str.replace(re,"<span style='color:#$2'>");
    re = /\|r/g;
    str = str.replace(re,"</span>");
    return str;
}
function movedescription(event) {
    var p = getMousePointer(event);
    showdescription.style.left = p.x + 5 + "px";
    showdescription.style.top = p.y + 5 + "px";
    
}
function heroimagemove(obj) {
    var player = datalist[obj.parentNode.id.substr(4)];
    description(obj,player.heroname + " - <span style='color:red'>点击查看</span>",player.description);
}
function itemimagemove(obj,time) {
    var id = obj.src;
    var id = id.substr(id.length - 8,4);
    var itemobj = itemdes[id];
    if (itemobj) {
        if (itemobj.money > 0)
            description(obj,itemobj.title + " - <span style='color:#FFC000'>"+ itemobj.money +"</span>",itemobj.des,obj.src,time);
        else
            description(obj,itemobj.title,itemobj.des,obj.src,time);
    }
}
function skillimagemove(obj,level,al,time) {
    var id = obj.src;
    var id = id.substr(id.length - 8,4);
    var itemobj = itemdes[id];
    var ss = itemobj.des.split("\",\"");
    if (ss.length == 1)
       ss = itemobj.des.split(",");
    var sdes = level - 1;
    if (sdes >= ss.length)
        sdes = ss.length - 1;

    if (itemobj)
        description(obj,al+". "+ itemobj.title + " - <span style='color:#FFC000'>Level:"+ level +"</span>",ss[sdes],obj.src,time);

}
