Calculators/Code

// /*General*/ $('body:first').append(''+ '#calculators_container{width:660px;text-align:center;}'+ '#calculators_container>div {display:inline-block;border:1px solid #bfcfcf; background-color:#f9fcff; padding:8px 10px;}'+ '.text_align_left {text-align:left;}'+ '.text_align_center {text-align:center;}'+ '.text_align_right {text-align:right;}'+ '#calculator_statsrs {display:block;width:296px;position:relative;}'+ '#calculator_statsrs div {position:absolute;color:#bfbfbf;font:bold 14px Tahoma;}'+ '#calculator_statsr1 {top:64px;right:30px;}'+ '#calculator_statsr2 {top:90px;right:30px;}'+ '#calculator_statsr3 {top:30px;left:165px;}'+ '#calculator_statsr4 {top:30px;left:23px;}'+ '.calculator_lootrc {vertical-align:top;}'+ '#calculator_looti1 {width:600px;}'+ '#calculator_armor {width:100%;}'+ '#calculator_armor td {vertical-align:top;}'+ '#calculator_armor_damages {width:100%;}'+ '#calculator_armor_damages td, #calculator_armor_damages th {width: 10%;vertical-align:middle;}'+ '#calculator_armor_body_main {background:url(\'http://images4.wikia.nocookie.net/tibia/en/images/3/39/Tibia_Client_Background_Light.gif\');margin:0px 10px;position:relative;display:inline-block;width:112px;height:149px;border:1px black solid;}'+ '#calculator_armor_body_main div {position:absolute; width:32px; height:32px;background:url(\'http://images2.wikia.nocookie.net/tibia/en/images/b/b3/Set_Background.gif\');padding: 1px;}'+ '#calculator_armor_body_main img {border:none;cursor:pointer;}'+ '#calculator_armor_tt_items {display:none;position:absolute;width:auto;height:auto;background:#DDDDDD;border:1px black solid;padding:4px;}'+ ' '); calculator_numcs = function(n) { n = ''+n; while ((/\d{4}/).test(n)) { n = n.replace(/(\d{3},|\d{3}$)/, ',$1'); } return n; }; calculator_btn_m = function(o){$(o).prev.val(parseInt($(o).prev.val, 10)-1);$(o).prev.keyup;}; calculator_btn_p = function(o){$(o).prev.prev.val(parseInt($(o).prev.prev.val, 10)+1);$(o).prev.prev.keyup;};

calculator_array_sort = function(inputArr, numeric, by_key, reverse, sub_key) { var tmp_arr={}, valArr=[], keyArr=[], keys=[], sorter, i, k, populateArr = []; if (typeof numeric == 'undefined') { numeric = false;} if (typeof by_key == 'undefined') { by_key = false;} if (typeof reverse == 'undefined') { reverse = false;} if (typeof sub_key == 'undefined') { sub_key = '';} var is_numeric = function(v) { v = parseFloat(v); return (typeof v === 'number' && !isNaN(v)); }; if (numeric) { sorter = function(a, b) { return (reverse ? b - a : a - b); }; } else { sorter = function(a, b) { var x = a, y = b, tmp; if (!is_numeric(a) && !is_numeric(b)) { tmp = function(a,b){a=a.search(/[a-z]/);b=b.search(/[a-z]/);if((a!==0 && b!==0)||a==b){return 0;}if(a===0){return -1;}if(b===0){return 1;}}(x, y); if (tmp !== 0) { return tmp*(reverse?-1:1); } if (a == b) { return 0; } if (a > b) { return (reverse ? -1 : 1); } return (reverse ? 1 : -1); }     else { a = parseFloat(a) || 0; b = parseFloat(b) || 0; return (reverse ? b - a : a - b); } }; }  if (by_key) { for (k in inputArr) { if (inputArr.hasOwnProperty(k)) { keys.push(k); } }/*Make a list of key names*/ keys.sort(sorter); for (i = 0; i < keys.length; i++) { k = keys[i]; tmp_arr[k] = inputArr[k]; }/*Rebuild array with sorted key names*/ for (i in tmp_arr) { if (tmp_arr.hasOwnProperty(i)) { populateArr[i] = tmp_arr[i]; } } } else { var bubbleSort = function(keyArr, inputArr, sub_key) { var i, j, tempValue, tempKeyVal, ret; for (i = inputArr.length-2; i >= 0; i--) { for (j = 0; j <= i; j++) { ret = (sub_key === '') ? sorter(inputArr[j+1], inputArr[j]) : sorter((typeof inputArr[j+1].resist[sub_key]=='undefined'?inputArr[j+1][sub_key]:inputArr[j+1].resist[sub_key]), (typeof inputArr[j].resist[sub_key]=='undefined'?inputArr[j][sub_key]:inputArr[j].resist[sub_key])); if (ret < 0) { tempValue = inputArr[j]; inputArr[j] = inputArr[j+1]; inputArr[j+1] = tempValue; tempKeyVal = keyArr[j]; keyArr[j] = keyArr[j+1]; keyArr[j+1] = tempKeyVal; } } }   };    for (k in inputArr) { if (inputArr.hasOwnProperty(k)) { valArr.push(inputArr[k]); keyArr.push(k); } }/*Get key and value*/ try { bubbleSort(keyArr, valArr, sub_key); } catch (e) { return false; }/*Sort our new temporary arrays*/ for (i = 0; i < valArr.length; i++) { populateArr[keyArr[i]] = valArr[i]; }/*Repopulate the old array*/ } return populateArr; };

