(function(jQuery) {
	jQuery.fn.thumbPopup = function(options)
	{
		//Combine the passed in options with the default settings
		settings = jQuery.extend({
			popupId: "thumbPopup",
			popupCSS: {'border': '1px solid #000000', 'background': '#FFFFFF'},
			imgSmallFlag: "_t",
			imgLargeFlag: "_l",
			cursorTopOffset: 5,
			cursorLeftOffset: 15,
			loadingHtml: "<span style='padding: 5px;'>Grafik wird geladen</span>"
		}, options);
		
		//Create our popup element
		popup =
		jQuery("<div />")
		.css(settings.popupCSS)
		.attr("id", settings.popupId)
		.css("position", "absolute")
		.appendTo("body").hide();
		
		//Attach hover events that manage the popup
		jQuery(this)
		.hover(setPopup, hidePopup)
		.mousemove(updatePopupPosition)
		.mouseout(hidePopup);
		
		function setPopup(event)
		{
			var fullImgURL =  jQuery(this).attr("hover_src");
//                        fullImgURL = jQuery(this).attr("src").replace(settings.imgSmallFlag, settings.imgLargeFlag);
			
			jQuery(this).data("hovered", true);
			
			//Load full image in popup
			jQuery("<img />")
			.bind("load", {thumbImage: this}, function(event)
			{
				//Only display the larger image if the thumbnail is still being hovered
				if (jQuery(event.data.thumbImage).data("hovered") == true) {
					jQuery(popup).empty().append(this);
					updatePopupPosition(event);
					jQuery(popup).show();
				}
				jQuery(event.data.thumbImage).data("cached", true);
			})
			.attr("src", fullImgURL);
			
			//If no image has been loaded yet then place a loading message
			if (jQuery(this).data("cached") != true) {
				jQuery(popup).append(jQuery(settings.loadingHtml));
				jQuery(popup).show();
			}

			updatePopupPosition(event);			
		}
		
		function updatePopupPosition(event)
		{
			var windowSize = getWindowSize();
			var popupSize = getPopupSize();
                        var top;
			if (windowSize.width + windowSize.scrollLeft < event.pageX + popupSize.width + settings.cursorLeftOffset + 10){
				jQuery(popup).css("left", event.pageX - popupSize.width - settings.cursorLeftOffset);
			} else {
				jQuery(popup).css("left", event.pageX + settings.cursorLeftOffset);
			}
                        top = event.pageY + settings.cursorTopOffset;
                        if (top + popupSize.height > windowSize.scrollTop + windowSize.height - 10) {
                            top = windowSize.scrollTop + windowSize.height - 10 - popupSize.height;
                        }
                        jQuery(popup).css("top", top);
		}
		
		function hidePopup(event)
		{
			jQuery(this).data("hovered", false);
			jQuery(popup).empty().hide();
		}
		
		function getWindowSize() {
			return {
				scrollLeft: jQuery(window).scrollLeft(),
				scrollTop: jQuery(window).scrollTop(),
				width: jQuery(window).width(),
				height: jQuery(window).height()
			};
		}
		
		function getPopupSize() {
			return {
				width: jQuery(popup).width(),
				height: jQuery(popup).height()
			};
		}
                
		//Return original selection for chaining
		return this;
	};
})(jQuery);