$(document).ready(function(){

	$(".portf_img img").css("visibility", "hidden");
    
	$(".portf_img img").each(function(e) {
        if (this.complete) {
            calc($(this));
        } else {
            $(this).bind("load", function(e) {
                var el=$(e.target);
                calc(el);
                el.unbind("load");
            });
        }
    });
    $(".prod_img img").each(function(e) {
		if (this.complete) {
			calc2($(this));
		} else {
			$(this).bind("load", function(e) {
				var el=$(e.target);
				calc(el);
				el.unbind("load");
			});
		}
	});
});

function calc(img) {
    var width = img.width(), new_w;
    var height = img.height(), new_h;
    var top;
    
    if ((width > 108) || (height > 108)){
        if (width > height) {
            new_w = 108;
            new_h = 108/width*height;
            top = (108 - new_h)/2;
        }
        else {
            new_h = 108;
            new_w = 108/height*width;
            top = 0;
        }
        
        img.css({"width": new_w, "height": new_h, "margin-top": top}).css("visibility", "visible");
    }
    else img.css({"margin-top": (108-height)/2});
}
function calc2(img) {
	var width = img.width(), new_w;
	var height = img.height(), new_h;
	var top;
	
	if ((width > 98) || (height > 108)){
		if (width > height) {
			new_w = 98;
			new_h = 98/width*height;
			top = (98 - new_h)/2;
		}
		else {
			new_h = 98;
			new_w = 98/height*width;
			top = 0;
		}
		
		img.css({"width": new_w, "height": new_h, "margin-top": top}).css("visibility", "visible");
	}
	else img.css({"margin-top": (98-height)/2});
}


