// JavaScript Document
function BuildBestOf() {

    var categories =    [
                            { name: "Weapons",  attack: "wattack", defense: "wdefense", list: weapon },
                            { name: "Armor",    attack: "aattack", defense: "adefense", list: armor },
                            { name: "Vehicles", attack: "vattack", defense: "vdefense", list: vehicle },
                            { name: "Animals",  attack: "nattack", defense: "ndefense", list: animal }
                        ];

    var bestof =        [
                            { type : "",  name: "Overall" },
                            { type : "#", name: "Buyable" },
                            { type : "D", name: "Drop" },
                            { type : "Y", name: "Mastery Item" },
                            { type : "G", name: "Godfather Points" }
                        ];

    var i,x,cats,loot; // iterators

    function copyItem(item) {
        var obj       = {};
        obj.name      = item.name;
        obj.image     = item.image;
        obj.attack    = item.attack;
        obj.defense   = item.defense;
        obj.where     = item.where;
        obj.type      = item.type;
        obj.giftid    = item.lootid;
        return(obj);
    }
    
    for(i=0;i<bestof.length;i++) {
        for(cats=0;cats<categories.length;cats++) {
            bestof[i][categories[cats].attack] = { "attack":0 }
            bestof[i][categories[cats].defense] = { "defense":0 }
        }
    }

    // locate the best loot in all categories
    for(cats=0;cats<categories.length;cats++) {

        for(i=0;i<categories[cats].list.length;i++) {
            
            for(x=0;x<bestof.length;x++) {
                
                loot = categories[cats].list[i];
                    
                if(loot.city.indexOf(bestof[x].type) >= 0) {
                    
                    if(loot.attack > bestof[x][categories[cats].attack].attack)
                        bestof[x][categories[cats].attack] = copyItem(loot);
                            
                    if(loot.defense > bestof[x][categories[cats].defense].defense)
                        bestof[x][categories[cats].defense] = copyItem(loot);
                            
                }
            }
        }
    }

    function makeline(name,item) {
        var output = "";
        var directory = (item.type=="W")?"weapons":(item.type=="A")?"armor":(item.type=="V")?"vehicles":(item.type=="N")?"animals":"";
        output += "<tr><td colspan='3' valign='top'>Best " + name + "</td></tr>";
        output += "<tr><td style='border-bottom: 1px solid gray'><img src='http://images.blofelds.com/" + directory + "/";
        output += item.image + "' style='width: 75px'/></td>";
        output += "<td style='border-bottom: 1px solid gray'><span class='mylabel'>" + item.name + "</span><br/>";
        output += "<img src='attack.png' style='padding-right: 10px'/>" + item.attack + "&nbsp;";
        output += "<img src='defense.png' style='padding-right: 10px'/>" + item.defense + "<p/>";
        output += "<span class='wherefrom'>" + item.where + "</span></td>";
        output += "<td>";

        if(item.giftif > 0) {
            output += "<a target=\"new\" href=\"" + getGiftString(item.type,item.giftid) + "\"><img border='0' style=\"padding-top: 3px\" src='images/addtowishlist.jpg' width='29' height='27'></a>";
        }

        output += "</td>";

        output += "</tr>";
        return(output);
    }
    
    var obj;
   	var output = "<table border='0' cellpadding='5' cellspacing='0' width='100%' style='margin-bottom: 30px'>";

    for(cats=0;cats < categories.length; cats++) {
        output += "<tr><td colspan='2' valign='top' style='font-size: 15pt; background-color: DimGray'>Best " + categories[cats].name + "</td></tr>";
    		
  	     for(i=0;i<bestof.length;i++) {
            if(bestof[i][categories[cats].attack].attack > 0)
                output += makeline(bestof[i].name + " attack",bestof[i][categories[cats].attack]);
            if(bestof[i][categories[cats].defense].defense > 0)
                output += makeline(bestof[i].name + " defense",bestof[i][categories[cats].defense]);
        }

    }
    
	output += "</table>";
	// get the table and replace it's contents

	obj = document.getElementById("Bestoftab");
	
	if(obj)
		obj.innerHTML = output;
	else
		alert("couldn't find Bestoftab object"); 
		
}
