last = [];
last['STR'] = 0;
last['DEX'] = 0;
last['CON'] = 0;
last['WIS'] = 0;
last['INT'] = 0;
last['CHA'] = 0;

str = [];
str[7] = ['CDMG',-2];
str[8] = ['CDMG',-2];
str[9] = ['CDMG',-1];
str[10] = ['CDMG',-1];
str[11] = ['CDMG',0];
str[12] = ['CDMG',0];
str[13] = ['CDMG',1];
str[14] = ['CDMG',1];
str[15] = ['CDMG',2];
str[16] = ['CDMG',2];
str[17] = ['CDMG',3];
str[18] = ['CDMG',3];
str[19] = ['CDMG',4];
str[20] = ['CDMG',4];
str[21] = ['CDMG',5];
str[22] = ['CDMG',5];
str[23] = ['CDMG',6];
str[24] = ['CDMG',6];
str[25] = ['CDMG',6];
str[26] = ['CDMG',7];
str[27] = ['CDMG',7];
str[28] = ['CDMG',7];
str[29] = ['CDMG',8];
str[30] = ['CDMG',8];
str[31] = ['CDMG',9];
str[32] = ['CDMG',9];
str[33] = ['CDMG',10];
str[34] = ['CDMG',11];
str[35] = ['CDMG',12];
str[36] = ['CDMG',12];
str[37] = ['CDMG',12];
str[38] = ['CDMG',12];
str[39] = ['CDMG',13];
str[40] = ['CDMG',13];
str[41] = ['CDMG',13];
str[42] = ['CDMG',13];
str[43] = ['CDMG',14];
str[44] = ['CDMG',14];
str[45] = ['CDMG',14];
str[46] = ['CDMG',14];
str[47] = ['CDMG',15];
str[48] = ['CDMG',15];
str[49] = ['CDMG',15];
str[50] = ['CDMG',15];

dex = [];
dex[7] = ['RDMG',0];
dex[8] = ['RDMG',0];
dex[9] = ['RDMG',0];
dex[10] = ['RDMG',0];
dex[11] = ['RDMG',0];
dex[12] = ['RDMG',0];
dex[13] = ['RDMG',0];
dex[14] = ['RDMG',0];
dex[15] = ['RDMG',1];
dex[16] = ['RDMG',2];
dex[17] = ['RDMG',3];
dex[18] = ['RDMG',4];
dex[19] = ['RDMG',4];
dex[20] = ['RDMG',4];
dex[21] = ['RDMG',5];
dex[22] = ['RDMG',5];
dex[23] = ['RDMG',5];
dex[24] = ['RDMG',6];
dex[25] = ['RDMG',6];
dex[26] = ['RDMG',6];
dex[27] = ['RDMG',7];
dex[28] = ['RDMG',7];
dex[29] = ['RDMG',7];
dex[30] = ['RDMG',8];
dex[31] = ['RDMG',8];
dex[32] = ['RDMG',8];
dex[33] = ['RDMG',9];
dex[34] = ['RDMG',9];
dex[35] = ['RDMG',9];
dex[36] = ['RDMG',10];
dex[37] = ['RDMG',10];
dex[38] = ['RDMG',10];
dex[39] = ['RDMG',10];
dex[40] = ['RDMG',11];
dex[41] = ['RDMG',11];
dex[42] = ['RDMG',11];
dex[43] = ['RDMG',11];
dex[44] = ['RDMG',12];
dex[45] = ['RDMG',12];
dex[46] = ['RDMG',12];
dex[47] = ['RDMG',12];
dex[48] = ['RDMG',13];
dex[49] = ['RDMG',13];
dex[50] = ['RDMG',13]; 

