/* ImageResize
 * @author take
 */

ImageResize = function(classname) {

	$('img.'+classname).each(function() {
		var img = $(this);
		//img.css('visibility','hidden');
		var w = img.width();
		var h = img.height();
		if(!img.parent().hasClass('imgOver')) {
			img.wrap($(document.createElement('div')).addClass('imgOver').width(w).height(h).css('position', 'rerative').css('text-align','center'));
		} else {
			w = img.parent().width();
			h = img.parent().height();
		}
		//img.css('max-width',w).css('max-height',h).width('auto').height('auto');
		img.css('max-width',w).css('max-height',h).width('auto').height('auto').css('margin-left','-1px');
		if(typeof document.body.style.maxWidth=='undefined') {
			var preimg = new Image();
			
			preimg.onload = function() {
				var wp = preimg.width / w;
				var hp = preimg.height / h;
				if( wp > 1 && (wp > hp || wp == hp)) {
					img.width(w+'px');
					img.css('margin-top',(h-preimg.height)/2+'px');
				}else if(hp > 1 && hp > wp) {
					img.height(h+'px');
				}
			}
			preimg.src = img.attr('src');
		}else{
			setTimeout(function(){ 
				img.css('margin-top',((h-img[0].offsetHeight)/2)+'px');
				//img.css('visibility','visible');
			},500);
		}
	});
};

