/*  (c) Evan Roberts - 7/10/2009
	IE 6.0 Browser Support (c) Regan Swallow 9/22/2009
	www.GoWFB.com - Wholesale Furniture Brokers
	Everyone is free to use/mdoify this but please keep the copyright statement!
*/

function css_emulateMaxWidthHeight() {
    $('img').each(function() { 
		if($(this).css) {
			if( $(this).css('max-width') != 'none' || $(this).css('max-height') != 'none' ) {
				max_width = $(this).css('max-width').replace(/[^0-9]/g, '');
				max_height = $(this).css('max-height').replace(/[^0-9]/g, '');
				if( $(this).width() > max_width || $(this).height() > max_height ) {
					if( (max_width / $(this).width()) < (max_height / $(this).height()) ) { 
						$(this).css('height', Math.round($(this).height() * (max_width / $(this).width())));
						$(this).css('width', max_width);  
					} else {
						$(this).css('width', Math.round($(this).width() * (max_height / $(this).height())));
						$(this).css('height', max_height);
					}
				}
			}
		}
    });
}

function emulateMaxWidthHeight() {
    $('img').each(function() { 
		var style_arr = $(this).attr('style').split(';');
		var max_width = 0;
		var max_height = 0;
		for(x = 0; x < style_arr.length; x++){
			var style_elm = style_arr[x].split(':');
			if (style_elm[0].search(/max-width/i) > -1){
				max_width = parseInt(style_elm[1].replace(/[^0-9]/g, ''));
			} else if (style_elm[0].search(/max-height/i) > -1){
				max_height = parseInt(style_elm[1].replace(/[^0-9]/g, ''));
			}
		}
		if (max_width != '0' || max_height != '0'){
			if( $(this).width() > max_width || $(this).height() > max_height ) {
				if( (max_width / $(this).width()) < (max_height / $(this).height()) ) { 
					$(this).height(Math.round($(this).height() * (max_width / $(this).width())));
					$(this).width(max_width);                        
				} else {
					$(this).width(Math.round($(this).width() * (max_height / $(this).height())));
					$(this).height(max_height);
				}
			}
		}
    });
}

function resize_image(){
	jQuery.each(jQuery.browser, function(i, val) {
		if (val == true){
			if (i == 'msie' && jQuery.browser.version == '6.0'){
				emulateMaxWidthHeight();
			} else {
				css_emulateMaxWidthHeight();
			}
		}
    });
}

$(document).ready(function() {
	resize_image()
});

$(window).load(function() {
	resize_image()
});