var CreateGallery = function(parameters) {
	var self = this;
	this.galleryImages = [];
	this.galleryImagesSrcs = [];
	this.galleryImagesNum = 0;
	this.counter;
	this.galleryTitle = parameters.galleryTitle != undefined ? parameters.galleryTitle : 'Gallery image';
	this.maxMobileWidth = parameters.maxMobileWidth != undefined ? parameters.maxMobileWidth : 768;
	this.numMobileImg = parameters.numMobileImg != undefined ? parameters.numMobileImg : 2;
	this.maxTabletWidth = parameters.maxTabletWidth != undefined ? parameters.maxTabletWidth : 1024;
	this.numTabletImg = parameters.numTabletImg != undefined ? parameters.numTabletImg : 3;
	this.addModalGallery = function(gallerySelf = self){
		var $div = $(document.createElement('div')).attr({
			'id': 'modal-gallery'
		}).append($(document.createElement('div')).attr({
				'class': 'header'
			}).append($(document.createElement('button')).attr({
					'class': 'close-button',
					'type': 'button'
				}).html('×')
			).append($(document.createElement('h2')).text(gallerySelf.galleryTitle))
		).append($(document.createElement('div')).attr({
				'class': 'text-center'
			}).append($(document.createElement('img')).attr({
					'id': 'gallery-img'
				})
			).append($(document.createElement('button')).attr({
					'class': 'prev-button',
					'type': 'button'
				}).html('◄')
			).append($(document.createElement('button')).attr({
					'class': 'next-button',
					'type': 'button'
				}).html('►')
			)
		);
		parameters.element.after($div);
	};
	$(document).on('click', 'button.prev-button', {self: self}, function() {
		var $currImg = self.counter >= 1 ? self.galleryImages[--self.counter] : self.galleryImages[self.counter];
		var $currHash = self.galleryImagesSrcs[self.counter];
		var $src = $currImg.src;
		window.location.hash = $currHash;
		$('img#gallery-img').attr('src', $src);
		$('div#modal-gallery h2').text(self.galleryTitle + ' ' + (self.counter + 1));
	});
	$(document).on('click', 'button.next-button', {self: self}, function() {
		var $currImg = self.counter < (self.galleryImagesNum - 1) ? self.galleryImages[++self.counter] : self.galleryImages[self.counter];
		var $currHash = self.galleryImagesSrcs[self.counter];
		var $src = $currImg.src;
		window.location.hash = $currHash;
		$('img#gallery-img').attr('src', $src);
		$('div#modal-gallery h2').text(self.galleryTitle + ' ' + (self.counter + 1));
	});
	$(document).on('click', 'div#modal-gallery button.close-button', function() {
		$('#modal-gallery').css('position', 'relative');
		$('#modal-gallery').hide();
		$('body').css('overflow', 'visible');
	});
	parameters.element.find('a').on('click', {self: self}, function(event) {
		event.preventDefault();
		$('#modal-gallery').css('position', 'fixed');
		$('#modal-gallery').show();
		$('body').css('overflow', 'hidden');
		var $currHash = this.hash.substr(1);
		self.counter = self.galleryImagesSrcs.indexOf($currHash); 
		var $src = $currHash;
		window.location.hash = $currHash;
		$('img#gallery-img').attr('src', $src);
		$('div#modal-gallery h2').text(self.galleryTitle + ' ' + (self.counter + 1));
	});
	this.sizeGallery = function(gallerySelf = self) {
		var $modalGallery = $(document).find("div#modal-gallery");
		if($modalGallery.length <= 0) {
			this.addModalGallery();
		}
		var $windowWidth = $(window).width();
		var $parentWidth = parameters.element.width();
		var $thumbnailHref = parameters.element.find('img:first').attr('src');
		if($windowWidth < gallerySelf.maxMobileWidth) {
			var percentMobile = Math.floor(100 / gallerySelf.numMobileImg); 
			var emMobile = 0;
			var pxMobile = 2;
//                        var emMobile = gallerySelf.numMobileImg * 0.4;
//                        var pxMobile = gallerySelf.numMobileImg * 2;
			parameters.element.find('img').css('width', 'calc('+percentMobile+'% - '+emMobile+'em - '+pxMobile+'px)');
		} else if($windowWidth < gallerySelf.maxTabletWidth) {
			var percentTablet = Math.floor(100 / gallerySelf.numTabletImg); 
			var emTablet = 0;
			var pxTablet = 2;
//                        var emTablet = gallerySelf.numMobileImg * 0.4;
//                        var pxTablet = gallerySelf.numMobileImg * 2;
			parameters.element.find('img').css('width', 'calc('+percentTablet+'% - '+emTablet+'em - '+pxTablet+'px)');
		} else {
			var $thumbImg = new Image();
			$thumbImg.src = $thumbnailHref;
			$thumbImg.onload = function() {
				var $thumbnailWidth = this.width;
				if($parentWidth > 0 && $thumbnailWidth > 0) {
					parameters.element.find('img').css('width', (Math.ceil($thumbnailWidth * 100 / $parentWidth))+'%');
				}
			};
		}
	};
	this.startGallery = function(gallerySelf = self) {
		parameters.element.find('a').each(function(index, el) {
			var $currHash = el.hash.substr(1);
			var $img = new Image();
			$img.src = $currHash;
			gallerySelf.galleryImages.push($img);
			gallerySelf.galleryImagesSrcs.push($currHash);
		});
		this.galleryImagesNum = this.galleryImages.length;
	};
	this.sizeGallery();
	this.startGallery();
};
var myGallery;
$(window).on('load', function() {
	myGallery = new CreateGallery({
		element: $('#div-gallery'),
		maxMobileWidth: 768,
		numMobileImg: 2,
		maxTabletWidth: 1024,
		numTabletImg: 3
	});
});
$(window).on('resize', function() {
	myGallery.sizeGallery();
});
#div-gallery {
	text-align: center;
}
#div-gallery img {
	margin-right: auto;
	margin-left: auto;
}
div#modal-gallery {
	top: 0;
	left: 0;
	width: 100%;
	max-width: none;
	height: 100vh;
	min-height: 100vh;
	margin-left: 0;
	border: 0;
	border-radius: 0;
	z-index: 1006;
	-webkit-backface-visibility: hidden;
	backface-visibility: hidden;
	display: none;
	background-color: #fefefe;
	position: relative;
	margin-right: auto;
	overflow-y: auto;
	padding: 0;
}
div#modal-gallery div.header {
	position: relative; 
}
div#modal-gallery div.header h2 {
	margin-left: 1rem; 
}
div#modal-gallery div.header button.close-button {
	position: absolute;
	top: calc(50% - 1em);
	right: 1rem;
}
div#modal-gallery img {
	display: block;
	max-width: 98%;
	max-height: calc(100vh - 5em);
	margin-right: auto;
	margin-left: auto;
}
div#modal-gallery div.text-center {
	position: relative;
}
button.close-button {
	display: inline-block;
	padding: 6px 12px;
	margin-bottom: 0;
	font-size: 20px;
	font-weight: 400;
	line-height: 1.42857143;
	text-align: center;
	white-space: nowrap;
	vertical-align: middle;
	-ms-touch-action: manipulation;
	touch-action: manipulation;
	cursor: pointer;
	-webkit-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	user-select: none;
	background-image: none;
	border: 1px solid transparent;
	border-radius: 4px;
	color: #333;
	background-color: #fff;
	border-color: #ccc;
}
button.close-button:hover, button.close-button:focus {
	background-color: #ddd;
}
button.next-button:hover, button.prev-button:hover, button.next-button:focus, button.prev-button:focus {
	color: #fff;
	text-decoration: none;
	filter: alpha(opacity=90);
	outline: 0;
	opacity: .9;
}
button.next-button, button.prev-button {
	position: absolute;
	top: 0;
	bottom: 0;
	left: 0;
	width: 15%;
	font-size: 20px;
	color: #fff;
	text-align: center;
	text-shadow: 0 1px 2px rgba(0,0,0,.6);
	background-color: rgba(0,0,0,0);
	filter: alpha(opacity=50);
	opacity: .5;
	border: none;
}
button.next-button {
	right: 0;
	left: auto;
	background-image: -webkit-linear-gradient(left,rgba(0,0,0,.0001) 0,rgba(0,0,0,.5) 100%);
	background-image: -o-linear-gradient(left,rgba(0,0,0,.0001) 0,rgba(0,0,0,.5) 100%);
	background-image: -webkit-gradient(linear,left top,right top,from(rgba(0,0,0,.0001)),to(rgba(0,0,0,.5)));
	background-image: linear-gradient(to right,rgba(0,0,0,.0001) 0,rgba(0,0,0,.5) 100%);
	filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#00000000', endColorstr='#80000000', GradientType=1);
	background-repeat: repeat-x;
}
button.prev-button {
	background-image: -webkit-linear-gradient(left,rgba(0,0,0,.5) 0,rgba(0,0,0,.0001) 100%);
	background-image: -o-linear-gradient(left,rgba(0,0,0,.5) 0,rgba(0,0,0,.0001) 100%);
	background-image: -webkit-gradient(linear,left top,right top,from(rgba(0,0,0,.5)),to(rgba(0,0,0,.0001)));
	background-image: linear-gradient(to right,rgba(0,0,0,.5) 0,rgba(0,0,0,.0001) 100%);
	filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#80000000', endColorstr='#00000000', GradientType=1);
	background-repeat: repeat-x;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="div-gallery">
	<a href="#http://placehold.it/1024x1024/ff0000/000000?text=Gallery+image+1"><img src="http://placehold.it/300x300/ff0000/000000?text=Gallery+image+1" alt=""></a>
	<a href="#http://placehold.it/1024x1024/ff4000/000000?text=Gallery+image+2"><img src="http://placehold.it/300x300/ff4000/000000?text=Gallery+image+2" alt=""></a>
	<a href="#http://placehold.it/1024x1024/ff8000/000000?text=Gallery+image+3"><img src="http://placehold.it/300x300/ff8000/000000?text=Gallery+image+3" alt=""></a>
	<a href="#http://placehold.it/1024x1024/ffbf00/000000?text=Gallery+image+4"><img src="http://placehold.it/300x300/ffbf00/000000?text=Gallery+image+4" alt=""></a>
	<a href="#http://placehold.it/1024x1024/ffff00/000000?text=Gallery+image+5"><img src="http://placehold.it/300x300/ffff00/000000?text=Gallery+image+5" alt=""></a>
	<a href="#http://placehold.it/1024x1024/bfff00/000000?text=Gallery+image+6"><img src="http://placehold.it/300x300/bfff00/000000?text=Gallery+image+6" alt=""></a>
	<a href="#http://placehold.it/1024x1024/80ff00/000000?text=Gallery+image+7"><img src="http://placehold.it/300x300/80ff00/000000?text=Gallery+image+7" alt=""></a>
	<a href="#http://placehold.it/1024x1024/40ff00/000000?text=Gallery+image+8"><img src="http://placehold.it/300x300/40ff00/000000?text=Gallery+image+8" alt=""></a>
	<a href="#http://placehold.it/1024x1024/00ff00/000000?text=Gallery+image+9"><img src="http://placehold.it/300x300/00ff00/000000?text=Gallery+image+9" alt=""></a>
	<a href="#http://placehold.it/1024x1024/00ff40/000000?text=Gallery+image+10"><img src="http://placehold.it/300x300/00ff40/000000?text=Gallery+image+10" alt=""></a>
	<a href="#http://placehold.it/1024x1024/00ff80/000000?text=Gallery+image+11"><img src="http://placehold.it/300x300/00ff80/000000?text=Gallery+image+11" alt=""></a>
	<a href="#http://placehold.it/1024x1024/00ffbf/000000?text=Gallery+image+12"><img src="http://placehold.it/300x300/00ffbf/000000?text=Gallery+image+12" alt=""></a>
	<a href="#http://placehold.it/1024x1024/00ffff/000000?text=Gallery+image+13"><img src="http://placehold.it/300x300/00ffff/000000?text=Gallery+image+13" alt=""></a>
	<a href="#http://placehold.it/1024x1024/00bfff/000000?text=Gallery+image+14"><img src="http://placehold.it/300x300/00bfff/000000?text=Gallery+image+14" alt=""></a>
	<a href="#http://placehold.it/1024x1024/0080ff/000000?text=Gallery+image+15"><img src="http://placehold.it/300x300/0080ff/000000?text=Gallery+image+15" alt=""></a>
	<a href="#http://placehold.it/1024x1024/0040ff/000000?text=Gallery+image+16"><img src="http://placehold.it/300x300/0040ff/000000?text=Gallery+image+16" alt=""></a>
</div>