$(document).ready(function () {
	
    $(".valup").bind("mousedown", function(e) {
    	
    	$(this).css({'background-image' : 'url(_gfx/bnt_up_in.gif)'});
    });
    $(".valup").bind("mouseup", function(e) {
    	
    	$(this).css({'background-image' : 'url(_gfx/bnt_up.gif)'});
    });

    $(".valdown").bind("mousedown", function(e) {
    	
    	$(this).css({'background-image' : 'url(_gfx/bnt_down_in.gif)'});
    });
    $(".valdown").bind("mouseup", function(e) {
    	
    	$(this).css({'background-image' : 'url(_gfx/bnt_down.gif)'});
    });


});

function InitStats() {

	$('#info').html('Distribute your bonus points.');
	
	$('#bnt_elixir').css({'opacity' : 0.4});
	$('#bnt_autolvl').css({'opacity' : 0.4});
	$('#bnt_levelup').css({'opacity' : 0.4});
	
	for(key in init) {
		curr[key] = init[key];
		if(key != 'STR' && key != 'DEX' && key != 'CON' && key != 'WIS' && key != 'INT' && key != 'CHA') 
			base[key] = 0;

	}
	
	for(key in last)
		last[key] = 0;
		
	UpdateStats();
}

function GiveElixir() {
	if(curr['LEVEL'] < 45) {
		$('#info').html('Using an elixir requires to be level 45 or higher.');

	}
	if(curr['elixir'] <= 0) {
		$('#info').html('You already added the maximum of 5 elixirs.');	

	}
	if(curr['elixir'] > 0 && curr['LEVEL'] >= 45) {
		curr['elixir']--;
		curr['POINTS']++;
		UpdateStats();
	} else {
	
		$('#bnt_elixir').css({'opacity' : 0.4});
	}
}


function UpdateStats() {

	UpdateBoni();
	for(key in curr) {
		
		var obj = document.getElementById(key);
		if(obj)
			obj.innerHTML = curr[key];

	}

	if(curr['POINTS'] == 0 && curr['LEVEL'] < 99) {
		if(curr['LEVEL'] < 50)
			$('#bnt_autolvl').css({'opacity' : 1.0});
		$('#bnt_levelup').css({'opacity' : 1.0});
		$('#info').html('Ready to level up.');
	} else {
		$('#info').html('Distribute your bonus points.');
		$('#bnt_autolvl').css({'opacity' : 0.4});
		$('#bnt_levelup').css({'opacity' : 0.4});	
	
	}
	
	if(document.getElementById('statlvlup').checked == true && curr['LEVEL'] > 50)
		LevelUp();
}

function LevelUp() {

	var thisLevel = curr['LEVEL'];

	if(curr['POINTS'] > 0) {
		$('#info').html('Please distribute your bonus points first.');
		return;
	}
		
	
	if(thisLevel >= 45 && curr['elixir'] > 0)
		$('#bnt_elixir').css({'opacity' : 1.0});
	else
		$('#bnt_elixir').css({'opacity' : 0.4});	
	
	for(stat in last)
		last[stat] = curr[stat];
	
	if(thisLevel >= 99)
		return;

	thisLevel++;

	if(thisLevel > 50)
		curr['POINTS']++;
		
	curr['LEVEL'] = thisLevel;

	var min = 0;
	var max = 3;
	curr['HP'] += curr['HPLVL']+(Math.round((max-min) * Math.random() + min));
	curr['MP'] += curr['MPLVL']+Math.round(Math.random());
	UpdateStats();
}

function AutoLevel() {

	if(curr['POINTS'] == 0 && curr['LEVEL'] < 50) {
	
		while(curr['LEVEL'] < 51) {
			LevelUp();
		}
	
	
	}

}




function StatIncrease(stat) {

	if(curr[stat] >= 35 || curr['POINTS'] == 0)
		return;

	var nextStat = curr[stat]+1;
	var baseLen = base[stat].length;
	var maxStat = base[stat][baseLen-1][0];
	if(curr['LEVEL'] == 1 && nextStat > maxStat)
		return;

		
	curr[stat]++;
	curr['POINTS']--;	

	if(curr['LEVEL'] == 1) {
		curr['MP'] = Math.floor(curr['WIS']/4)-2+init['MP'];
		
		
		var arLen = base[stat].length;
		for(var x=0; x < arLen; x++) {
			if(curr[stat] == base[stat][x][0]) {
				base[base[stat][x][1]] += base[stat][x][2];	
			}
		}
	
	}
	

	UpdateStats();

}

