/**
 * jQuery imageDescription
 * Version 0.9 - 15.02.2011
 * @author Artur Riewe
 *
 * Häng einem Bild eine Bildunterschrift aus dem alt-Tag an.
 * Es sind keinerlei Voraussetzungen für die Bilder nötig.
 *
 * Beispiel:
 * $.imageDescription("#inhalt span.bildbeschreibung img");
 *
 **/

$.extend({
	imageDescription : function(query)
	{
		function _transform()
		{
			// Jedes Element durchlaufen
			$(query).each(function(){
				var alt = $(this).attr('alt');
				var pos = $(this).position();

				var bild_breite = $(this).css("width").replace("px","");
				var bild_hoehe = $(this).css("height").replace("px","");
				var pos_top = parseInt(pos.top) + parseInt(7) + parseInt(bild_hoehe);
				var pos_left = pos.left + 5;

				var style = 'display: block;';
				style += ' position: absolute; ';
				style += ' background-color: #ffffff;';
				style += ' width: '+bild_breite + 'px;';
				style += ' padding: 0px;';
				style += ' left: '+pos_left+'px;';
				style += ' top: '+pos_top+'px;';

				$(this).after('<div class="ccc" style="' + style + '"><div style="font-size: 10px; line-height: 12px; padding: 4px 4px 4px 0px;">'+alt+'</div></div>');

			});

			_createMargin();	// Abstand erzeugen
		}

		// Abstand nach unten hin erzeugen
		function _createMargin()
		{
			$(query).each(function(){
				var h = $(this).parent().find(".ccc").height();
				$(this).css("margin-bottom",h+"px");
			});
		}

		// Die zentrale Funktion einmalig aufrufen
		_transform();

		
		
		// Beim Resizen die Bildbeschreibung nochmal neu generieren
		$(window).bind('resize scroll load', function() {
			$(query).next().remove();
			_transform();
		});
		
	}
});