/*Exp*/ $('#calculator_exp') .append('Level: ') .append(' ') .append($('').click(function{calculator_btn_m(this);})).append(' ') .append($('').click(function{calculator_btn_p(this);})) .append('

 '); $('#calculator_expi1').keyup(function { if ($(this).val === '') { $(this).val(1).select; } var exp, lvl = Math.abs(parseInt($(this).val, 10) || 1); $(this).val(lvl); exp = ''+(50*Math.pow(lvl-1,3)-150*Math.pow(lvl-1,2)+400*(lvl-1))/3; while ((/\d{4}/).test(exp)) { exp = exp.replace(/(\d{3},|\d{3}$)/, ',$1'); } $('#calculator_expr1').html('Experience for level '+lvl+': '+exp+''); }); $('#calculator_expi1').keyup;

/*Stats*/ var x, tmp = ''; calculator_stats_voc = {'Druid':[5, 30, 10], 'Knight':[15, 5, 25], 'Paladin':[10, 15, 20], 'Sorcerer':[5, 30, 10], 'Rookstayer':[5, 5, 10]};//[hp, mana, cap]

calculator_stats_update = function { var x, lvl, rooklvl; for (x=2;x<=4;x++) { if ($('#calculator_statsi'+x).val === '') { $('#calculator_statsi'+x).val(8).select; } $('#calculator_statsi'+x).val(Math.abs(parseInt($('#calculator_statsi'+x).val, 10) || 8)); } lvl = parseInt($('#calculator_statsi2').val, 10); rooklvl = parseInt($('#calculator_statsi3').val, 10); x = calculator_stats_voc[$('#calculator_statsi1').val][0]; $('#calculator_statsr1').text(145+(rooklvl*5)+((lvl-rooklvl)*x)); x = calculator_stats_voc[$('#calculator_statsi1').val][1]; $('#calculator_statsr2').text(-5+(rooklvl*5)+((lvl-rooklvl)*x)); x = calculator_stats_voc[$('#calculator_statsi1').val][2]; $('#calculator_statsr3').text(390+(rooklvl*10)+((lvl-rooklvl)*x)); $('#calculator_statsr4').text(220+(2*(lvl-1))); };

for (x in calculator_stats_voc) { if (calculator_stats_voc.hasOwnProperty(x)) { tmp += ''+x+' '; }} $('#calculator_stats').html(' ');

$('#calculator_statsi2, #calculator_statsi3').keyup(calculator_stats_update) .next.click(function{calculator_btn_m(this);calculator_stats_update;}) .next.click(function{calculator_btn_p(this);calculator_stats_update;}); $('#calculator_statsi1').change(calculator_stats_update); calculator_stats_update;

/*Loot*/ $('#calculator_loot').append( ''+ ' '+ ' '+ ''+ ' '+ ''+ ' '+ ''+ ''+ ' '+ ' '+ ' '+ '  '+ ' ' ); calculator_loot_process = function { var ucwords = function(str) { str = (str+'').replace(/^(.)|\s(.)|-(.)/g, function ($1) { return $1.toUpperCase; }); return str.replace(' Of The ', ' of the ').replace(' Of A ', ' of a ').replace(' Of An ', ' of an ').replace(' Of ', ' of ').replace(' The ', ' the '); }, get_wiki_data = function { var ret = {}, tmp, x, p, h = $('#calculator_loot_dpl').html.replace(/ /gi, ).replace(/<\/p>/gi, ); while (h.search(/\s\s/) !== -1) { h = h.replace(/\s\s/g, ' '); } h = h.replace(/\s/g, ' '); p = h.split('|'); for (x in p) { if (p.hasOwnProperty(x)) { p[x] = $.trim(p[x]); } } while (p[0] === '') { p = p.slice(1); } //item_name={weight,sellto,npcvalue} for (x = 0; x < p.length; x = x + 4) { tmp = p[x+2].split(',')[0].split('(')[0].replace(/>gp<\/a>/i, ',');//remove unused data     tmp = tmp.substr(0, (tmp.lastIndexOf('') || (tmp.length - 4))+4);//leave from the beginning to last link      tmp = tmp.replace(/player[s]?\.?/i, );//remove players      tmp = $.trim(tmp.replace(/<\/?b>/i, ));//remove boldic and trim      ret[ucwords(p[x])] = {'weight':parseFloat(p[x+1]), 'sellto':tmp, 'npcvalue':(parseInt(p[x+3].replace(/,/g, ''), 10) || 0)};    }    return ret;  },  loot_to_singular = function(t) {    var calculator_loot_p_words = {'Cookies':'Cookie', '*Pieces of *':'Piece of ', '*Bundles of *':'Bundle of ', '*Strands of *':'Strand of ',    '*Bunches of *':'Bunch of ', '* Toes':' Toe', '*Flasks of *':'Flask of ', '*Teeth':'Tooth',    'Globs of *':'Glob of ', 'Essences of *':'Essence of ', 'Books of *':'Book of ', 'Piles of *':'Pile of ',    'Lumps of *':'Lump of ', '*Ears of *':'Ear of ', '*s of Corruption':' of Corruption'};/*Exceptions*/ var calculator_loot_p_ends = {'she':'sh', 'ie':'y', 've':'fe', 'oe':'o', 'ze':'z', 'che':'ch', 'sse':'ss'};/*Exceptions of endings (after removing the last 's')*/ var x; for (x in calculator_loot_p_words) { if (calculator_loot_p_words.hasOwnProperty(x)) { if ((new RegExp('^'+x.replace(/\*/g,'.*?')+'$')).test(t)) { return t.replace(x.replace(/\*/g, ''), calculator_loot_p_words[x]); } } }   var lastletter = t.slice(t.length - 1); if (lastletter == "s") { t = t.slice(0, t.length - 1);/*remove the s*/ lastletter = t.slice(t.length - 3);/*check last 3 letters*/ if (typeof calculator_loot_p_ends[lastletter] != 'undefined') { t = t.slice(0, t.length - 3) + calculator_loot_p_ends[lastletter]; } lastletter = t.slice(t.length - 2);/*check last 2 letters*/ if (typeof calculator_loot_p_ends[lastletter] != 'undefined') { t = t.slice(0, t.length - 2) + calculator_loot_p_ends[lastletter]; } }   return t;  }; var x, line, amount, tmp, data, data_items = {}, d = get_wiki_data, skiplist = []; var totalsee = 0; data = $.trim($('#calculator_looti1').val).split('\n'); for (x in data) { if (data.hasOwnProperty(x)) { line = data[x].indexOf('You see '); if (line === -1) { continue; } line = data[x].substr(line + 8); amount = parseInt(line.match(/^a[n]? /i) ? 1 : (line.match(/^\d+ /i) ? line.match(/^\d+ /i) : 0), 10); if (line.substr(line.length - 1) == '.') { line = line.substr(0, line.length - 1); } if ((tmp = line.indexOf('(')) !== -1) { line = line.substr(0, tmp - 1); }   if ((tmp = line.indexOf('that has energy for ')) !== -1) { line = line.substr(0, tmp - 1); }    else if ((tmp = line.indexOf('that is brand-new')) !== -1) { line = line.substr(0, tmp - 1); }    line = ucwords(line);//item name    if (amount === 0) {      try {        tmp = data[parseInt(x, 10)+1].match(/weigh[s]? (\d{1,4}\.\d{1,2}) oz/);       if (tmp === null) {          tmp = data[parseInt(x, 10)+2].match(/weigh[s]? (\d{1,4}\.\d{1,2}) oz/);       }      }      catch(e) { tmp = null; }      tmp = (tmp === null || tmp[1] == 'undefined' ? 0 : parseFloat(tmp[1]));     if (typeof d[line] == 'undefined' || d[line].npcvalue === 0) { skiplist.push(line); continue; }//not in list or 0gp      amount = parseInt(tmp / d[line].weight, 10) || 0;      if (amount === 0) { skiplist.push(line); alert(line + ' will be omitted, couldn\'t find the weight.'); }    }    else {      line = line.substr(line.indexOf(' ') + 1);      if (amount > 1) { line = loot_to_singular(line); }    }    if (typeof d[line] == 'undefined' || d[line].npcvalue === 0) { skiplist.push(line); continue; }//not in list or 0 gp    //Item is included    totalsee++;    if (typeof data_items[line] == 'undefined') {      tmp = (!d[line].sellto.match(/You can return the/gi) && !d[line].sellto.match(/player/gi)) ? d[line].sellto : ;      data_items[line] = [0, d[line].npcvalue, tmp];    }//[name][amount, price, npc/]    data_items[line][0] += amount;  } }

calculator_loot_sum = function { var tmp = 0, npcs = {}, npcso = , x, skipped = ; $('.calculator_loot_includecb').each(function { if ($(this).attr('checked')) {     tmp += (Number($(this).parent.nextAll(':eq(3)').html.replace(',','')) || 0);      npcs[data_items[$(this).parent.nextAll(':eq(1)').html][2]] = 0;    }}); npcs = calculator_array_sort(npcs, false, true); for (x in npcs) { if (npcs.hasOwnProperty(x)) { npcso += ' '+x+'  '; }} $('#calculator_lootr2').html(npcso); $('#calculator_lootr4').html(calculator_numcs(tmp)+' gp'); $('#calculator_lootr1').html(totalsee); for (x in skiplist) { if (skiplist.hasOwnProperty(x)) { skipped += ' '+skiplist[x]+'  '; } } if (skipped) { $('#calculator_lootr5').html(skipped); } $('.calculator_lootrc a').click(function{window.open(this.href); return false;}); };

data_items = calculator_array_sort(data_items, false, true); $('#calculator_lootr3').empty.append(   ' '+    ' '  ); for (x in data_items) { if (data_items.hasOwnProperty(x)) { $('#calculator_loot_tableb').append($('<tr title="NPC that buys: '+(data_items[x][2].replace(/(.*?)<\/a>/gi, '$1').replace(/</g, ''))+'"> ').append(' <input type="checkbox" class="calculator_loot_includecb" checked="checked" /> '+data_items[x][0]+'  '+x+'  '+calculator_numcs(data_items[x][1])+'  '+calculator_numcs(data_items[x][0] * data_items[x][1])+' ')); } } calculator_loot_sum; $('.calculator_loot_includecb').click(function { calculator_loot_sum; }); $('#calculator_loot_table :checkbox:first').click(function{ $('#calculator_loot_table :checkbox').attr('checked', $(this).attr('checked')); calculator_loot_sum; }); $('#calculator_loot_tg1').hide; $('#calculator_loot_tg2').show; try{ts_makeSortable($('#calculator_loot_table')[0]);}catch(er){} };

$('#calculator_lootb1').click(function { calculator_loot_process; }); $('#calculator_lootb2').click(function{ $('#calculator_looti1').val(''); }); $('#calculator_lootb3').click(function{ $('#calculator_loot_tg2').hide; $('#calculator_loot_tg1').show;  $('#calculator_lootb2').click; });

/*Armor*/ $('#calculator_armor').append( ' '+ ' <td style="width:115px;">'+ ' Damage type: <select id="calculator_armor_damage_type" size="1"><option value="physical" selected="selected">Physical  Fire  Earth  Energy  Ice  Holy  Death  '+ '

'+ ' Show items for: '+ ' <select id="calculator_armor_voc" size="1"> All <option value="1" selected="selected">Druid  Knight  Paladin  Sorcerer  '+ '

'+ ' <input type="checkbox" value="1" id="calculator_armor_np" /> Show non protective items'+

' '+ '  Required Level: <span id="calculator_armor_req_level">None '+ ' Needed Cap: <span id="calculator_armor_set_oz">0.00 oz '+ ' Total Armor: <span id="calculator_armor_set_arm">0 '+ ' <span id="calculator_armor_set_prot">Physical protection: 0% '+ '  '+ ' ' ); calculator_armor_voc = 1; calculator_armor_current_part = 0; calculator_armor_parts_names = ['amulet', 'ring', 'helmet', 'armor', 'legs', 'boots']; calculator_armor_names_parts = {'amulet':0, 'ring':1, 'helmet':2, 'armor':3, 'legs':4, 'boots':5}; calculator_armor_get_link_for_name = function(item, part) {  var r = $('#calculator_armor_list_'+calculator_armor_parts_names[part]+'_img img').filter(function { var a = $(this).attr('src').split('/'); return (unescape(a[a.length -1]) == unescape(item.replace(/ /g, '_')+'.gif') ? true : false); }).first.attr('src'); return r; }; var get_items_data = function(name) {  var part = calculator_armor_names_parts[name];  var ret = {}, x, p, h = $('#calculator_armor_list_'+name).html.replace(/ /gi, ).replace(/<\/p>/gi, );  var get_vocation_number = function(t) {    t = t.toLowerCase;    return (t.match(/druid/i)?1:0)+(t.match(/knight/i)?2:0)+(t.match(/paladin/i)?4:0)+(t.match(/sorcerer/i)?8:0);  };  while (h.search(/\s\s/) !== -1) { h = h.replace(/\s\s/g, ' '); }  h = h.replace(/\s/g, ' ');  p = h.split('|');  for (x in p) { if (p.hasOwnProperty(x)) { p[x] = $.trim(p[x]); } }  while (p[0] === ) { p = p.slice(1); }  for (x = 0; x < p.length; x = x + 7) {    ret[p[x].toLowerCase] = {      name:p[x], arm:(parseInt(p[x+1], 10) || 0), oz:parseFloat(p[x+2]) || 0, lvl:(parseInt(p[x+5], 10) || 0),      att:$.trim(p[x+3].replace(/none\.?/gi, ).replace(/(.*?)<\/a>/gi, '$1').replace(/</g, '')), voc:get_vocation_number(p[x+6]), ur:calculator_armor_get_link_for_name(p[x], part), resist:{ physical:(parseInt(p[x+4].split(/physical/i)[1], 10) || 0), fire:(parseInt(p[x+4].split(/fire/i)[1], 10) || 0), earth:(parseInt(p[x+4].split(/earth/i)[1], 10) || 0), energy:(parseInt(p[x+4].split(/energy/i)[1], 10) || 0), ice:(parseInt(p[x+4].split(/ice/i)[1], 10) || 0), holy:(parseInt(p[x+4].split(/holy/i)[1], 10) || 0), death:(parseInt(p[x+4].split(/death/i)[1], 10) || 0) }   };  }  x = 'No'+name.slice(0, 1).toUpperCase+name.slice(1); ret[x.toLowerCase] = {name:x, arm:0, oz:0, att:'', lvl:0, voc:0, resist:{physical:0, fire:0, earth:0, energy:0, ice:0, holy:0, death:0}}; ret[x.toLowerCase].ur = calculator_armor_get_link_for_name(x, part); $('#calculator_armor_body_'+calculator_armor_parts_names[part]).attr('src', ret[x.toLowerCase].ur); return ret; };

calculator_array_sort = function(inputArr, numeric, by_key, reverse, sub_key) { var tmp_arr={}, valArr=[], keyArr=[], keys=[], sorter, i, k, populateArr = []; if (typeof numeric == 'undefined') { numeric = false;} if (typeof by_key == 'undefined') { by_key = false;} if (typeof reverse == 'undefined') { reverse = false;} if (typeof sub_key == 'undefined') { sub_key = '';} var is_numeric = function(v) { v = parseFloat(v); return (typeof v === 'number' && !isNaN(v)); }; if (numeric) { sorter = function(a, b) { return (reverse ? b - a : a - b); }; } else { sorter = function(a, b) { var x = a, y = b, tmp; if (!is_numeric(a) && !is_numeric(b)) { tmp = function(a,b){a=a.search(/[a-z]/);b=b.search(/[a-z]/);if((a!==0 && b!==0)||a==b){return 0;}if(a===0){return -1;}if(b===0){return 1;}}(x, y); if (tmp !== 0) { return tmp*(reverse?-1:1); } if (a == b) { return 0; } if (a > b) { return (reverse ? -1 : 1); } return (reverse ? 1 : -1); }     else { a = parseFloat(a) || 0; b = parseFloat(b) || 0; return (reverse ? b - a : a - b); } }; }  if (by_key) { for (k in inputArr) { if (inputArr.hasOwnProperty(k)) { keys.push(k); } }/*Make a list of key names*/ keys.sort(sorter); for (i = 0; i < keys.length; i++) { k = keys[i]; tmp_arr[k] = inputArr[k]; }/*Rebuild array with sorted key names*/ for (i in tmp_arr) { if (tmp_arr.hasOwnProperty(i)) { populateArr[i] = tmp_arr[i]; } } } else { var bubbleSort = function(keyArr, inputArr, sub_key) { var i, j, tempValue, tempKeyVal, ret; for (i = inputArr.length-2; i >= 0; i--) { for (j = 0; j <= i; j++) { ret = (sub_key === '') ? sorter(inputArr[j+1], inputArr[j]) : sorter((typeof inputArr[j+1].resist[sub_key]=='undefined'?inputArr[j+1][sub_key]:inputArr[j+1].resist[sub_key]), (typeof inputArr[j].resist[sub_key]=='undefined'?inputArr[j][sub_key]:inputArr[j].resist[sub_key])); if (ret < 0) { tempValue = inputArr[j]; inputArr[j] = inputArr[j+1]; inputArr[j+1] = tempValue; tempKeyVal = keyArr[j]; keyArr[j] = keyArr[j+1]; keyArr[j+1] = tempKeyVal; } } }   };    for (k in inputArr) { if (inputArr.hasOwnProperty(k)) { valArr.push(inputArr[k]); keyArr.push(k); } }/*Get key and value*/ try { bubbleSort(keyArr, valArr, sub_key); } catch (e) { return false; }/*Sort our new temporary arrays*/ for (i = 0; i < valArr.length; i++) { populateArr[keyArr[i]] = valArr[i]; }/*Repopulate the old array*/ } return populateArr; };

function do_items_sorted(part, key) { if (typeof key == 'undefined') { key = $('[name=calculator_armor_items_sort]:checked').val; } calculator_armor_items_sorted = calculator_armor_items_data[part]; if (key == 'name') { calculator_armor_items_sorted = calculator_array_sort(calculator_armor_items_sorted, false, true); } else { calculator_armor_items_sorted = calculator_array_sort(calculator_armor_items_sorted, false, false, true, key); } }

calculator_armor_calculate = function { var x, lvl = 0, oz = 0, arm = 0, dmg_prot = [], tmp, dmg_type = $('#calculator_armor_damage_type :selected').text; $('#calculator_armor_links').html('Links to items '); for (x=0;x<calculator_armor_parts_names.length;x++) { tmp = unescape($('#calculator_armor_body_'+calculator_armor_parts_names[x]).attr('src').replace(/.*\/([^\/]*)\..*/, '$1').replace(/_/g,' ').toLowerCase); if (!tmp) { continue; } arm += calculator_armor_items_data[x][tmp].arm; oz += calculator_armor_items_data[x][tmp].oz; lvl = (calculator_armor_items_data[x][tmp].lvl > lvl ? calculator_armor_items_data[x][tmp].lvl : lvl); if ('no'+calculator_armor_parts_names[x] != tmp.toLowerCase) { $('#calculator_armor_links').append(''+calculator_armor_items_data[x][tmp].name+'</a> '); }   if ((tmp = calculator_armor_items_data[x][tmp].resist[dmg_type.toLowerCase]) !== 0) { dmg_prot.push(tmp); } } $('#calculator_armor_links a').click(function{window.open(this.href); return false;}); tmp = []; for (x=0;x<dmg_prot.length;x++) { tmp.push(dmg_prot[x]+'%'); } $('#calculator_armor_set_prot').text(dmg_type+' protection: '+(!tmp.length ? '0%' : tmp.join(', '))); var calculate_damage = function(total_arm, damage, prot) { var r1 = 0, r2 = 0, r3; damage = parseInt(damage, 10); if ($('#calculator_armor_damage_type').val == 'physical') { $.each(prot, function(i, v) { r1 += Math.ceil(damage * (v * 0.01)); r2 += Math.ceil(damage * (v * 0.01)); }); r1 += Math.floor(total_arm / 2); r2 += (Math.floor(total_arm / 2) * 2) - 1; r1 = r1 < 0 ? 0 : r1; r2 = r2 < 0 ? 0 : r2; r3 = r3 < 0 ? 0 : r3; r1 = r1 > damage ? damage : r1; r2 = r2 > damage ? damage : r2; r3 = r3 > damage ? damage : r3; r3 = (r2+r1)/2; // min/max (avg) return (r2 > damage ? 0 : (damage - r2))+'/'+(r1 > damage ? 0 : (damage - r1))+' ('+ (r3 > damage ? 0 : (damage - r3)) +')'; }   else { r1 = damage; $.each(prot, function(i, v) { r1 = parseInt(((100 - v)/100)*r1, 10); }); return r1; } }; $('#calculator_armor_damages tr:eq(1) td').text(function(i) {    if (i != 8) { return calculate_damage(arm, $('#calculator_armor_damages tr:eq(0) td:eq('+i+')').text, dmg_prot); }    return calculate_damage(arm, $('#calculator_armor_damages input').val, dmg_prot);  }); $('#calculator_armor_req_level').html(lvl || 'None'); oz = ''+oz; $('#calculator_armor_set_oz').html(oz + (oz.match(/\.\d/) ? '0' : (oz.match(/\.\d\d/) ? '' : '.00')) +' oz'); $('#calculator_armor_set_arm').html(arm); };

$('#calculator_armor_damage_type').change(function { $('[name=calculator_armor_items_sort][value='+$('#calculator_armor_damage_type').val+']').attr('checked', 'checked');  $('#calculator_armor_damage_type_ind').text($('#calculator_armor_damage_type :selected').text);  $('#calculator_armor_body_'+calculator_armor_parts_names[calculator_armor_current_part]).click;  calculator_armor_calculate; });

$('#calculator_armor_voc').change(function { var need_clear = false;  $.each(calculator_armor_parts_names, function(i, v) { var tmp = unescape($('#calculator_armor_body_'+v).attr('src').replace(/.*\/([^\/]*)\..*/, '$1').replace(/_/g,' ').toLowerCase); if (!tmp) { return; } if (calculator_armor_items_data[i][tmp].voc !== 0 && !(calculator_armor_items_data[i][tmp].voc & parseInt($('#calculator_armor_voc').val, 10))) { need_clear = true; return false; } }); if (need_clear) {    if (confirm('This will reset the set, continue?')) {      $.each(calculator_armor_parts_names, function(i, v) { var x = 'No'+v.slice(0, 1).toUpperCase+v.slice(1); $('#calculator_armor_body_'+v).attr('src', calculator_armor_get_link_for_name(x, i)).attr('alt', ''); });     calculator_armor_voc = parseInt($('#calculator_armor_voc').val, 10);    }    else { $('#calculator_armor_voc').val(calculator_armor_voc); }  }  else { calculator_armor_voc = parseInt($('#calculator_armor_voc').val, 10); }  $('#calculator_armor_body_'+calculator_armor_parts_names[calculator_armor_current_part]).click;  calculator_armor_calculate; });

$('#calculator_armor_custom_damage').keyup(function{ $(this).val(parseInt($(this).val, 10) || 0); if ($(this).val=='0') { $(this).select; }calculator_armor_calculate; });

calculator_armor_tt_html = function(d) { var arm_att_resist, att_resist, oz, x, voc_lvl = [], resist = []; for (x in d.resist) { if (d.resist.hasOwnProperty(x)) { if (d.resist[x] !== 0) { resist.push(x + ' '+(d.resist[x] > 0?'+':'')+d.resist[x]+'%'); } } } resist = resist.length ? 'protection '+resist.join(', ') : ''; if (d.voc & 1) { voc_lvl.push('druids'); } if (d.voc & 2) { voc_lvl.push('knights'); } if (d.voc & 4) { voc_lvl.push('paladins'); } if (d.voc & 8) { voc_lvl.push('sorcerers'); } voc_lvl = (voc_lvl.length || d.lvl) ? ' It can only be wielded properly by '+(voc_lvl.length ? voc_lvl.join(' and ') : 'players')+(d.lvl ? ' of level '+d.lvl+' or higher' : )+'.' : ; att_resist = (d.att ? (resist.length ? [d.att, resist] : [d.att]) : (resist.length ? [resist] : [])).join(', '); arm_att_resist = (d.arm ? 'Arm:' + d.arm + (att_resist.length ? ', ' : ) : ); arm_att_resist += (att_resist ? att_resist : ''); oz = ''+d.oz; oz = oz + (oz.match(/\.\d/) ? '0' : (oz.match(/\.\d\d/) ?  : '.00')); return +d.name+ + (arm_att_resist ? ' ('+arm_att_resist+')' : )+ voc_lvl + (d.oz ? ' It weighs '+oz+' oz.' : ''); }; $('#calculator_armor_body_main img').click(function(e) { var tmp, x, y, t = parseInt(calculator_armor_names_parts[$(this).attr('id').split('_')[3]], 10);  calculator_armor_current_part = t; do_items_sorted(calculator_armor_current_part);  $('#calculator_armor_items_div').html('');  for (x in calculator_armor_items_sorted) { if (calculator_armor_items_sorted.hasOwnProperty(x)) {    tmp = true; for (y in calculator_armor_items_sorted[x].resist) { if (calculator_armor_items_sorted[x].resist.hasOwnProperty(y)) {      if (calculator_armor_items_sorted[x].resist[y] !== 0) { tmp = false; break; }    } }    if (('no'+calculator_armor_parts_names[t] != x.toLowerCase) && !$('#calculator_armor_np').is(':checked') && tmp && calculator_armor_items_sorted[x].arm === 0) { continue; }    if (calculator_armor_items_sorted[x].voc !== 0 && calculator_armor_voc !== 0 && !(calculator_armor_items_sorted[x].voc & calculator_armor_voc)) { continue; } $('#calculator_armor_items_div').append(     $('<img class="item_img" '+'src="'+calculator_armor_items_sorted[x].ur+'" '+ 'alt="'+calculator_armor_items_sorted[x].name+'" '+ 'width="32" height="32" />')); } } $('.item_img').css('cursor', 'pointer') .click(function{   $('#calculator_armor_body_'+calculator_armor_parts_names[calculator_armor_current_part]).attr('src', $(this).attr('src')).attr('alt', $(this).attr('alt'));    calculator_armor_calculate;  }) .mousemove(function(e) {   if ($(this).attr('alt') === '') { return; }    var t = calculator_armor_items_data[calculator_armor_current_part][unescape($(this).attr('src').replace(/.*\/([^\/]*)\..*/, '$1').replace(/_/g,' ').toLowerCase)];    $('#calculator_armor_tt_items').show.css({top:(e.pageY+20)+'px', left:(e.pageX+10)+'px'}).html(calculator_armor_tt_html(t));  }) .mouseout(function(e){$('#calculator_armor_tt_items').hide;}); if ($('#calculator_armor_tt_items').size === 0) { $('body:eq(0)').append($('<div id="calculator_armor_tt_items"> ')); } }) .mousemove(function(e) { if ($(this).attr('alt') === '') { return; } var t = calculator_armor_items_data[calculator_armor_names_parts[$(this).attr('id').split('_')[3]]][unescape($(this).attr('src').replace(/.*\/([^\/]*)\..*/, '$1').replace(/_/g,' ').toLowerCase)]; $('#calculator_armor_tt_items').show.css({top:(e.pageY+20)+'px', left:(e.pageX+10)+'px'}).html(calculator_armor_tt_html(t)); }) .mouseout(function(e){$('#calculator_armor_tt_items').hide;});

$('#calculator_armor_np').click(function{ $('#calculator_armor_body_'+calculator_armor_parts_names[calculator_armor_current_part]).click; }); $('[name=calculator_armor_items_sort]').click(function { $('#calculator_armor_body_'+calculator_armor_parts_names[calculator_armor_current_part]).click; });

calculator_armor_items_data = [get_items_data('amulet'), get_items_data('ring'), get_items_data('helmet'), get_items_data('armor'), get_items_data('legs'), get_items_data('boots')];

$('#calculator_armor_body_helmet').click; calculator_armor_calculate;

/*General*/ $('#calculators_loading').hide; $('#calculators_container').show; //