(function($) {
	$.fn.extend({
		// Toggle um das aside-Element ein- und auszublenden
		toggle_aside: function(elem) {
			var aside = $(elem);
			var top = aside.height() + 2;
			var button = $(this).find("div");
			var hidden = false;
			
			var result = button.hasClass("asc");
			if (result) hidden = true;
			
			if (hidden) {
				aside
					.stop(true, true)
					.animate(
						{top: "0px"}, 
						1000,
						"easeOutQuart",
						function() {
							button.removeClass("asc");
						});
			} else {
				aside
					.stop(true, true)
					.animate(
						{top: "-"+ top +"px"}, 
						1000,
						"easeOutQuart",
						function() {
							button.addClass("asc");
						});
			}
			
			return aside;
		},
		
		
		// Setzt die Groesse eines Input-Feldes automatisch auf die Laenge des Strings darin
		auto_size_input: function(options) {
			options = $.extend({
				maxWidth: 9999,
				minWidth: 0,
				comfortZone: 10,
				blur: false // bei "true" wird beim Blur des Eingabefeldes die Laenge zurueckgesetzt
			}, options);
			
			
			this.filter('input:text').each(function(){
				var minWidth = options.minWidth || $(this).width(),
					val = '',
					input = $(this),
					testSubject = $('<tester/>').css({
						position: 'absolute',
						top: -9999,
						left: -9999,
						width: 'auto',
						fontSize: input.css('fontSize'),
						fontFamily: input.css('fontFamily'),
						fontWeight: input.css('fontWeight'),
						letterSpacing: input.css('letterSpacing'),
						whiteSpace: 'nowrap'
					}),
					check = function() {
						if (val === (val = input.val())) {
							// Enter new content into testSubject
							var escaped = val.replace(/&/g, '&amp;').replace(/\s/g,'&nbsp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
							testSubject.html(escaped);
						}
						
						// Calculate new width + whether to change
						var testerWidth = testSubject.width(),
							newWidth = (testerWidth + options.comfortZone) >= minWidth ? testerWidth + options.comfortZone : minWidth,
							currentWidth = input.width(),
							isValidWidthChange = (newWidth < currentWidth && newWidth >= minWidth)
								|| (newWidth > minWidth && newWidth < options.maxWidth);
						
						// Animate width
						if (isValidWidthChange) {
							input.width(newWidth);
						}
					};
				testSubject.insertAfter(input);
				
				$(this).bind('keyup keydown focus update click', check);
				if (options.blur) {
					$(this).bind('blur', function() {
						input.css("width", "auto");
					});
				}
			});
			return this;
		},
		
		
		// Erstellt ein Tooltipp
		tooltipp: function(options) {
			// Standardsettings,  falls sie nicht im Objekt beim Aufruf mitgegeben wurden
			var settings = {
				id: "bubble",          // ID die der Tooltipp erhaelt
				attr: "info",          // Attribut, in dem die Daten stehen
				arrow: "10"            // Groesse des "Pfeils" unter dem Tooltipp
			}
			
			// Falls mit den Optionen kein Objekt uebergeben wird, wird angenommen dass es der Wert fuer "bubble" ist 
			if (typeof(options) != "object" && options != undefined ) options = {id: options};
			var cfg = $.extend({}, settings, options);
			
			
			$(this).bind({
				mouseenter: function() {
					var tr     = $(this);
					var pos    = tr.offset();
					var text   = tr.attr(cfg.attr);
					var ttip = $("<div>", {
						id:   cfg.id,
						html: text
					}).appendTo('body');
					
					ttip.css({
						top: pos.top - ttip.outerHeight() - cfg.arrow,
						left: pos.left 
					});
				},
				mouseleave: function() {
					$('#' + cfg.id).remove();
				}
			});
		},
		
		
		// Laesst ein Element wachsen, was ein Kindelement zu gross ist
		grow: function(options) {
			// Standardsettings,  falls sie nicht im Objekt beim Aufruf mitgegeben wurden
			var settings = {
				id: "ezOverview",     // ID des Elements, anhand dem sich die Groesse ausrichtet
				padding: 40           // Evt. vorhandenes Padding
			}
			
			// Falls mit den Optionen kein Objekt uebergeben wird, wird angenommen dass es der Wert fuer "id" ist 
			if (typeof(options) != "object" && options != undefined ) options = {id: options};
			var cfg = $.extend({}, settings, options);
			
			var elem = $(this);
			var child = $('#' + cfg.id);
			
			
			var wrap = elem.outerWidth();
			var table = child.outerWidth();
			var padding = 2 * cfg.padding;
			
			
			if (typeof jQuery.browser.webkit != "undefined")
				 var bugfix = 152;
			else var bugfix = 0;
			
			if (wrap < (table + padding))
				elem.css("width", table + padding + bugfix);
		}
	});
})(jQuery);

