// skills js functions

// This code is in the public domain. Feel free to link back to http://jan.moesen.nu/
function sprintf() {
	if (!arguments || arguments.length < 1 || !RegExp) {
		return;
	}
	var str = arguments[0];
	var re = /([^%]*)%(\'.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
	var a = b = [], numSubstitutions = 0, numMatches = 0;
	while (a = re.exec(str)) {
		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
		var pPrecision = a[5], pType = a[6], rightPart = a[7];
	
		//alert(a + \'\n\' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

		numMatches++;
		if (pType == '%')	{
			subst = '%';
		} else {
			numSubstitutions++;
			if (numSubstitutions >= arguments.length) {
				alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
			}
			var param = arguments[numSubstitutions];
			var pad = '';
			if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
			else if (pPad) pad = pPad;
			var justifyRight = true;
			if (pJustify && pJustify === "-") justifyRight = false;
			var minLength = -1;
			if (pMinLength) minLength = parseInt(pMinLength);
			var precision = -1;
			if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
			var subst = param;
			if (pType == 'b') subst = parseInt(param).toString(2);
			else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
			else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
			else if (pType == 'u') subst = Math.abs(param);
			else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
			else if (pType == 'o') subst = parseInt(param).toString(8);
			else if (pType == 's') subst = param;
			else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
			else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
		}
		str = leftpart + subst + rightPart;
	}
	return str;
}

skills = new Object();
summon = new Object();
stats = new Object();
var allskills = 0;
var lastModifiedSkill = -1;
var pointsLeft = 120;
var skill_request = false;
var requestWait = 1000; 
var loadedSkill = -1;
var lastRequest = 0;

function switchSkillTab(num) {
	var parent = document.getElementById("skilltab");
	var elems = document.getElementsByClassName("skill");
	var i = 0;
	var len = elems.length;
	while(document.getElementsByClassName("skill").length > 0) {
		parent.removeChild(document.getElementsByClassName("skill")[0]);
		parent.removeChild(document.getElementsByClassName("skill_num")[0]);
	}
	parent.style.backgroundImage = "url(/itemdb/img/ui/skills/" + charname + "/skilltab_" + num + ".gif)";
	var skillid = -1;
	for (i = 0; i < skilltabs[num - 1].skills.length; i++) {
		skillid = skilltabs[num - 1].skills[i].id;
		parent.innerHTML += skilltabs[num - 1].skills[i].html;
		var elem = document.getElementById("skill_num_" + skillid);
		if(elem != null) {
			elem.innerHTML = skills[skillid].lvl;
			if(skills[skillid].lvl > 0) {
				document.getElementById("skill_" + skillid).innerHTML = "";
			}
		}
	}
	updateReqs();
}
		
function switchImage(id, num) {
	document.getElementById(id).style.backgroundImage = "url(/itemdb/img/ui/skills/" + charname + "/" + iconprefix + "skillicon0x" + num + ".gif)";
}
			
function hideSkillInfo() {
	if(infoElem == null) {
		infoElem = document.getElementById("itemInfo");
	}
	infoElem.style.display = "none";
}

function addAllSkills(n) {
	allskills = Math.max(allskills + n, 0);
	updateAllSkillsDisplay();
}

function remAllSkills(n) {
	allskills = Math.max(allskills - n, 0);
	updateAllSkillsDisplay();
}

function updateAllSkillsDisplay() {
	var e = document.getElementById("allskills");
	if(e != null) {
		e.innerHTML = allskills;
	}
}

function resetSkills() {
	pointsLeft = 120;
	document.getElementById("skillPoints").innerHTML = pointsLeft;
	for (id in skills) {
		skills[id].lvl = 0;
		if(document.getElementById("skill_num_" + id) != null) {
			document.getElementById("skill_num_" + id).innerHTML = skills[id].lvl; 
		}
	}
	updateReqs();
	lastModifiedSkill = -1;
}

function maxLastSkill() {
	if(lastModifiedSkill == -1 || skills[lastModifiedSkill] == null || skills[lastModifiedSkill].lvl == 20 || !skillReqMet(lastModifiedSkill)) {
		return;
	}
	var maxTo = Math.min(pointsLeft, 20 - skills[lastModifiedSkill].lvl);
	addLastSkill(maxTo);
}

function addLastSkill(n) {
	if(lastModifiedSkill == -1 || skills[lastModifiedSkill] == null || skills[lastModifiedSkill].lvl == 20 || !skillReqMet(lastModifiedSkill)) {
		return;
	}
	if(pointsLeft < n) {
		return;
	}
	skills[lastModifiedSkill].lvl += n;
	pointsLeft -= n;
	document.getElementById("skill_num_" + lastModifiedSkill).innerHTML = skills[lastModifiedSkill].lvl;
	document.getElementById("skillPoints").innerHTML = pointsLeft;
	updateReqs();
}

function remLastSkill(n) {
	if(lastModifiedSkill == -1 || skills[lastModifiedSkill] == null || skills[lastModifiedSkill].lvl == 0 || !skillReqMet(lastModifiedSkill)) {
		return;
	}
	var rem = Math.min(skills[lastModifiedSkill].lvl, n);
	skills[lastModifiedSkill].lvl -= rem;
	pointsLeft += rem;
	document.getElementById("skill_num_" + lastModifiedSkill).innerHTML = skills[lastModifiedSkill].lvl;
	document.getElementById("skillPoints").innerHTML = pointsLeft;
	updateReqs();
}
			
function addPointToSkill(id, num) {
	if(pointsLeft > 0 && skillReqMet(id)) {
		if(skills[id].lvl == 0) {
			skills[id].lvl = 1;
			updateReqs();
		} else if(skills[id].lvl < 20) {
			skills[id].lvl++;
		} else {
			switchImage("skill_" + id, num);
			return;
		}
		pointsLeft--;
		document.getElementById("skill_num_" + id).innerHTML = skills[id].lvl;
		document.getElementById("skill_" + id).innerHTML = "";
		document.getElementById("skillPoints").innerHTML = pointsLeft;
		lastModifiedSkill = id;
	}
	switchImage("skill_" + id, num);
	eval("showSkill_" + id + "(document.getElementById(\"skill_\" + id), skills[id].lvl);");
	loadedSkill = -1;
}

function updateReqs() {
	for(var id in pres) {
		elem = document.getElementById("skill_" + id);
		if(skillReqMet(id) && elem != null) {
			elem.innerHTML = "";
		} else if(!skillReqMet(id) && elem != null) {
			elem.innerHTML = "<div class=\"skill_overlay skill_disabled\"></div>";
		}
	}
}

function skillReqMet(id) {
	if(skipReqCheck) {
		return true;
	} else if(pres[id] == -1 || typeof(pres[id]) == "number" && skills[pres[id]].lvl > 0) {
		return true;
	} else if(typeof(pres[id]) == "number") {
		return false;
	} else {
		for(var i = 0; i < pres[id].length; i++) {
			if(skills[pres[id][i]].lvl == 0) {
				return false;
			}
		}
		return true;
	}
}

function calcSkillLevelDmg(level, a1, a2, a3, a4, a5) {
	if(level == 0) { level = 1; }
	var dmg = 0;
	if(level > 0) {
		dmg = 0;
	}
	if(level >= 2) {
		dmg += a1 * Math.min(level - 1, 7);
	}
	if(level >= 9) {
		dmg += a2 * Math.min(level - 8, 8);
	}
	if(level >= 17) {
		dmg += a3 * Math.min(level - 16, 6);
	}
	if(level >= 23) {
		dmg += a4 * Math.min(level - 22, 6);
	}
	if(level > 28) {
		dmg += a5 * (level - 28);
	}
	return Math.floor(dmg);
}

function calcSkillDmg(min, max, nl1, nl2, nl3, nl4, nl5, xl1, xl2, xl3, xl4, xl5, src, shift, syn, level) {
	if(level == 0) { level = 1; }
	dmg = new Array(2);
	syn = (100 + syn) / 100;
	var enhancer = 0;
	enhancer = (100 + enhancer) / 100;
	//baseMin = min * (src / 128);
	//baseMax = max * (src / 128);
	var levelMin = calcSkillLevelDmg(level, nl1, nl2, nl3, nl4, nl5);
	var levelMax = calcSkillLevelDmg(level, xl1, xl2, xl3, xl4, xl5);
	dmg[0] = (((min + levelMin) << shift) * syn * enhancer / 256);
	dmg[1] = (((max + levelMax) << shift) * syn * enhancer / 256);
	return dmg;
}

colors = new Array("grey", "red", "green", "blue", "gold", "dimgray", "white", "tan", "orange", "yellow");

function calcToHit(level, tohitcalc, tohit, levtohit) {
	if(level == 0) { level = 1; }
	return (tohitcalc != 0 ? tohitcalc : tohit + levtohit * (level - 1));
}

function calcLN(a, b, l) {
	if(l == 0) { return 0; }
	return a + (l - 1) * b;
}

function calcDM(a, b, l) {
	if(l == 0) { return a; }
	return (110 * l * (b - a)) / (100 * (l + 6)) + a;
}

function calcManaCost(mana, lvlmana, manashift, level) {
	if(level == 0) { level = 1; }
	return (mana + lvlmana * (level - 1)) * (1 << manashift);
}

function calcSkillLen(level, elen, elevlen1, elevlen2, elevlen3, elensympercalc) {
	if(level == 0) { level = 1; }
	var len = 0;
	if(level > 0) {
		len = elen;
	}
	if(level >=2) {
		len += elevlen1 * (level - 1);
	}
	if(level >= 9) {
		len += elevlen2 * (level - 8);
	}
	if(level >= 17) {
		len += elevlen3 * (level - 16);
	}
	len *= ((100 + elensympercalc) / 100);
	return len;
}

function min(a, b) {
	return Math.min(a, b);
}

function max(a, b) {
	return Math.max(a, b);
}
