// RESIZE TO FILL - SIDEBAR HEADER IMAGES
//
// This script resizes and positions sidebar header images to fit their container proportionally.

$(document).ready(function() {
	
	var fillImgContainer_sbh = "#sb-headerimg-box";
	
	$(fillImgContainer_sbh + ' img').hide();
	
	$(window).load(function() {
		
		$(fillImgContainer_sbh + ' img').each(function(index) {
				
			var imgW = $(this).width();
			var imgH = $(this).height();
			
			var containerW = $(fillImgContainer_sbh).width();
			var containerH = $(fillImgContainer_sbh).height();
						
			var imgA = (imgW/imgH);
			var containerA = (containerW/containerH);
					
			$(this).css('position', 'relative');
			
			if ((imgW*imgH)>(containerW*containerH)) {
			
				if( imgA < containerA ) {
					$(this).css('min-width', containerW + 'px');
					$(this).css('max-width', containerW + 'px');
					var NewImgH = $(this).height();
					var CtrH = Math.round(((NewImgH-containerH)/2)*(-1));
					$(this).css('top', CtrH + 'px');
					$(this).show();
					} //if(imgW < imgH)
				
				if( imgA > containerA ) {
					$(this).css('min-height', containerH + 'px');
					$(this).css('max-height', containerH + 'px');
					var NewImgW = $(this).width();
					var CtrW = Math.round(((NewImgW-containerW)/2)*(-1));
					$(this).css('left', CtrW + 'px');
					$(this).show();
					}//if(imgW > imgH)
					
				if( imgA == containerA ) {
					$(this).css('min-width', containerW + 'px');
					$(this).css('max-width', containerW + 'px');
					var NewImgH = $(this).height();
					var CtrH = Math.round(((NewImgH-containerH)/2)*(-1));
					$(this).css('top', CtrH + 'px');
					$(this).show();
					} //if(imgW == imgH)
				
				} else { 
					var CtrW = ((containerW-imgW)/2);
					var CtrH = ((containerH-imgH)/2);
					$(this).css('left', CtrW + 'px');
					$(this).css('top', CtrH + 'px');
					$(this).show();
					}
			}); 
			
	}); //(window).load
	
}); //(document).ready
