


/**
 * Counts the top contributors. 
 */
function topContributors() {
	var users = count("div.topDeltakere div.deltaker");
	
	var counter = 1;
	var src = '<table cellpadding="5" cellspacing="5" class="topContributorsTable"><tr><td class="header">' + userLabel + '</td><td class="header" align="right">' + countLabel + '</td></tr>';
	while (true) {
		var userArray = getHighest(users);
		if (userArray == undefined || counter >= 5) {
			break;
		}
		src += '<tr><td>' + userArray[4] + '</td><td align="right">' + userArray[1] + '</td></tr>'; 
		users[userArray[0]] = undefined;
		counter++;
	}
	src += "</table>";
	$("div.topContributors").html(src);
}


/**
 * Counts the contributors. 
 */
function countContributors() {
	var users = count("div.alleDeltakere div.deltaker");
	
	var src = '<table cellpadding="5" cellspacing="5" class="contributorsTable"><tr><td class="header">' + countLabel + '</td><td class="header">' + userLabel + '</td><td class="header">' + firstEditLabel + '</td><td class="header">' + lastEditLabel + '</td></tr>';
	while (true) {
		var userArray = getHighest(users);
		if (userArray == undefined) {
			break;
		}
		src += '<tr><td>' + userArray[1] + '</td><td>' + userArray[4] + '</td><td>' + userArray[2] + '</td><td>' + userArray[3] + '</td></tr>';
		users[userArray[0]] = undefined;
	}
	src += "</table>";
	$("div.contributorsTable").html(src);
}


/**
 * Returns the user with the highest count from a given array with users. 
 * @param users The array with the users to get the one with the highest count from.
 */
function getHighest(users) {
	var highestName = "";
	var highestCount = 0;
	
	for (var userName in users) {
		var userArray = users[userName];
		if (userArray == undefined || userArray[1] == undefined) {
			continue;
		}
		var userArray = users[userName];
		var count = userArray[1];
		if (count >= highestCount) {
			highestCount = count;
			highestName = userName;
		}
	}
	
	var userArray = users[highestName];
	return userArray;
}


/**
 * Counts the users, for each user the name, count and first and last edit date are recorded. 
 * @param contributors The jquery selector to select the contributors
 * @return The array containing the users. 
 */
function count(contributors) {
	var users = new Array();
	$(contributors).each(function() {
		var navnLink = $("div.deltakerNavn", this).html();
		var navn = $(navnLink).html();
		var dato = $("div.endretDato", this).html();
		if (!containsUser(navn, users)) {
			// user does not exist yet, create a array for the user
			var userArray = new Array();
			userArray[0] = navn;
			// count
			userArray[1] = 1;
			// first date
			userArray[2] = dato;
			// last date
			userArray[3] = dato;
			// user link
			userArray[4] = navnLink;
			users[navn] = userArray;
		}
		else {
			// users exists already update the count and last edit date
			users[navn][1] = users[navn][1] + 1;
			users[navn][3] = dato;
		}
	});
	return users;
}


/** 
 * Checks if a given array contains a given user.
 * @param user The user to check for. 
 * @param users The array to check in. 
 * @return True if user is in users, false otherwise. 
 */
function containsUser(user, users) {
	//for (var i = 0; i < users.length; i++) {
	for (var userName in users) {
		if (users[userName][0] == user) {
			return true;
		}
	}
	return false;
}

