(function($) {
	$.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: 15,
			cursorLeftOffset: 15,
			imgIcoHref : '',
			loadingHtml: "<span style='padding: 5px;'>Loading</span>"
		}, options);
		
		//Create our popup element
		popup =
		$("<div />")
		.css(settings.popupCSS)
		.attr("id", settings.popupId)
		.css("position", "absolute")
		.appendTo("body").hide();
		
		//Attach hover events that manage the popup
		$(this)
		.hover(setPopup)
		.mousemove(updatePopupPosition)
		.mouseout(hidePopup);
		
		function setPopup(event)
		{
			var fullImgURL;
			if ($(this).attr('data') == '')
	 			fullImgURL = $(this).attr("src").replace(settings.imgSmallFlag, settings.imgLargeFlag);
	 		else
	 			fullImgURL = $(this).attr('data');
			
			$(this).data("hovered", true);
			
			//Load full image in popup
			$("<img />")
			.bind("load", {thumbImage: this}, function(event)
			{
				//Only display the larger image if the thumbnail is still being hovered
				if ($(event.data.thumbImage).data("hovered") == true) {
					$(popup).empty().append(this);
					updatePopupPosition(event);
					$(popup).fadeIn('slow');
				}
				$(event.data.thumbImage).data("cached", true);
			})
			.attr("src", fullImgURL);
			
			//If no image has been loaded yet then place a loading message
			if ($(this).data("cached") != true) {
				$(popup).append($(settings.loadingHtml));
				$(popup).fadeIn('slow');
			}
			
			updatePopupPosition(event);			
		}
		
		function updatePopupPosition(event)
		{
			var windowSize = getWindowSize();
			var popupSize = getPopupSize();
			$(popup).css({
				left: windowSize.scrollLeft + Math.ceil((windowSize.width / 2) - (popupSize.width / 2)),
				top : windowSize.scrollTop  + Math.ceil((windowSize.height / 2) - (popupSize.height / 2))
			})
			/*
			if (windowSize.width + windowSize.scrollLeft < event.pageX + popupSize.width + settings.cursorLeftOffset){
				$(popup).css("left", event.pageX - popupSize.width - settings.cursorLeftOffset);
			} else {
				$(popup).css("left", event.pageX + settings.cursorLeftOffset);
			}
			if (windowSize.height + windowSize.scrollTop < event.pageY + popupSize.height + settings.cursorTopOffset){
				$(popup).css("top", event.pageY - popupSize.height - settings.cursorTopOffset);
			} else {
				$(popup).css("top", event.pageY + settings.cursorTopOffset);
			}
			*/
		}
		
		function hidePopup(event)
		{
			if (
				(event.pageX >= popup.offset().left && event.pageX <= (popup.width() + popup.offset().left)) &&
				(event.pageY >= popup.offset().top && event.pageY <= (popup.height() + popup.offset().top))
			) {
				var self = this;
				popup.mouseout(function(){
					$(self).trigger('mouseout');
				})
				return ;
			}
			$(this).data("hovered", false);
			$(popup).fadeOut('slow', function(){$(this).empty().hide();})
		}
		
		function getWindowSize() {
			return {
				scrollLeft: $(window).scrollLeft(),
				scrollTop: $(window).scrollTop(),
				width: $(window).width(),
				height: $(window).height()
			};
		}
		
		function getPopupSize() {
			return {
				width: $(popup).width(),
				height: $(popup).height()
			};
		}

		/*$.each(this, function(){
			$(this).css('position', 'relative');
			var $_img = $('img', this);
			var ico_zoom = $(new Image()).attr('src' , settings.imgIcoHref);
				ico_zoom.css({
					position: 'absolute',
					marginLeft: $_img.width() - 18,
					marginTop : $_img.height() - 18
				})
				
				.attr('data', $(this).attr('data'))
				.hover(setPopup)
				.mousemove(updatePopupPosition)
				.mouseout(hidePopup);
				
				
			$(this).prepend(ico_zoom);
		})
		*/
		
		//Return original selection for chaining
		return this;
	};
})(jQuery);
