(function($){
 
	$.fn.roundedImageCorners = function(options){
	
		// Merge defaults with supplied user options
		var opts = $.extend({}, $.fn.roundedImageCorners.defaults, options);
		
		// Iterate all selected images
		return this.each(function(){	
			// Get some values from the original image
			var $this = $(this);
			var imgSrc = $this.attr("src");
			var imgHeight = $this.height();
			var imgWidth = $this.width();
			var imgAltText = $this.attr("alt");
			var customImageClasses = $this.attr("class");

			// Wrap the image in a div
			$this.wrap("<div />");
			var wrapperDiv = $this.parent("div");
		
			// Give the div the same dimensions as the original image
			// and set the original image as the div background.
			wrapperDiv.css({"background":" transparent url(" + imgSrc + ") no-repeat top center", "height": imgHeight + "px", "width": imgWidth + "px"}).addClass(opts.imageWrapperDivClass);
			
			// Check if the user wants to use the alt tag of the
			// image as a caption?
			if(opts.altAsCaption && imgAltText !== "") {
				// Wrap the wrapper div in an additional div
				wrapperDiv.wrap("<div />");
				var imageAndCaptionWrapperDiv = wrapperDiv.parent("div");
				imageAndCaptionWrapperDiv.addClass(opts.imageAndCaptionWrapperDivClass);
				imageAndCaptionWrapperDiv.css({"width": imgWidth + "px"});
				
				// Add a paragraph tag for the caption
				wrapperDiv.after("<p>" + imgAltText + "</p>");
				var caption = imageAndCaptionWrapperDiv.children("p");
				caption.addClass(opts.captionClass);
				
				// Add any custom classes that the original image had
				// to the image and caption wrapper div
				imageAndCaptionWrapperDiv.addClass(customImageClasses);
			} else {
				// Add any custom classes that the original image had
				// to the wapper div
				wrapperDiv.addClass(customImageClasses);
			}
			
			// Remove the original image from the DOM
			$this.remove();
			
		});
	}
 
	$.fn.roundedImageCorners.defaults = {
		imageWrapperDivClass: "imageWrapper",
		altAsCaption: false,
		captionClass: "caption",
		imageAndCaptionWrapperDivClass : "imageAndCaptionWrapper"
		
	};
 
})(jQuery);

