﻿function processImages() {
	$("ul.blk-ni").find("li").each(function(idx, value) {
		var _this = $(this);
		var p_height=_this.find("p.artThumb").css("height");
		var int_p_height=p_height.substring(0,p_height.length-2);
		
		var p_width=_this.find("p.artThumb").css("width");
		var int_p_width=p_width.substring(0,p_width.length-2);
		
		var int_img_height=_this.find("p.artThumb img").height();
		var int_img_width=_this.find("p.artThumb img").width();
		
		if((int_img_height>int_p_height) || (int_img_width>int_p_width)){
			//tutaj wchodzi tylko IE
			if (int_img_height>int_p_height){
				var nowa_szerokosc = Math.round((int_p_height/int_img_height)*int_p_width);
				_this.find("p.artThumb img").css("height",int_p_height+"px");
				_this.find("p.artThumb img").css("width",nowa_szerokosc+"px");
				int_img_height=_this.find("p.artThumb img").height();
				int_img_width=_this.find("p.artThumb img").width();
			}
			else if (int_img_width>int_p_width){
				var nowa_wysokosc = Math.round((int_p_width/int_img_width)*int_p_height);
				_this.find("p.artThumb img").css("width",int_p_width+"px");
				_this.find("p.artThumb img").css("height",nowa_wysokosc+"px");
				int_img_height=_this.find("p.artThumb img").height();
				int_img_width=_this.find("p.artThumb img").width();
			}
		}
		
		var padding_top=Math.round((int_p_height-int_img_height)/2);
		_this.children("p").children("img").css("padding-top",padding_top+"px");
	});
};

$(document).ready(function () {
	var loadedImages = 0;
	var img = $("ul.blk-ni p.artThumb img");
	var imgCount = img.length;
	var ImageLoaders = [];
	img.each(function(idx, value) {
		ImageLoaders[idx] = new Image;
		ImageLoaders[idx].onload = function(){
			loadedImages++;
			if(loadedImages >= imgCount) {
				processImages();
			}
		}
		ImageLoaders[idx].src = $(value).attr("src");
	});
});



