/* 
 * Copyright 2011, Tobias Lindig 
 * 
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * 
 */

(function($) {
	$.fn.closestChild = function(selector) {
		// breadth first search for the first matched node
		if (selector && selector != '') {
			var queue = [];
			queue.push(this);
			while(queue.length > 0) {
				var node = queue.shift();
				var children = node.children();
				for(var i = 0; i < children.length; ++i) {
					var child = $(children[i]);
					if (child.is(selector)) {
						return child; //well, we found one
					}
					queue.push(child);
				}
			}
		}
		return $();//nothing found
	};
})(jQuery);
