var s3prices=new Array(3750,3150,2625,2625,1125,3750,1000,1000,1875,1875,1875,1875,1500,1125,1125,1000,1000,1000);
var s2prices=new Array(3261,2739,2283,2283,978,3261,870,870,1630,1630,1630,1630,1304,978,978,870,870,870);

function calc_arena_points(arenaRating, multFactor, outputId)
{
	var arenaRatingLength = arenaRating.length;
	if (arenaRatingLength == 0)
	{
		$('#' + outputId).text('0');
	}
	else if (arenaRating > 1500)
	{
		points = (1511.26 / (1 + 1639.28 * Math.pow(2.71828, -0.00412 * arenaRating))) * multFactor;
		$('#' + outputId).text(Math.floor(points), true);
	}
	else if (arenaRating <= 1500)
	{
		points = (0.22 * arenaRating + 14) * multFactor;
		$('#' + outputId).text(Math.floor(points), true);
	}
	update_gear_costs();
}

function update_gear_costs()
{
	var currpoints = $('#pointinput').val();
	var currlength = currpoints.length;
	if (currlength != 0)
		currpoints = parseInt(currpoints);
	else
		currpoints = 0;	
	var twopoints = parseInt($('#twoout').text());
	var threepoints = parseInt($('#threeout').text());
	var fivepoints = parseInt($('#fiveout').text());		
	if (twopoints > threepoints && twopoints > fivepoints)
	{
		currpoints += twopoints;
		if (currpoints > 5000)
			currpoints = 5000;
		$('span#highpoints').text(twopoints, true);
		$('span#totalpoints').text(currpoints, true);
	}
	else if (threepoints > twopoints && threepoints > fivepoints)
	{
		currpoints += threepoints;
		if (currpoints > 5000)
			currpoints = 5000;
		$('span#highpoints').text(threepoints, true);
		$('span#totalpoints').text(currpoints, true);
	}
	else if (fivepoints >= twopoints && fivepoints >= threepoints)
	{
		currpoints += fivepoints;
		if (currpoints > 5000)
			currpoints = 5000;
		$('span#highpoints').text(fivepoints, true);
		$('span#totalpoints').text(currpoints, true);
	}
	
	if (currpoints == 0)
		$('span#totalpoints').text('Total Arena Points', true);
	
	for (var i = 0; i < s2prices.length; i++)
	{
		var s3pointdelta = s3prices[i] - currpoints;
		var s2pointdelta = s2prices[i] - currpoints;
		if (s2pointdelta < 1)
		{
			$('#s2pointsneed' + (i+1)).addClass('negNeed');
			$('#s2pointsneed' + (i+1)).removeClass('posNeed');
		}
		if (s2pointdelta >= 1)
		{
			$('#s2pointsneed' + (i+1)).removeClass('negNeed');
			$('#s2pointsneed' + (i+1)).addClass('posNeed');
		}
		if (s3pointdelta < 1)
		{
			$('#s3pointsneed' + (i+1)).addClass('negNeed');
			$('#s3pointsneed' + (i+1)).removeClass('posNeed');
		}
		if (s3pointdelta >= 1)
		{
			$('#s3pointsneed' + (i+1)).removeClass('negNeed');
			$('#s3pointsneed' + (i+1)).addClass('posNeed');
		}
		$('#s3pointsneed' + (i+1)).text(s3pointdelta, true);
		$('#s2pointsneed' + (i+1)).text(s2pointdelta, true);	
	}
}

$(document).ready(function()
{		
	$('#ratinginputfive').val('');
	$('#ratinginputthree').val('');
	$('#ratinginputtwo').val('');
	$('#pointinput').val('');
	
	$('#seasonsel').change(function()
	{
		if(this.value == 'season3')
		{
			$('.season3').removeClass('disabled');
			$('.season2').addClass('disabled');
		}
		if(this.value == 'season2')
		{
			$('.season3').addClass('disabled');
			$('.season2').removeClass('disabled');
		}
	});
	
	$('#ratinginputfive').keyup(function() { calc_arena_points($(this).val(), 1.0, 'fiveout') });
	$('#ratinginputthree').keyup(function() { calc_arena_points($(this).val(), 0.88, 'threeout') });
	$('#ratinginputtwo').keyup(function() { calc_arena_points($(this).val(), 0.76, 'twoout') });
	
	$('#pointinput').keyup(update_gear_costs);

});