function StatDecrease(stat) {
	
	if(curr[stat] <= init[stat])
		return;
	
	if(curr['LEVEL'] == 1) {
	
		var arLen = base[stat].length;
		for(var x=0; x < arLen; x++) {
			if(curr[stat] == base[stat][x][0]) {
				base[base[stat][x][1]] -= base[stat][x][2];	
			}
		}
		
	}
	
	
	if(curr['LEVEL'] > 50)
		if(last[stat] >= curr[stat])
			return;
	
	curr[stat]--;
	curr['POINTS']++;	

	if(curr['LEVEL'] == 1)
		curr['MP'] = Math.floor(curr['WIS']/4)-2+init['MP'];
	
	UpdateStats();

}



function UpdateBoni() {

	// Weight Capacity *complete
	curr['WEIGHT'] = Math.round((curr['STR']+curr['CON']+1)/2)*150;
	curr['WEIGHT'] += base['WEIGHT'];
	if(curr['WEIGHT'] > 5400)
		curr['WEIGHT'] = 5400;
	
	
	// Armor Class *complete
	var acDif = 8;
	if(curr['DEX'] >= 10 && curr['DEX'] <= 12)
		acDif = 7;
	if(curr['DEX'] >= 13 && curr['DEX'] <= 15)
		acDif = 6;
	if(curr['DEX'] >= 16 && curr['DEX'] <= 17)
		acDif = 5;
	if(curr['DEX'] >= 18)
		acDif = 4;
		
	curr['AC'] = init['AC']-Math.floor(curr['LEVEL']/acDif);
	curr['AC'] += base['AC'];
	
	// Evasion Rate *complete
	curr['ER']  = (Math.floor(curr['DEX']/2)-4)
	curr['ER'] += (Math.floor(curr['LEVEL']/lvlBonus['ER'][0]));
	curr['ER'] += base['ER'];
	
	
	// Magic Resistance *complete
	if(curr['WIS'] <= 14) {
		curr['MR'] = 0;
	} else if(curr['WIS'] <= 16) {
		curr['MR'] = 3;
	} else if(curr['WIS'] <= 16) {
		curr['MR'] = 3;
	} else if(curr['WIS'] == 17) {
		curr['MR'] = 6;
	} else if(curr['WIS'] <= 18) {
		curr['MR'] = 10;
	} else if(curr['WIS'] <= 19) {
		curr['MR'] = 15;
	} else if(curr['WIS'] <= 20) {
		curr['MR'] = 21;
	} else if(curr['WIS'] <= 21) {
		curr['MR'] = 28;
	} else if(curr['WIS'] <= 22) {
		curr['MR'] = 37;
	} else if(curr['WIS'] <= 23) {
		curr['MR'] = 47;
	} else if(curr['WIS'] >= 24) {
		curr['MR'] = 50;
	}
	curr['MR'] += init['MR']+Math.floor(curr['LEVEL']/lvlBonus['MR'][0]);
	curr['MR'] += base['MR'];
	
	// Dex Hit Bonus
	if(curr['DEX'] < 19)
		var dexHit = Math.round(curr['DEX']/2)-5;
	else
		var dexHit = curr['DEX']-14;

	//****** Melee Hit Bonus *complete
	if(curr['STR'] < 18)
		curr['CHIT'] = Math.floor(curr['STR']/2)-5;
	else
		curr['CHIT'] = Math.floor(curr['STR']/3)-2;
	curr['CHIT'] += dexHit;
	curr['CHIT'] += base['CHIT'];
	// Level Hit Bonus 
	if(lvlBonus['CHIT'])
		curr['CHIT'] += Math.floor(curr['LEVEL']/lvlBonus['CHIT'][0]);	


	//****** Melee Damage Bonus *complete
	curr['CDMG'] = str[curr['STR']][1];
	curr['CDMG'] += base['CDMG'];
	if(lvlBonus['CDMG'])
		curr['CDMG'] += Math.floor(curr['LEVEL']/lvlBonus['CDMG'][0]);


	//****** Ranged Hit Bonus
	curr['RHIT'] = dexHit;
	curr['RHIT'] += base['RHIT'];
	if(lvlBonus['RHIT'])
		curr['RHIT'] += Math.floor(curr['LEVEL']/lvlBonus['RHIT'][0]);
	
	
	//****** Ranged Damage Bonus
	curr['RDMG'] = dex[curr['DEX']][1];
	curr['RDMG'] += base['RDMG'];
	if(lvlBonus['RDMG'])
		curr['RDMG'] += Math.floor(curr['LEVEL']/lvlBonus['RDMG'][0]);	


	//****** HP Level Up
	curr['HPLVL'] = init['HPLVL'];
	if(curr['CON'] > 15)
		curr['HPLVL'] += curr['CON']-15;
	curr['HPLVL'] += base['HPLVL'];
	
	//****** HP Regeneration
	if(curr['CON'] < 14)
		curr['HPGEN'] = 1;
	else
		curr['HPGEN'] = curr['CON']-12;
	curr['HPGEN'] += base['HPGEN'];
	
	
	//****** MP Level Up
	curr['MPLVL'] = init['MPLVL'];
	if(curr['WIS'] > 15) {
		curr['MPLVL'] += (curr['WIS']-14);
	} else {
		curr['MPLVL'] += Math.round(curr['WIS']/2-5.5);
	}
	curr['MPLVL'] += base['MPLVL'];
	
	
	
	//****** MP Regeneration
	if(curr['WIS'] < 11)
		curr['MPGEN'] = 1;
	else
		curr['MPGEN'] = curr['WIS']-10;
	curr['MPGEN'] += base['MPGEN'];


	//****** Magic Bonus
	if(curr['INT'] < 18)
		curr['MB'] = Math.floor(curr['INT']/2)-5;
	if(curr['INT'] >= 18 && curr['INT'] <= 25)
		curr['MB'] = curr['INT']-15;
	if(curr['INT'] >= 26 && curr['INT'] <= 34)
		curr['MB'] = 10;
	if(curr['INT'] >= 35 && curr['INT'] <= 44)
		curr['MB'] = 11;		

	curr['MB'] += base['MB'];
	
	
	//****** Spell Power
	curr['SP'] = curr['MB'];
	curr['SP'] += base['SP'];
	
	
	//****** Magic Hit
	curr['MHIT'] = base['MHIT'];
	
	
	//****** Magic Crit
	curr['MCRIT'] = base['MCRIT'];
	
	//****** MP Discount
	curr['MPREQ'] = 0;
	if(curr['INT'] >= 12 && curr['INT'] <= 18)
		curr['MPREQ'] = -1*(curr['INT']-12);
	if(curr['INT'] >= 19 && curr['INT'] <= 22)
		curr['MPREQ'] = -7;
	if(curr['INT'] >= 23 && curr['INT'] <= 26)
		curr['MPREQ'] = -8;
	if(curr['INT'] >= 27)
		curr['MPREQ'] = -9;
	if(curr['MPREQ'] <= -maxMagicLvl)
		curr['MPREQ'] = -1*(maxMagicLvl-1);
	curr['MPREQ'] += base['MPREQ'];
		
	//****** Add fixed level bonus
	var arLen = fixBonus.length;
	for(var x=0; x < arLen; x++) {
		if(curr['LEVEL'] >= fixBonus[x][0])
			curr[fixBonus[x][1]] += fixBonus[x][2];	
	}
		
}
