var max_chars = 450;
var text_show = 'komplettes Zitat anzeigen';
var text_hide = 'verk&uuml;rztes Zitat anzeigen';
$(document).ready(function() {	
	$('.message_body').each(function(){
		$(this).children('blockquote').each(function(){
			$(this).addClass('quote_full');
			cite = $(this).children('cite').html();
			quote = $(this).html();
			if(quote.length > max_chars || howMany(quote) > 2) {
				if(quote.search('<blockquote>')) {
					last = quote.lastIndexOf('</blockquote>') + 12;	
				} else {
					last = 0;
				}
				quote = quote.substr(last);
				if(quote.search('<cite>')) {
					last = quote.lastIndexOf('</cite>') + 7;
				} else {
					last = 0;
				}
				quote = quote.substr(last);
				quote = quote.substr(0, max_chars);
				quote = quote.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi, "");
				$('<a href="javascript:;" class="quotelink italic">' + text_show + '</a>').insertAfter($(this));
				$('<blockquote class="quote_short"><cite><strong>' + cite + '</strong></cite>' + quote + ' ... ' + '</blockquote>').insertAfter($(this));
				$(this).hide();
			}
		});
	});
	$('.quotelink').click(function(){
		$(this).parent().children('blockquote.quote_full').toggle();
		$(this).parent().children('blockquote.quote_short').toggle();
		if($(this).html() == text_show) {
			$(this).html(text_hide);
		} else {
			$(this).html(text_show);
		}
	});
});
function howMany(string) {
	return string.toLowerCase().split('<cite>').length - 1;
}
