// Truncate long entries and add a Show More link
// By Todd Kummer, Rockridge Solutions

// requires prototype.js
var rosoShowMore = {

	linkText:"Read More...", 
	SHOW_MORE: "showMore",
	HIDE_MORE: "hideMore",
	WORD_LIMIT: 100,
	_logger: "",

	init: function (entry) {
		var children = $(entry).childElements();
		children = children.reject(function (c) {
			return (c.tagName.toUpperCase() == "BLOCKQUOTE");
		});
	
		// hide everything past limit
		var total = 0;
		children.each(function (c) {
			if (total < rosoShowMore.WORD_LIMIT) {
				// only let <p>'s push it past limit
				if (c.tagName.toUpperCase() == "P") {total = total + c.innerHTML.split(" ").length;}
				
			} else {
				c.addClassName(rosoShowMore.HIDE_MORE);
			}
		});

		// append a show more link
		if (total > rosoShowMore.WORD_LIMIT) {
			children.last().insert({after: this.getShowMoreLink()});
		}
	},
	
	getShowMoreLink: function(){
		var div = $(document.createElement("div"));
		div.addClassName(this.SHOW_MORE);
		
		var more = div.appendChild(document.createElement("a"));
		more.appendChild(document.createTextNode(this.linkText));
		Event.observe(more, "click", rosoShowMore.showMore);
		return div;
	},
	
	showMore: function(event){
		var element = Event.element(event);

		// show rest of entry
		element.up(".entry").select("." + rosoShowMore.HIDE_MORE).each(function(e) {
			e.removeClassName(rosoShowMore.HIDE_MORE);
		});

		// hide link
		element.hide(); 
	}
};


