
function sizeUp() {
	var ch = $("body").get(0).clientHeight;
	var page = $("#page");
	if (ch < page.height()) {
		page.height("auto");
	} else {
		page.height("100%");
	}
}

function limitSize(e, chars) {
	var len = e.text().length;
	var text = e.text();

	if (len <= chars) return;

	// break at a space if possible
	var idx = text.lastIndexOf(" ", chars);
	var brkPoint = (idx) ? idx : chars;
	var	href = e.prev("h3").eq(0).children("a").eq(0).attr("href");
	e.html(text.substring(0, brkPoint) + " <a href=\"" + href +"\">... more</a>");
}

function wordTrimmer(word, len) {
	var w = word;

	if (word.length > len) {
		var l = word.substring(0, len);
		var r = word.substring(len);
		w = l + "<span title=\"" + word + "\">...</span>";
		//debug(w);
	}
	return w;
}

function lineTrimmer(line, len) {
	var words = line.split(" ");
	var newLine = "";
	for (var i = 0; i < words.length; i++) {
		newLine += wordTrimmer(words[i], len) + " ";
	}
	return newLine;
}

function loop(e) {
	var t = document.createElement("span");
	
	for (var i = 0; i < e.childNodes.length; i++) {
		var child = e.childNodes[i];
		if (child.nodeType == Node.TEXT_NODE) {
			t.innerHTML = lineTrimmer(child.nodeValue, 20);
			
			e.insertBefore(t, child);
			//e.removeChild(e.childNodes);
		} else if (child.nodeType == Node.ELEMENT_NODE) {
			loop(child);
		}
	}
}

function ligature(text) {
    //console.log(text);
    var last = "";
    var newText = "";
    for (var i = 0; i < text.length; i++) {
        var ch = text.charAt(i);
        //console.log(ch);
        if (last == "f") {
            newText = newText.substr(0, newText.length - 1);
            ch = "&#64257;";
            last = "";
        }
        if (ch == "f") {
            last = ch;
        }
        newText += ch;
    }
    //console.log(newText);
    return newText;
}

$(document).ready(function() {
    /*
    $("h1, #navigation").each(function() {
        $(this).text(ligature($(this).text()));
    });
    */
	$("a img").each(function(i) { $(this).parent().css("background-color", "transparent"); });
	$("#older .entry .content").each(
  		function() { 
			limitSize($(this), 300);
		}
	);
});
