/* Euler constant */
var e = Math.E;

/* how many points a certain rating will yield */
function ap(rating)
{
	return Math.max(343.93522377, 1511.26 / (1 + 1639.28 * Math.pow(e, -0.00412 * rating)));
}

/* how many points a 2v2 team will gain for their rating */
function ap2v2(rating)
{
	return Math.round(ap(rating) * 0.76);	
}

/* how many points a 3v3 team will gain for their rating */
function ap3v3(rating)
{
	return Math.round(ap(rating) * 0.88);	
}

/* how many points a 5v5 team will gain for their rating */
function ap5v5(rating)
{
	return Math.round(ap(rating));
}

function ra(points)
{
	return Math.log((1511.26 / points - 1) / 1639.28) / Math.log(e) / -0.00412;
}

function ra2v2(points)
{
	return Math.ceil(ra(points / 0.76));
}

function ra3v3(points)
{
	return Math.ceil(ra(points / 0.88));
}

function ra5v5(points)
{
	return Math.ceil(ra(points));	
}

/* jQuery scripts */

$(document).ready(function() {
	$(".innhold").hide();
	$("#forside").show();
	$(".craft").hide();
	$(".calc").hide();

	$("ul.tabs li a").click(function() {
		if(!$(this).parent().hasClass("active") && $(this).attr("title") !== "Forum") {
			$(".active").removeClass("active");
			$(this).parent().addClass("active");
			$(".innhold").slideUp();
			$("#" + $(this).attr("title").toLowerCase()).slideDown();
		}
	});
	
	$("#crafting li a.switch").click(function() {
		if(!$(this).parent().hasClass("open")) {
			$(".open").removeClass("open");
			$(this).parent().addClass("open");
			$(".craft").slideUp();
			$("#" + $(this).attr("title").toLowerCase()).slideDown();
		}
	});	
	
	$("#diverse li a.switch").click(function() {
		if(!$(this).parent().hasClass("open")) {
			$(".open").removeClass("open");
			$(this).parent().addClass("open");
			$(".calc").slideUp();
			$("#" + $(this).attr("title").toLowerCase()).slideDown();
		}
	});
	
	$("#pointkalkulator input").bind("change keyup", function() {
		$("#pointkalkulator .output p.2v2").html("2v2: " + ap2v2($(this).attr("value")));
		$("#pointkalkulator .output p.3v3").html("3v3: " + ap3v3($(this).attr("value")));
		$("#pointkalkulator .output p.5v5").html("5v5: " + ap5v5($(this).attr("value")));
	});
	
	$("#ratingkalkulator input").bind("change keyup", function() {
		var need = Math.max(0, $("#ratingpris").attr("value") - $("#ratingpoints").attr("value"));				
		$("#ratingkalkulator .output tbody tr").each(function(i) {
			var r2 = ra2v2(need / (i + 1));
			var r3 = ra3v3(need / (i + 1));
			var r5 = ra5v5(need / (i + 1));
			$(this).children("td.2v2").html(isNaN(r2) ? "<span>ikke mulig</span>" : r2);
			$(this).children("td.3v3").html(isNaN(r3) ? "<span>ikke mulig</span>" : r3);
			$(this).children("td.5v5").html(isNaN(r5) ? "<span>ikke mulig</span>" : r5);
		});
	});
	
	$("#tidskalkulator input").bind("change keyup", function() {
		var need = Math.max(0, $("#tidspris").attr("value") - $("#tidspoints").attr("value"));
		$("#tidskalkulator .output p.2v2").html("2v2: " + Math.ceil(need / Math.max(1, ap2v2($("#tidsrating").attr("value")))) + " uker");
		$("#tidskalkulator .output p.3v3").html("3v3: " + Math.ceil(need / Math.max(1, ap3v3($("#tidsrating").attr("value")))) + " uker");
		$("#tidskalkulator .output p.5v5").html("5v5: " + Math.ceil(need / Math.max(1, ap5v5($("#tidsrating").attr("value")))) + " uker");
	});
});