var baseRunes = {
  "Rune of Daggers": { "description":"Adds +%d to damage whenever you do 3 or more damage.", "diff_add":100, "diff_mult":1, "data":1, "position":"weapon" },
  "Rune of Swords": { "description":"Adds +%d to damage whenever you do 3 or more damage.", "diff_add":150, "diff_mult":1, "data":2, "position":"weapon" },
  "Rune of Crowns": { "description":"Adds +%d to your Morale skill.", "diff_add":125, "diff_mult":1, "data":3, "position":"head" },
  "Rune of War": { "description":"Adds +%d to your Battle skill.", "diff_add":125, "diff_mult":1, "data":3, "position":"misc" },
  "Rune of Thought": { "description":"Adds +%d to your Cunning skill.", "diff_add":125, "diff_mult":1, "data":3, "position":"misc" },
  "Rune of Flight": { "description":"Adds +%d to your Air Mastery.", "diff_add":125, "diff_mult":1, "data":3, "position":"body" },
  "Rune of Roads": { "description":"Adds +%d to your Earth Mastery.", "diff_add":125, "diff_mult":1, "data":3, "position":"misc" },
  "Rune of Lava": { "description":"Adds +%d to your Fire Mastery.", "diff_add":125, "diff_mult":1, "data":3, "position":"body" },
  "Rune of Serpents": { "description":"Adds +%d to your Water Mastery.", "diff_add":125, "diff_mult":1, "data":3, "position":"misc" },
  "Rune of Axes": { "description":"Adds +%d to damage whenever you do 6 or more damage.", "diff_add":200, "diff_mult":1, "data":3, "position":"weapon" },
  "Rune of Staves": { "description":"Adds +%d to your Earth, Fire, Water, and Air Mastery skills.", "diff_add":180, "diff_mult":1, "data":1, "position":"weapon" },
  "Rune of Orbs": { "description":"Adds +%d to all of your Mana Reserves whenever your enemy casts a spell.", "diff_add":150, "diff_mult":1, "data":1, "position":"misc" },
  "Rune of Music": { "description":"Adds +%d to all of your Mana Reserves whenever you match 4 or 5 gems.", "diff_add":160, "diff_mult":1, "data":1, "position":"misc" },
  "Rune of Jewels": { "description":"Adds +%d to damage for each full Mana Reserve when doing 3 or more damage.", "diff_add":160, "diff_mult":1, "data":2, "position":"head" },
  "Rune of Shields": { "description":"Does up to +%d damage to your enemy whenever you receive 6 or more damage.", "diff_add":170, "diff_mult":1, "data":1, "position":"misc" }
};
var modifierRunes = {
  "Rune of Crafting": { "description":"Basic Modifier Rune. No additions to the item.", "diff_add":0,  "diff_mult":1,   "base_add":0, "base_mult":1, "power_add":0, "power_mult":1 },
  "Rune of Mastery": { "description":"Adds +2 to Base Rune ability", "diff_add":0,  "diff_mult":1.2, "base_add":2, "base_mult":1, "power_add":0, "power_mult":1 },
  "Rune of Goblins": { "description":"Adds +1 to Base Rune ability and +25% to Power Rune ability.", "diff_add":0,  "diff_mult":1.4, "base_add":1, "base_mult":1, "power_add":0, "power_mult":1.25 },
  "Rune of Dwarves": { "description":"Adds +2 to Base Rune ability and +50% to Power Rune ability.", "diff_add":0,  "diff_mult":1.6, "base_add":2, "base_mult":1, "power_add":0, "power_mult":1.5 },
  "Rune of Mages": { "description":"Adds +100% to Power Rune ability.", "diff_add":0,  "diff_mult":1.8, "base_add":0, "base_mult":1, "power_add":0, "power_mult":2 },
  "Rune of Chaos": { "description":"Adds -1 to Base Rune ability and adds +200% to Power Rune ability.", "diff_add":0,  "diff_mult":2.0, "base_add":-1, "base_mult":1, "power_add":0, "power_mult":3 },
  "Rune of Elves": { "description":"Adds +1 to Base Rune ability and +75% to Power Rune ability.", "diff_add":0,  "diff_mult":2.2, "base_add":1, "base_mult":1, "power_add":0, "power_mult":1.75 },
  "Rune of Giants": { "description":"Adds +3 to Base Rune ability and -50% to Power Rune ability.", "diff_add":0,  "diff_mult":2.4, "base_add":3, "base_mult":1, "power_add":0, "power_mult":0.5 },
  "Rune of Dragons": { "description":"Adds +5 to Base Rune ability.", "diff_add":0,  "diff_mult":2.7, "base_add":5, "base_mult":1, "power_add":0, "power_mult":1 },
  "Rune of Gods": { "description":"Adds +4 to Base Rune ability and +100% to Power Rune ability.", "diff_add":50, "diff_mult":3.0, "base_add":4, "base_mult":1, "power_add":0, "power_mult":2 }  
};
var powerRunes = {
  "Rune of Fire": { "description":"Gives +%d to Red Mana at the beginning of each combat.", "text":"%s of Fire", "diff_add":100, "diff_mult":1, "data":8 },
  "Rune of Earth": { "description":"Gives +%d to Green Mana at the beginning of each combat.", "text":"%s of Stone", "diff_add":100, "diff_mult":1, "data":8 },
  "Rune of Air": { "description":"Gives +%d to Yellow Mana at the beginning of each combat.", "text":"%s of Wind", "diff_add":100, "diff_mult":1, "data":8 },
  "Rune of Water": { "description":"Gives +%d to Blue Mana at the beginning of each combat.", "text":"%s of the Lake", "diff_add":100, "diff_mult":1, "data":8 },
  "Rune of Trolls": { "description":"Gives +%d to maximum Life Points at the beginning of each combat.", "text":"Trollkin&apos;s %s", "diff_add":125, "diff_mult":1, "data":8 },
  "Rune of Shells": { "description":"Adds +%d%% to Water Resistance.", "text":"Miridine&apos;s %s", "diff_add":170, "diff_mult":1, "data":8 },
  "Rune of Caverns": { "description":"Adds +%d%% to Earth Resistance.", "text":"Deep %s", "diff_add":170, "diff_mult":1, "data":8 },
  "Rune of Glass": { "description":"Adds +%d%% to Air Resistance.", "text":"Quartz %s", "diff_add":170, "diff_mult":1, "data":8 },
  "Rune of Ice": { "description":"Adds +%d%% to Fire Resistance.", "text":"Frozen %s", "diff_add":170, "diff_mult":1, "data":8 },
  "Rune of Poison": { "description":"Gives a %d%% chance to cause Poison whenever you do damage.", "text":"%s of Poison", "diff_add":200, "diff_mult":1, "data":20 },
  "Rune of Plague": { "description":"Gives a %d%% chance to cause Disease whenever you do damage.", "text":"%s of Plague", "diff_add":180, "diff_mult":1, "data":20 },
  "Rune of Greed": { "description":"Gives +%d to Gold whenever you win a combat.", "text":"%s of Avarice", "diff_add":120, "diff_mult":1, "data":20 },
  "Rune of Life": { "description":"Adds +%d to Experience whenever you win a combat.", "text":"Mighty %s", "diff_add":150, "diff_mult":1, "data":20 }
};
var itemNames = {
  "Rune of Crafting": { "Rune of Daggers": "Dagger", "Rune of Swords": "Sword", "Rune of Crowns": "Crown", "Rune of War": "Flag", 
    "Rune of Thought": "Scroll", "Rune of Flight": "Cloak", "Rune of Roads": "Shoes", "Rune of Lava": "Belt", "Rune of Serpents": "Ring", 
    "Rune of Axes": "Hand Axe", "Rune of Staves": "Stick", "Rune of Orbs": "Orb", "Rune of Music": "Zither", "Rune of Jewels": "Necklace", 
    "Rune of Shields": "Shield" },
  "Rune of Mastery": { "Rune of Daggers": "Poniard", "Rune of Swords": "Broadsword", "Rune of Crowns": "Diadem", "Rune of War": "Banner",
    "Rune of Thought": "Tome", "Rune of Flight": "Cape", "Rune of Roads": "Greaves", "Rune of Lava": "Strap", "Rune of Serpents": "Bracelet",
    "Rune of Axes": "Hatchet", "Rune of Staves": "Wand", "Rune of Orbs": "Sphere", "Rune of Music": "Harpanola", "Rune of Jewels": "Pendant",
    "Rune of Shields": "Guard" },
  "Rune of Goblins": { "Rune of Daggers": "Shiv", "Rune of Swords": "Shortsword", "Rune of Crowns": "Cap", "Rune of War": "Badge",
    "Rune of Thought": "Codex", "Rune of Flight": "Drape", "Rune of Roads": "Sandals", "Rune of Lava": "Cinch", "Rune of Serpents": "Loop",
    "Rune of Axes": "Chopper", "Rune of Staves": "Pole", "Rune of Orbs": "Ball", "Rune of Music": "Horn", "Rune of Jewels": "Bauble",
    "Rune of Shields": "Buckler" },
  "Rune of Dwarves": { "Rune of Daggers": "Kris", "Rune of Swords": "Runesword", "Rune of Crowns": "Helm", "Rune of War": "Symbol",
    "Rune of Thought": "Manual", "Rune of Flight": "Mantle", "Rune of Roads": "Boots", "Rune of Lava": "Waistguard", "Rune of Serpents": "Bracer",
    "Rune of Axes": "Battle Axe", "Rune of Staves": "Rod", "Rune of Orbs": "Crystal", "Rune of Music": "Bugle", "Rune of Jewels": "Medallion",
    "Rune of Shields": "Protector" },
  "Rune of Mages": { "Rune of Daggers": "Poker", "Rune of Swords": "Blade", "Rune of Crowns": "Tiara", "Rune of War": "Field",
    "Rune of Thought": "Lexicon", "Rune of Flight": "Shroud", "Rune of Roads": "Slippers", "Rune of Lava": "Cord", "Rune of Serpents": "Seal",
    "Rune of Axes": "Tomahawk", "Rune of Staves": "Staff", "Rune of Orbs": "Phylactery", "Rune of Music": "Lyre", "Rune of Jewels": "Amulet",
    "Rune of Shields": "Targe" },
  "Rune of Chaos": { "Rune of Daggers": "Razor", "Rune of Swords": "Falchion", "Rune of Crowns": "Horns", "Rune of War": "Mark",
    "Rune of Thought": "Grimoire", "Rune of Flight": "Veil", "Rune of Roads": "Sabatons", "Rune of Lava": "Grasp", "Rune of Serpents": "Circle",
    "Rune of Axes": "Reaver", "Rune of Staves": "Splinter", "Rune of Orbs": "Scarab", "Rune of Music": "Didgeridoo", "Rune of Jewels": "Talisman",
    "Rune of Shields": "Disk" },
  "Rune of Elves": { "Rune of Daggers": "Thorn", "Rune of Swords": "Rapier", "Rune of Crowns": "Circlet", "Rune of War": "Standard",
    "Rune of Thought": "Book", "Rune of Flight": "Shawl", "Rune of Roads": "Striders", "Rune of Lava": "Sash", "Rune of Serpents": "Signet",
    "Rune of Axes": "Francisca", "Rune of Staves": "Sceptre", "Rune of Orbs": "Stone", "Rune of Music": "Psaltery", "Rune of Jewels": "Charm",
    "Rune of Shields": "Defender" },
  "Rune of Giants": { "Rune of Daggers": "Spike", "Rune of Swords": "Brand", "Rune of Crowns": "Greathelm", "Rune of War": "Totem",
    "Rune of Thought": "Tablet", "Rune of Flight": "Cover", "Rune of Roads": "Footwraps", "Rune of Lava": "Girdle", "Rune of Serpents": "Hoop",
    "Rune of Axes": "Cleaver", "Rune of Staves": "Quarterstaff", "Rune of Orbs": "Globe", "Rune of Music": "Alphorn", "Rune of Jewels": "Chain",
    "Rune of Shields": "Bulwark" },
  "Rune of Dragons": { "Rune of Daggers": "Dragonfang", "Rune of Swords": "Dragonslayer", "Rune of Crowns": "Crest", "Rune of War": "Wing",
    "Rune of Thought": "Hide", "Rune of Flight": "Scale", "Rune of Roads": "Talons", "Rune of Lava": "Sinew", "Rune of Serpents": "Band",
    "Rune of Axes": "Dragontooth", "Rune of Staves": "Tail", "Rune of Orbs": "Eye", "Rune of Music": "Dragonhorn", "Rune of Jewels": "Collar",
    "Rune of Shields": "Reflector" },
  "Rune of Gods": { "Rune of Daggers": "Godblade", "Rune of Swords": "Avenger", "Rune of Crowns": "Halo", "Rune of War": "Aura",
    "Rune of Thought": "Writ", "Rune of Flight": "Vestments", "Rune of Roads": "Geta", "Rune of Lava": "Clasp", "Rune of Serpents": "Link",
    "Rune of Axes": "Edge", "Rune of Staves": "Pillar", "Rune of Orbs": "Shard", "Rune of Music": "Harp", "Rune of Jewels": "Relic",
    "Rune of Shields": "Aegis"}  
};
var baseRune = null;
var modifierRune = null;
var powerRune = null;
function setBaseRune(rune)
{
  baseRune = rune;
  showItems();
}
function setModifierRune(rune)
{
  modifierRune = rune;
  showItems();
}
function setPowerRune(rune)
{
  powerRune = rune;
  showItems();
}
function getItemDetails(baseRune, modifierRune, powerRune)
{
  var i;
  var name = powerRunes[powerRune].text.replace(/%s/, itemNames[modifierRune][baseRune]);
  var desc = baseRunes[baseRune].description.replace(/%d/, ((baseRunes[baseRune].data + modifierRunes[modifierRune].base_add) * modifierRunes[modifierRune].base_mult).toString());
  desc += "<br />" + powerRunes[powerRune].description.replace(/%d%?/, ((powerRunes[powerRune].data + modifierRunes[modifierRune].power_add) * modifierRunes[modifierRune].power_mult).toString());
  var type = baseRunes[baseRune].position;
  var diff = (baseRunes[baseRune].diff_add + powerRunes[powerRune].diff_add + modifierRunes[modifierRune].diff_add) * modifierRunes[modifierRune].diff_mult;
  diff = Math.round(diff);
  var diff2 = baseRunes[baseRune].diff_add * modifierRunes[modifierRune].diff_mult + modifierRunes[modifierRune].diff_add + powerRunes[powerRune].diff_add;
  diff2 = Math.round(diff2);
  return { "name":name, "description":desc, "type":type, "difficulty":baseRunes[baseRune].diff_add + " " + modifierRunes[modifierRune].diff_mult + " " + powerRunes[powerRune].diff_add + " " + diff.toString() + " " + diff2.toString() };
}
function showItems()
{
  var i, j, name, desc, type, item;
  var tab = document.getElementById("runes").tBodies[0];
  while (tab.rows.length > 0)
  {
    tab.deleteRow(0);
  }
  if (baseRune || modifierRune || powerRune)
  {
    document.getElementById("chosen_runes").innerHTML = (baseRune || "?") + " + " + (modifierRune || "?") + " + " + (powerRune || "?");
    document.getElementById("runes").style.display = "";
  }
  else
  {
    document.getElementById("chosen_runes").innerHTML = "";
    document.getElementById("runes").style.display = "none";
  }
  if (baseRune)
  {
    if (modifierRune && powerRune)
    {
      item = getItemDetails(baseRune, modifierRune, powerRune);
      createRow(item.name, item.description, item.type, baseRune, modifierRune, powerRune, item.difficulty);
    }
    else if (modifierRune && !powerRune)
    {
      for (i in powerRunes)
      {
        item = getItemDetails(baseRune, modifierRune, i);
        createRow(item.name, item.description, item.type, baseRune, modifierRune, i, item.difficulty);
      }
    }
    else if (powerRune && !modifierRune)
    {
      for (i in modifierRunes)
      {
        item = getItemDetails(baseRune, i, powerRune);
        createRow(item.name, item.description, item.type, baseRune, i, powerRune, item.difficulty);
      }
    }
    else
    {
      for (i in modifierRunes)
      {
        for (j in powerRunes)
        {
          item = getItemDetails(baseRune, i, j);
          createRow(item.name, item.description, item.type, baseRune, i, j, item.difficulty);
        }
      }
    }
  }
  else if (modifierRune)
  {
    if (powerRune)
    {
      for (i in baseRunes)
      {
        item = getItemDetails(i, modifierRune, powerRune);
        createRow(item.name, item.description, item.type, i, modifierRune, powerRune, item.difficulty);
      }
    }
    else
    {
      for (i in baseRunes)
      {
        for (j in powerRunes)
        {
          item = getItemDetails(i, modifierRune, j);
          createRow(item.name, item.description, item.type, i, modifierRune, j, item.difficulty);
        }
      }
    }    
  }
  else if (powerRune)
  {
    for (i in baseRunes)
    {
      for (j in modifierRunes)
      {
        item = getItemDetails(i, j, powerRune);
        createRow(item.name, item.description, item.type, i, j, powerRune, item.difficulty);
      }
    }
  }
  var l = document.getElementById("runes").tBodies[0].rows.length;
  document.getElementById("row_count").innerHTML = l + " item" + (l == 1 ? "" : "s");
}
function createRow(name, description, type, base, modifier, power, difficulty)
{
  var tab = document.getElementById("runes");
  var row = document.createElement("tr");
  var tdName = document.createElement("td");
  var tdType = document.createElement("td");
  var tdBase = document.createElement("td");
  var tdModi = document.createElement("td");
  var tdPowr = document.createElement("td");
  var tdDiff = document.createElement("td");
  tdName.innerHTML = "<h2>" + name + "</h2><p>" + description + "</p>";
  tdType.innerHTML = type;
  tdBase.innerHTML = base;
  tdModi.innerHTML = modifier;
  tdPowr.innerHTML = power;
  tdDiff.innerHTML = difficulty;
  row.appendChild(tdName);
  row.appendChild(tdType);
  row.appendChild(tdBase);
  row.appendChild(tdModi);
  row.appendChild(tdPowr);
  //row.appendChild(tdDiff);
  tab.tBodies[0].appendChild(row);
}
function showTables(baseDisplay, modifierDisplay, powerDisplay)
{
  document.getElementById("base_runes_link").className = (baseDisplay == "" ? "selected" : "") ;
  document.getElementById("modifier_runes_link").className = (modifierDisplay == "" ? "selected" : "") ;
  document.getElementById("power_runes_link").className = (powerDisplay == "" ? "selected" : "") ;
  document.getElementById("base_runes").style.display = baseDisplay;
  document.getElementById("modifier_runes").style.display = modifierDisplay;
  document.getElementById("power_runes").style.display = powerDisplay;
}
