;(function(jQuery) {

	var defaults = {
	  minMarginLeftRight: 0,
	  resizeTo: 0, //0 = fullscreen both axes, 1 = resize horizontal, 2 = resize vertical
	  setHeightTo: 'img',
	  maxHeight: 0,
	  maxWidth: 0
	};
	
	//extend the fn for the methods
	jQuery.fn.resizer= function(settings) {
		
		jQuery.extend(this,{
						
			initialize: function() {
				var sizer = this;
				
				jQuery(window).bind('resize', function() {
					sizer.resize();
				});
				jQuery(this).css({'margin-left': this.minMarginLeftRight, 'margin-right': this.minMarginLeftRight});
				
				jQuery(this).find('img').load(function() {
					if (jQuery(this).hasClass('resizable')) {
						sizer.resize();
					};
				});


				return this;
			},

			resize: function() {
				//this.$backimg = jQuery(this).children('img');
				var sizer = this;
				
				var mar = 2*this.minMarginLeftRight;
				
				var winWidth	= jQuery(window).width(), 
				winHeight	= jQuery(window).height(), 
				ratioWin	= winHeight / winWidth;
				var maxW = 0; var maxH = 0;
				var pos = jQuery(sizer).offset();
				
				jQuery(this).find('img.resizable').each(function(){
					$img = new Image();
					$img.src = jQuery(this).attr('src');
						
					var eWidth	= $img.width,
					eHeight	= $img.height,
					ratioEl	= eHeight / eWidth,
					newWidth, newHeight, newLeft, newTop; 
					
					if(ratioWin > ratioEl){
						newHeight	= winHeight;
						newWidth	= (winHeight / ratioEl);
					} else{
						newHeight	= winWidth * ratioEl;
						newWidth	= winWidth;
					}
					//check max size of the image
					if (sizer.maxHeight && newHeight > sizer.maxHeight) newHeight=sizer.maxHeight;
					if (sizer.maxWidth && newWidth > sizer.maxWidth) newWidth=sizer.maxWidth;
					
					var newRatio=newHeight/newWidth;
					
					if ((newHeight+pos.top) > winHeight) {
						newHeight=winHeight-pos.top;
						newWidth=newHeight/newRatio;
					}
					
					//check plus minmargin
					if (newWidth+mar>winWidth) {
						newWidth=winWidth-(mar);
						newHeight=newWidth*newRatio;
					}
					
					newLeft = (winWidth - newWidth) / 2;
					newTop = (winHeight - newHeight) / 2; 
					//set the returned values and show the image
					jQuery(this).css({
						width	: newWidth + 'px',
						height	: newHeight + 'px'
					});
					
					maxW=(maxW<newWidth)?newWidth:maxW;
					maxH=(maxH<newHeight)?newHeight:maxH;

				});
				var newMargin = mar/2;
				
				if (maxW+mar < winWidth) newMargin = (winWidth-maxW)/2;
				
				jQuery(this).css({
					width: maxW+'px',
					height: maxH+'px',
					marginLeft : newMargin + 'px',
					marginRight : newMargin + 'px'
				});
				
				//var slidetop=maxH/2-(jQuery('.slide-prev').height())+pos.top+'px';
				//jQuery(this).find('.slide-prev').css('top', slidetop);
				//jQuery(this).find('.slide-next').css({'top': slidetop, 'left': maxW+'px'});
				
				if (this.onResize) this.onResize();
				
				return this;
			}
			
		});
		
		jQuery.extend(this, defaults, settings);
		
		this.initialize();
		
		return this;
	};
})(jQuery);
