/**
* jQuery Plugin Function
*
* Center images within a boxed area.
* Author: Morgan Newcomb
*/

(function($){
	$.fn.centerImage = function(options){
		// set default options
		var defaults = {
			width: 100,
			height: 100
		};
		
		// set options
		var options = $.extend(defaults, options);
		
		// find the image
  		var element = $(this).find('img');

		// wrap the image to confine within a div
		var elementBounds = $(element).wrap('<div />');
		
		// loop through all images
		$(element).each ( function(){
		
			$(this).parent().css({ // add css to our confining div
				'width' : options.width,
				'height' : options.height,
				'overflow' : 'hidden',
				'position' : 'relative'
			});
			
			$(this).load(function(){ // preload the image 
	   			$(this).css({ // position the image
	       			"position" : "absolute",  
	       			"left" : ( $(this).width() / 2 - options.width / 2 ) * -1,
	       			"top" : ( $(this).height() / 2 - options.height / 2 ) * -1
	   			});
			});
		
		});
  	};
})(jQuery);
