// This script picks out the large part of a web page, but it is very
// much work in process.

var body = document.getElementsByTagName("body")[0];
var best = body;

function mySize(object){
    // This should give a high score for stuff that looks like text.
    return object.textContent.split(/ +/).length;
}

function guts(bot, bottom){
    var active = false;
    var bits = best.getElementsByTagName("div");
    var biggest;
    var oldSize = 0;
    var size = 0;

    for (var i=0; i < bits.length; i++){
	var newSize = mySize(bits[i]);
	oldSize += newSize;
	if (newSize > size){
	    size = newSize;
	    biggest = bits[i];
	}
    }

    if ((size > bot*oldSize) && (size > bottom)){
	active = true;
	best = biggest;
    }

    return active;
}

function loop(){
    var active = true;
    var body = best;

    while (active)
	active = guts(0.10, 100);

    body.style.backgroundColor = "white";
    body.style.color = "black";
    body.style.font="12pt Helvetica";
    body.style.margin = "2%";
    body.align = "left";
    body.innerHTML = best.innerHTML;

    var articleTitle = document.createElement("H3");
    var articleFirst = body.firstChild;
    articleTitle.innerHTML = "From " + document.location;
    body.insertBefore(articleTitle, articleFirst);

    var bits = body.getElementsByTagName("div");
    for (var i=0; i < bits.length; i++){
	bits[i].align = "left";
	bits[i].style.margin = "1%";
	bits[i].style.backgroundColor = "white";
	bits[i].style.color = "black";
    }
}

if (document.kalvjaAbig){
    document.body.style.fontSize
        = document.body.style.fontSize.replace(/pt/, "")*1.2 + "pt";
} else {
    document.kalvjaAbig = true;
    loop();
}
