(function($) {
	$.fn.imgdesc = function(set) {
		var defaults = {
				autohide : true,
				source : "title",
				minHeight : 100,
				minWidth : 100
			},
			options = $.extend(defaults, set);
	
		return $(this).each(function() {
			if(this.rel == "noDesc") return;
			
			var desc = this[options.source],
				imgWidth = this.width,
				imgHeight = this.height,
				imgClass,
				imgSrc,
				imgTag;
			
			if(desc.length > 45) {
				desc = desc.substr(1,45);
			}
			
			if( imgWidth > options.minWidth && imgHeight > options.minHeight ) {
				imgClass = this.className;
				imgSrc = this.src;
				
				if( !( $(this).parent("a")[0] ) ) { 
					imgTag = "<a href='"+ imgSrc + "' class='" + imgClass + " imgLink' style='width:" +imgWidth + "px;height: " + imgHeight + "px;display: block;overflow:hidden;'></a>";
					
					$(this).wrap(imgTag);
				}
				
				$(this).removeClass().after("<span class='imgDesc imaDesc-opacity'>"+desc+"</span>").next(".imgDesc").css({"top" : "-39px" , "width" : (imgWidth - 20) + "px" });	
				
				if( options.autohide ) {
					$(this).hover(function(){
						$(this).next(".imgDesc").fadeTo("fast" , 0.8);
					},function() {
						$(this).next(".imgDesc").fadeTo("fast", 0);
					});
				}
				else {
					$(this).next(".imgDesc").fadeTo("fast" , 0.8);
				}
				
				$(this).parent("a").css({"width":imgWidth,"height":imgHeight ,"display":"block", "overflow":"hidden"}).addClass(imgClass);
				
			}

		});
	};
	$.fn.imgDescDel = function() {
		return $(this).each(function() {
			//clean the anchor created by the plugin
			var classname = $(this).parent(".imgLink").removeClass("imgLink").attr("class");
			//restore the classname
			$(this).attr("class",classname).remove(".imgDesc");
		});
	};

})(jQuery);
