function RankingElement(name, coefficient, power) {
	this.name = name;
	this.c = coefficient;
	this.p = power;
}

var rankParts = new Array();
rankParts[0] = new RankingElement("Links to the Element",1,2);
rankParts[1] = new RankingElement("Links to Elements Linked to the Element",1,1);
rankParts[2] = new RankingElement("Position in Article",0,1);
rankParts[3] = new RankingElement("Sentence in Article",0,1);

function rank() {
	var L2V, L2L2V, PoV;
	var links = new Array();
	var sentences, sentence;
	sentences = 0;
	for (i = 0; i < V.length; i++) {
		if (V[i].word == ".")
			sentences++;
	}
	sentence = sentences;
	
	for (i = 0; i < V.length; i++) {
		V[i].ranking = 0;
		L2V = 0;
		links.clear();
		
		// find the rank due to links to that element
		for (j = 0; j < E.length; j++) {
			if (E[j].parent == i) {
				L2V++;
				links.push(E[j].child);
			} else if (E[j].child == i) {
				L2V++;
				links.push(E[j].parent);
			}
		}
		V[i].ranking += rankParts[0].c*Math.pow(L2V,rankParts[0].p);
		
		// find the rank due to links to elements linked to the element.
		L2L2V = 0;
		for (j = 0; j < E.length; j++) {
			if (links.indexOf(E[j].parent) != -1) {
				L2L2V++;
			} else if (links.indexOf(E[j].child) != -1) {
				L2L2V++;
			}
		}
		V[i].ranking += rankParts[1].c*Math.pow(L2L2V,rankParts[1].p);
		
		// find the rank due to the position in the article.
		V[i].ranking += rankParts[2].c*Math.pow(i,rankParts[2].p);
		
		// find the rank due to the sentence in the article.
		if (V[i].word == ".")
			sentence--;
		V[i].ranking += rankParts[3].c*Math.pow(sentence,rankParts[3].p);
	}
}

function fillEditRankingBox() {
	editRankingBox = $('editRankingBox');
	editRankingBox.innerHTML = "<h2>Ranking</h2>";
	for (i = 0; i < rankParts.length; i++) {
		editRankingBox.innerHTML += "<div id='editRankParts" + i + "'>";
		editRankingBox.innerHTML += "<input type='text' size='2' class='coefficient' value='"+rankParts[i].c+"' onChange='rankParts["+i+"].c=this.value;rank();showDistribution();' /> * ";
		editRankingBox.innerHTML += rankParts[i].name;
		editRankingBox.innerHTML += " <input type='text' size='2' class='power' value='"+rankParts[i].p+"' onChange='rankParts["+i+"].p=this.value;rank();showDistribution();' />";
		if (i+1 < rankParts.length)
			editRankingBox.innerHTML += " + ";
		editRankingBox.innerHTML += "</div>";
	}
}

function showDistribution() {
	var buckets = 35;
	highestRanking = 0;
	// find the highest ranking
	for (i = 0; i < V.length; i++) {
		if (V[i].ranking > highestRanking)
			highestRanking = V[i].ranking;
	}
	
	// initialize a histogram with 50 buckets
	var histogram = new Array();
	for (i = 0; i <= buckets; i++) {
		histogram[i] = new Object();
		histogram[i].n = 0;
		histogram[i].v = 0;
		histogram[i].x = 0;
		histogram[i].total = 0;
	}
	
	// find out how many of each tag type are in each bucket
	var bucketSize = highestRanking/buckets;
	var biggestBucket = 0;
	for (i = 0; i < V.length; i++) {
		bucket = Math.floor(V[i].ranking/bucketSize);
		histogram[bucket].total++;
		if (histogram[bucket].total > biggestBucket)
			biggestBucket = histogram[bucket].total;
		if (V[i].tag == "n")
			histogram[bucket].n++;
		else if (V[i].tag == "v")
			histogram[bucket].v++;
		else
			histogram[bucket].x++;
	}
	
	var barWidth = 370;
	
	var tableOutput = "<table id='distribution'><tr><td> </td><th>Nouns</th><th>Verbs</th><th>Others</th></tr>";
	for (i = 0; i < histogram.length; i++) {
		tableOutput += "<tr><th>"+Math.round(i*bucketSize)+"</th>";
		if (histogram[i].n > 0)
			tableOutput += "<td class='nouns' style='width: "+Math.round(histogram[i].n/biggestBucket*barWidth)+"px' title='["+Math.round(i*bucketSize)+"-"+Math.round((i+1)*bucketSize)+"]: "+histogram[i].total+" total, "+histogram[i].n+" nouns' onClick='highlightWordsRankedBetween("+(i*bucketSize)+","+((i+1)*bucketSize)+",\"n\",event)'>"+histogram[i].n+"</td>";
		else
			tableOutput += "<td class='empty'></td>";
			
		if (histogram[i].v > 0)
			tableOutput += "<td class='verbs' style='width: "+Math.round(histogram[i].v/biggestBucket*barWidth)+"px' title='["+Math.round(i*bucketSize)+"-"+Math.round((i+1)*bucketSize)+"]: "+histogram[i].total+" total, "+histogram[i].v+" verbs' onClick='highlightWordsRankedBetween("+(i*bucketSize)+","+((i+1)*bucketSize)+",\"v\",event)'>"+histogram[i].v+"</td>";
		else
			tableOutput += "<td class='empty'></td>";
			
		if (histogram[i].x > 0)
			tableOutput += "<td class='others' style='width: "+Math.round(histogram[i].x/biggestBucket*barWidth)+"px' title='["+Math.round(i*bucketSize)+"-"+Math.round((i+1)*bucketSize)+"]: "+histogram[i].total+" total, "+histogram[i].x+" others' onClick='highlightWordsRankedBetween("+(i*bucketSize)+","+((i+1)*bucketSize)+",\"\",event)'>"+histogram[i].x+"</td>";
		else
			tableOutput += "<td class='empty'></td>";
		tableOutput += "</tr>";	
	}
	tableOutput += "</table>";
	$('distributionBox').innerHTML = tableOutput;
}


fillEditRankingBox();
showDistribution();	
			
		