
photoClass = function(maxWidth, maxHeight){
	if (maxWidth != undefined){
		this.maxWidth = maxWidth;
	}
	
	if (maxHeight != undefined){
		this.maxHeight = maxHeight;
	}
	
	this.imagesArr = new Array();
	
	this.countSize = function (width,height, mw, mh){
		if (height>mh){
			ratio = mh/height;
			width = parseInt(width*ratio);
			height = parseInt(height*ratio);
		}
	
		if (width>mw){
			ratio = mw/width;
			width = parseInt(width*ratio);
			height = parseInt(height*ratio);
		}
	
		var left = parseInt((mw - width) /2);
		var top = parseInt((mh - height) /2);
	
		return {h:height,w:width,t:top,l:left,b:(mh-height-top)};
	}
	
	
	this.renderPic = function(){
		var w1 = this.width;
		var h1 = this.height;
		size = this.manager.countSize(w1, h1, this.mw, this.mh);
		
		this.width = size.w;
		this.height = size.h;
		this.style.border = "0";
		
		document.getElementById(this.imgId).style.width = size.w+'px';
		document.getElementById(this.imgId).style.height = size.h+'px';
		document.getElementById(this.imgId).style.paddingLeft = size.l+"px";
		document.getElementById(this.imgId).style.paddingRight = size.l+"px";
		document.getElementById(this.imgId).style.paddingTop = size.t+"px";
		document.getElementById(this.imgId).style.paddingBottom = size.b+"px";
		document.getElementById(this.imgId).appendChild(this);
		return this.manager.render(this.next);
	}
	
	this.showPhoto = function (src, imgId, w, h){
		this.imagesArr.push({img:src, placeId: imgId, w:w, h:h});
	}
	
	this.nextPhoto = function(){
		if (this.er == 1){
			return this.manager.render(this.next);
		} else {
			this.onload = this.renderPic;
			this.src = this.src.toString().replace(/mini_/g, '');
			this.er = 1;
		}
	}

	this.render = function(next){
		if (next == undefined){
			next = 0;
		}
		
		if (next !== false){
			img =  this.imagesArr[next];
			next++;
			this.imagesArr[next-1] = null;
			
			if (next >= this.imagesArr.lenght){
				next = false;
			}
			try{
				image = new Image();
				image.manager = this;
				image.imgId = img.placeId;
				image.next = next;
				image.onload = this.renderPic;
				image.mw = img.w;
				image.mh = img.h;
				//img.img = img.img.toString().replace(/http:\/\/admin.jes.com.pl/g, 'https://admin.jes.com.pl:8443');

				image.src = img.img;
				image.onerror = this.nextPhoto;
				image.er = 0;
				return true;
			} catch(e){
				return false;
			}
		}
	}
}

superPhMan = new photoClass();

