/* jquery.imagefit 
 *
 * Version 0.2 by Oliver Boermans <http://www.ollicle.com/eg/jquery/imagefit/>
 *
 * Extends jQuery <http://jquery.com>
 *
 */
(function($) {
	$.fn.imagefit = function(options) {
		var fit = {
			all : function(imgs){
				imgs.each(function(){
					fit.one(this);
					})
				},
			one : function(img){
				$(img)
					.width('100%').each(function()
					{

						imageHeight = (Math.round(
							$(this).attr('startheight')*($(this).width()/$(this).attr('startwidth')))
						);
						
//						console.log('imageHeight: ' + imageHeight);
						

						navHeight = $('#nav2').height();
						navHeight = 100;

						navHeight = $('#brand').height() + 35;

						footerHeight = $('#footer').height();
//						maxHeight = $(window).height() - navHeight - footerHeight - 10 - 10 - 5 - 5;
						maxHeight = $(window).height() - navHeight - footerHeight - 10;



						if ( imageHeight > maxHeight ) {
						
							$('#hero img').each(function() {

								$(this).height(maxHeight);
							
								$(this).width(Math.round(
									$(this).attr('startwidth')*($(this).height()/$(this).attr('startheight')))
								);
								

//								$(this).css({'margin-left': ($('#hero').outerWidth() - $(this).width())/2 });
								$(this).css({'margin-left': ($('#hero').width() - $(this).width())/2 });

								
							});
						

						} else {



						$(this).height(Math.round(
							$(this).attr('startheight')*($(this).width()/$(this).attr('startwidth')))
						);
						
						$(this).css({'margin-left': 0});
						
						
						}
						
//						alert (imageHeight);
						
						
//						alert (maxHeight);

						
//						console.log( 'hero: ' + $('#hero').width() );
						
						
					})
				},
			launch : function(img){
				$(img)
					.width('100%').each(function()
					{

						imageHeight = (Math.round(
							$(this).attr('startheight')*($(this).width()/$(this).attr('startwidth')))
						);
						
						navHeight = $('#nav2').height();
						navHeight = 100;

						navHeight = $('#brand').height() + 35;


						footerHeight = $('#footer').height();
//						maxHeight = $(window).height() - navHeight - footerHeight - 10 - 10 - 5 - 5;
						maxHeight = $(window).height() - navHeight - footerHeight - 10;

						
						if ( imageHeight > maxHeight ) {

							$(this).height(maxHeight);
						
							$(this).width(Math.round(
								$(this).attr('startwidth')*($(this).height()/$(this).attr('startheight')))
							);
							
//							$(this).css({'margin-left': ($('#hero').outerWidth() - $(this).width())/2 }); 
							$(this).css({'margin-left': ($('#hero').width() - $(this).width())/2 }); 

						
						} else {
						
							$(this).height(Math.round(
								$(this).attr('startheight')*($(this).width()/$(this).attr('startwidth')))
							);
						
						}
						
						
					})
				}
		};
		
		this.each(function(){
				var container = this;
				
				// store list of contained images (excluding those in tables)
				var imgs = $('img', container).not($("table img"));
				
//				alert(imgs.length);
				
				var maxImgHeight = 0;
				
				// store initial dimensions on each image 
				imgs.each( function () {
					testVar = $(this).height();
					if (testVar > maxImgHeight) {
					maxImgHeight = testVar;
					}
				});
				
//				alert (maxImgHeight);

				var maxImgWidth = 0;
				
				// store initial dimensions on each image 
				imgs.each( function () {
					testVar = $(this).width();
					if (testVar > maxImgWidth) {
					maxImgWidth = testVar;
					}
				});
				
//				alert (maxImgWidth);

				
				
				imgs.each(function(){
//					console.log('img');
//					console.log('img width: ' + $(this).width());
					$(this).attr('startwidth', $(this).width())
						.attr('startheight', $(this).height());
						//.css('max-width', $(this).attr('startwidth')+"px");
				
//					console.log('startwidth: ' + $(this).attr('startwidth'));
					//console.log($(this).css('max-width'));
					fit.launch(this);
					
				});
				
				
				// Re-adjust when window width is changed
				$(window).bind('resize', function(){
					fit.all(imgs);

//						navHeight = $('#nav').height();
//						footerHeight = $('#footer').height();
//						maxHeight = $(window).height() - navHeight - footerHeight;
						
//						alert (maxHeight);

//						console.log(maxHeight);
					
//					$('#hero').height($('#hero > img').css('height'));

					$('#hero').height($('#hero img').css('height'));

//					$('#hero').height(maxImgHeight);

					
				});
			});
		return this;
	};
})(jQuery);