/*
 * Truncate Text in fixed Layouts (1.0)
 * by Marcus Viefeld (viefeld.net)
 * 
 * Based on ideas from "Auto Expanding Text Area" Plugin by Chrys Bader (www.chrysbader.com)
 *
 * Copyright (c) 2009 Marcus Viefeld (viefeld.net)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 */

(function($) {
		  
	var self = null;
 
	$.fn.truncateText = function(options)
	{	
		return this.each(function() {
			$.fn.truncateText.shorten(this, options);
		});
	};
	
	
    $.fn.truncateText.shorten = function(container, options) {
	
		this.dummy = null;
		
		this.dummy = $('<div></div>');
		this.dummy.css({
			'font-size'  : $(container).css('font-size'),
			'font-family': $(container).css('font-family'),
			'width'      : $(container).width(),
			'padding'    : $(container).css('padding'),
			'line-height': $(container).css('line-height'),
			'position'   : 'absolute',
			'top'        : 0,
			'left'		 : -9999
		}).appendTo('body');
	
	
		var html = $(container).html().replace(/\s+$/,'').replace(/^\s+/,'').replace(/<[^>]*?>/g, '');
		
		this.dummy.html(html);
		var h = this.dummy.height();
		var ch = $(container).height();
		
		var weiterlesen = $(container).parent().parent().find('h3 a').attr('href');
		
		
		
		//this.dummy.html ( h + ' - ' + $(container).height() + ' | ' + this.dummy.width() + ' - ' +$(container).css('width') + ' - ' +$(container).width());
		
		if (h > ch)
		{
			while (h > ch)
			{
				html = html.substring(0, html.length-5).replace(/\S+$/, '');
				this.dummy.html(html + ' &nbsp;|&hellip;|&nbsp; weiter lesen');
				h = this.dummy.height();
			}
			
			$(container).html(html + ' &nbsp;|&hellip;|&nbsp; <a class="more" href="' +weiterlesen+ '">weiter lesen</a>');
		}
	
		this.dummy.remove();
	};

})(jQuery);