(function($){
	$.fn.truncation = function(options) {
		var defaults = {
			trail: "&#8230;(more info)",
			margin: -2, //"..." is 1 character whereas "&#8230;" is 7 characters
			ie7margin: -2,
			//provide these options in case these ids and classes are already in use
			temp: "temp",
			link: "view-more",
			height: $(this).height(),
			maxCharLength: 250
		};
		options = $.extend(defaults, options);
		
		return this.each(function(){
			//create outside div to store content
			$("body").append("<div id='" + options.temp + "' style='position:absolute;'></div>");
			
			//cache selectors
			var thisEl = $(this);
			var temp = $("#"+options.temp);
			
			var content = thisEl.html(); // grab data from box
			
			temp.html(content); // store content in outside temp div
			
			//grab css for copying
			var fontFamily = thisEl.css("font-family");
			var fontWeight = thisEl.css("font-weight");
			var fontSize = thisEl.css("font-size");
			var letterSpacing = thisEl.css("letter-spacing");
			var textAlign = thisEl.css("text-align");
			var lineHeight = thisEl.css("line-height");
			var boxWidth = thisEl.width();
			
			//copy css
			temp.css({
				"font-family": fontFamily,
				"width": boxWidth,
				"font-weight": fontWeight,
				"font-size": fontSize,
				"letter-spacing": letterSpacing,
				"text-align": textAlign,
				"line-height": lineHeight
			});
			
			// grab height of box just inserted
			var height = temp.height();
			
			//pre-loop code. create copy of div
			var truncateYes = false;
			if (height > options.height) {
				thisEl.data("fullText", content);
				truncateYes = true;
			}
			
            var newText; //holds our truncated text
			//hack off the to the max length before getting precise
			newText = temp.text().substr(0, options.maxCharLength);
			temp.text(newText);
			
			//loop. start hacking away at the text
			while (height > options.height) {
				//-1 takes long, but it's accurate. -10 takes less time, but it is less accurate
				newText = temp.text().substr(0, temp.text().length - 1);
				
				temp.text(newText);
				height = temp.height();
			}
			
			//post-loop code. if text was shorten, attached the "view more" link to the end
			if (truncateYes) {
				var margin = ($.browser.msie && $.browser.version == "7.0") ? options.ie7margin : options.margin;
				newText = temp.text().substr(0, temp.text().length - options.trail.length - margin);
				thisEl.html(newText + "<span class='" + options.link + "'>" + options.trail + "</span>");
			}
			
			temp.remove(); //remove temporary div
		});
	};
})(jQuery);
