/**
 * Copyright (c) 2007 Ingo Schommer (www.chillu.com)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * Splits a <ul>/<ol>-list into equal-sized columns.
 * 
 * Requirements: 
 * <ul>
 * <li>All list-elements need to have the same height.</li>
 * <li>List has to be blocklevel</li>
 * </ul>
 * 
 * Caution: margin-top/margin-left on <li> are overridden.
 * Doesn't react to changes to the DOM, you need to call the function
 * manually afterwards.
 * 
 * @see http://www.alistapart.com/articles/multicolumnlists
 */
jQuery.fn.columnizeList = function(settings){
	settings = jQuery.extend({
		cols: 3,
		width: '13',
		unit: 'em'
	}, settings);
	
  	var prevColNum = 0;
  	var size = $('li',this).size();
  	var computedColHeight = 0;
  	var baseFontSize = parseFloat($(this).css('font-size'));
  	$('li',this).each(function(i) {
		var currentColNum = Math.floor(((i)/size) * settings.cols);
		$(this).css('margin-left',(currentColNum*settings.width)+''+settings.unit);
		if(prevColNum != currentColNum) {
			$(this).css('margin-top','-'+(computedColHeight/baseFontSize)+'em');
			computedColHeight = $(this).height();
		} else {
			$(this).css('margin-top','0');
			computedColHeight += $(this).height();
		}
		prevColNum = currentColNum;
	});

	this.css('height',(size/settings.cols)*(parseFloat($('li:first',this).height())/baseFontSize)+'em');
	this.after('<br style="clear: left;" />');
	
	var onchange = function(e) {
		if(!e.originalTarget || e.originalTarget.tagName != 'LI') return true;
		var scope = this; // caution: closure
		setTimeout(function() {$(scope).columnizeList(settings);}, 50);
	};
	
	this.one('DOMNodeInserted',onchange);
	this.one('DOMNodeRemoved',onchange);
	
	return this;
};

jQuery.fn.uncolumnizeList = function(){
	$('li',this).each(function(i) {
		if(!$(this).attr('style')) return;
		$(this).attr('style', 
			$(this).attr('style')
			.replace(/margin\-left[^,]*/g,'')
			.replace(/margin\-top[^,]*/g,'')
		);
	});
	$('ul',this).each(function(i) {
		if(!$(this).attr('style')) return;
		$(this).attr('style', 
			$(this).attr('style')
			.replace(/[^-]height[^,]*/g,'')
		);
	});
	$(this).height('auto');
	this.unbind('DOMNodeInserted');
	this.unbind('DOMNodeRemoved');
	
	return this;
}

/**
 * @see ttp://groups.google.com/group/jquery-plugins/browse_thread/thread/f4a39d579af3dc2e
 */
jQuery.fn.defuscate = function(settings) { 
   settings = jQuery.extend({link: true}, settings); 
   return this.each(function(){ 
     $(this).html($(this).html().replace(/\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9- ]+\.)+[A-Z]{2,6})\b/gi, settings.link ? '<a href="mailto:$1@$2">$1@$2</a>' : "$1@$2")); 
   }); 

};