<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">(function($) {

	$.fn.mtrGallery = function(options) {

		//Set common vars
		var bodyWidth 			= $('body').width();
		var containerHeight 	= 0; 
		var largeImageHeight 	= 0;
		var mainThis 			= this;
		var mainSelectedImage 	= 0;
		var currentlyLinkedImgs	= new Array();
		var currentlyLinkedWidth = 0;
		var currentSpaceLeft 	= 0;
		var zoomedImage 		=		null;
		var playingVideo 		= "";
		var zoomedImageProp		= [];		
		var currentWindowWidth 	= $(window).width();
		var currentWindowHeight = $(window).height();
		var currentMouseX		= 0;
		var currentMouseY		= 0;
		var phaseOneZoom     	= 0;
		var phaseTwoZoom 		= 0;
		var origPageTitle		= "";
		var windowMarginBottomMutable 	= 270;
		var windowMarginBottom 	= 270;
		var imageFitFix			= 0;

		//Set default settings
		var settings = {  
			imageMarginRight 	: 5,
			minImageHeight		: 100,
			thumbHeight			: 50,
			maxWidth 			: 10000,
			thumbContainerWidth : 800,
			minWidth			: 960,
			catHeader			: ""
		}
		
		// Extend the options so they work with the plugin
		if(options) {
			$.extend(settings, options);
		}
		
		//Track mouse position
		$( window ).mousemove(function( event ) {
			currentMouseX = event.pageX;
			currentMouseY = event.pageY;

			if(phaseTwoZoom == 1){
				panZoomedImage(currentMouseX,currentMouseY);
			}
		});


		//First init
		mainThis.find("img").css({"opacity":"0", "cursor":"pointer"});
		
		//mainThis.parent().css('min-width', settings.minWidth + 'px');
		
		
		mainThis.parent().append("&lt;img id='navleft' src='/lib/images/nav_left_90.svg' style='position:absolute;left:25px;cursor:pointer;opacity:0.2;' /&gt;");
		mainThis.parent().append("&lt;img id='navright' src='/lib/images/nav_right_90.svg' style='position:absolute;right:25px;cursor:pointer;' /&gt;");

		if(mainThis.find("img").length == 1){
			$("#navright").css('opacity','0.2');
		}

		$(".galleryCaptionClose").click(function(){
			$('.galleryCaption').fadeOut('fast',function(){
				$('.galleryCaptionContent').empty();	
			});
			
		});

		$('#navleft').click(function(){
			
			if((mainSelectedImage + 1) != 1){
		        	openImageOnPosition(mainSelectedImage - 1);
		        	
		       }
		});

		$('#navright').click(function(){
			
			if((mainSelectedImage + 1) != $(".mainImage").length){
		        	openImageOnPosition(mainSelectedImage + 1);
		        }
		});

		function initSizes(){
			if($(".catsubheader").height() &gt; 0){
				windowMarginBottomMutable = windowMarginBottom + $(".catsubheader").height();
			}

			currentWindowWidth = $(window).width();
			currentWindowHeight = $(window).height();

			mainThis.parent().height($(window).height());
			mainThis.height($(window).height() - windowMarginBottomMutable);

			mainThis.find("img").each(function(){
				$(this).css("margin-right",settings.imageMarginRight);
			});

			containerHeight = mainThis.height();
			if(containerHeight &lt; settings.minImageHeight){
				containerHeight = settings.minImageHeight;
			}
			
			//Set initial image height
			resetImageHeight();

			//Set main element width
			mainThis.width(calculateMainWidth());
			
			detectOnScreen(mainSelectedImage);

			$('#navleft').css('top',($(window).height() / 2) - ($('#navleft').height() / 2) + "px");
			$('#navright').css('top',($(window).height() / 2) - ($('#navleft').height() / 2) + "px");

			$('#navleftzoom').css('top',($(window).height() / 2) - ($('#navleft').height() / 2) + "px");
			$('#navrightzoom').css('top',($(window).height() / 2) - ($('#navleft').height() / 2) + "px");			
		}

		//Position images
		this.find("img").each(function(){
			
			$(this).css({
				'display'	: 'block'	,
				'float'		: 'left' ,
				'max-width' : settings.maxWidth+"px"
			});

			$(this).addClass("mainImage");
		});
		

		//ZOOM
		$(".mainImage").click(function(){
			
			if($(this).hasClass("mtrVisible")){
				if( $(this).hasClass("video")){
					
					
					if ( playingVideo != '' ) {
						
						$(playingVideo).stop();
						removeVideo();
						
						
						var myThis = this;
						
						setTimeout(function() {
							playVideo($(myThis), 1);
						}, 500);
						
					} else {
						playVideo($(this), 1);
					}
					
					
				}else{
					if ( playingVideo != '' ) {
						var clickThis = $(this);
						
						removeVideo();
						setTimeout(function(){
							zoomImagePhaseOne(clickThis);		
						},400);
						
					}else{
						zoomImagePhaseOne($(this));	
					}
				}
				
			}
		}); // END ZOOM
		
		
		origPageTitle = $(settings.catHeader).html();
		
		createThumbs();
		initSizes();
		fixImageAspectRatio();
		
		
		function playVideo(videoClicked, autoplay){
			
			zoomedImage = videoClicked;
			
			videoClicked.css('visibility', 'hidden');
			
			var workingContainer = mainThis.parent();
			workingContainer.append("&lt;div id='_mtrGalleryVideoControlsContainer' style='display:none;width:"+settings.thumbContainerWidth+"px;margin:auto;height:"+(settings.thumbHeight + 20)+"px;margin-top:20px;text-align:center;overflow:auto;'&gt;&lt;/div&gt;");
			
			if ( autoplay == 1 ) {
				$("#_mtrGalleryThumbContainer").fadeOut('fast',function(){
					$("#_mtrGalleryVideoControlsContainer").fadeIn("fast");
				});
			} else {
				$("#_mtrGalleryThumbContainer").hide();
				$("#_mtrGalleryVideoControlsContainer").show();
			}
			
			var video = document.createElement('video');
			video.setAttribute("style", "background-color:#f8f8f8;position:absolute;z-index:10000;left:"+videoClicked.offset().left+"px;top:"+videoClicked.offset().top+"px;width:"+videoClicked.width()+"px;height:"+videoClicked.height()+"px;");
			video.setAttribute("width", videoClicked.width());
			video.setAttribute("height", videoClicked.height());
			video.setAttribute("class", "_mtrVideoPlayer");
			video.setAttribute("poster", videoClicked.attr('src'));

			var sourceMP4 = document.createElement("source"); 
			sourceMP4.type = "video/mp4";
			sourceMP4.src = videoClicked.data('mp4');

			var sourceOGG = document.createElement("source"); 
			sourceOGG.type = "video/ogg";
			sourceOGG.src = videoClicked.data('ogg')

			video.appendChild(sourceMP4);
			video.appendChild(sourceOGG);
			document.body.appendChild(video);
		
			$("#_mtrGalleryVideoControlsContainer").append("&lt;div style='width:100%;height:1px;background-color:#828282' class='_mtrSeekBar'&gt;&lt;div class='_mtrSeekBarProgress' style='width:0px;height:1px;background-color:#cccccc;'&gt;&lt;/div&gt;&lt;/div&gt;");
			
			$("#_mtrGalleryVideoControlsContainer").append("&lt;div class='_mtrGalleryControllers' style='width: 90px;margin:auto;margin-top:12px;'&gt;&lt;/div&gt;");
			if ( autoplay == 1 ) {
				$("._mtrGalleryControllers").append("&lt;img id='_mtrPlay' src='/lib/images/pause.png' style='display:block;margin:0px;padding:0px;float:left;margin-right:16px;cursor:pointer;' /&gt;");
			} else {
				$("._mtrGalleryControllers").append("&lt;img id='_mtrPlay' src='/lib/images/play.png' style='display:block;margin:0px;padding:0px;float:left;margin-right:16px;cursor:pointer;' /&gt;");
			}
			
			$("._mtrGalleryControllers").append("&lt;img id='_mtrStop' src='/lib/images/stop.png' style='display:block;margin:0px;padding:0px;float:left;cursor:pointer;margin-right:16px;' /&gt;");
			$("._mtrGalleryControllers").append("&lt;img id='_mtrFullscreen' src='/lib/images/full-screen.png' style='display:block;margin:0px;padding:0px;float:left;cursor:pointer;margin-top:-7px;' /&gt;");
			
			if(zoomedImage.data('caption') == "YES"){
				$("#_mtrGalleryVideoControlsContainer").append("&lt;table style='width:100%;'&gt;&lt;tr&gt;&lt;td style='width:"+((settings.thumbContainerWidth / 2) - 45)+"px;text-align:center;'&gt;&amp;nbsp;&lt;/td&gt;&lt;td style='width:90px;text-align:center;'&gt;&lt;span id='_mtrStopBack' style='font-size:12px;display:block;margin:0px;padding:0px;cursor:pointer;'&gt;back to series&lt;/span&gt;&lt;/td&gt;&lt;td style='text-align:right;width:"+((settings.thumbContainerWidth / 2) - 45)+"px;'&gt;&lt;span id='_mtrCaption' style='font-size:12px;display:block;margin:0px;padding:0px;cursor:pointer;'&gt;caption&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;");
			}else{
				$("._mtrGalleryControllers").append("&lt;Br /&gt;&lt;span id='_mtrStopBack' style='font-size:12px;display:block;margin:0px;padding:0px;cursor:pointer;margin-top:10px;margin-left:-5px;'&gt;back to series&lt;/span&gt;");	
			}

			playingVideo = video;
			video.addEventListener('timeupdate', updateVideoProgressBar, false);
			
			if ( autoplay == 1 ) {
				video.play();
			}

			$('#_mtrCaption').click(function(){
				displayCaption(zoomedImage.data('uid'));

			});

			$("#_mtrPlay").click(function(){
				console.log(video.paused)
				if(video.paused){
				$(this).attr("src","/lib/images/pause.png");
					video.play();
				}else{
					$(this).attr("src","/lib/images/play.png");
					video.pause();
				}
			});

			$("#_mtrStopBack").click(function(){
				removeVideo();
			});
			
			$("#_mtrStop").click(function(){
				removeVideo();
			});

			$("#_mtrFullscreen").click(function(){
				if (video.requestFullscreen) {
					video.requestFullscreen();
				} else if (video.mozRequestFullScreen) {
					video.mozRequestFullScreen();
				} else{
					video.webkitEnterFullscreen();
				}
			});
		}

		function updateVideoProgressBar(){
			var progressBar = ""
			var percentage = Math.floor((100 / playingVideo.duration) * playingVideo.currentTime);
			var newWidth = ($("._mtrSeekBar").width() / 100) * percentage;
			$("._mtrSeekBarProgress").width(newWidth);
		}

		function removeVideo(){
			$('.galleryCaption').fadeOut('fast');
			
			zoomedImage.css('visibility', 'visible');
			
			$("#_mtrGalleryVideoControlsContainer").fadeOut('fast',function(){
				$("#_mtrGalleryVideoControlsContainer").empty();
				$("#_mtrGalleryVideoControlsContainer").remove();


				playingVideo.remove();
				$("#_mtrGalleryThumbContainer").show();
				playingVideo = "";
			});
			
			$(settings.catHeader).html(origPageTitle);
			
		}

		function zoomImagePhaseOne(clickedImage){
			
			$('#_mtrGalleryThumbContainer').hide();
			
			phaseOneZoom = 1;
			zoomedImage = clickedImage;

			$("body").prepend("&lt;div class='_mtrGalleryZoom'&gt;&lt;/div&gt;");

			$("._mtrGalleryZoom").css({
				'width'	: $(window).width(),
				'height' : mainThis.height() + (settings.thumbHeight + 10),
				'position' : 'absolute',
				"background-color" : "#f8f8f8",
				'z-index' : '10000',
				'display' : "none",
				"top"	: mainThis.position().top
			});

			var imagePosLeft 	= clickedImage.offset().left;
			var imagePosTop  	= clickedImage.offset().top;
			var imageWidth   	= clickedImage.width();
			var imageHeight   	= clickedImage.height();

			clickedImage.css('opacity','0');
			$('body').prepend("&lt;img class='_zoomMainImage' src='"+clickedImage.attr('src')+"' style='position:absolute;left:"+imagePosLeft+"px;top:"+imagePosTop+"px;height:"+imageHeight+"px;z-index:10001;' /&gt;");

			$('body').prepend("&lt;img id='navleftzoom' src='/lib/images/nav_left_90.svg' style='position:absolute;left:25px;cursor:pointer;z-index:10001;' /&gt;");
			$('body').prepend("&lt;img id='navrightzoom' src='/lib/images/nav_right_90.svg' style='position:absolute;right:25px;cursor:pointer;z-index:10001;' /&gt;");

			$('#navleftzoom').css('top',($(window).height() / 2) - ($('#navleft').height() / 2) + "px");
			$('#navrightzoom').css('top',($(window).height() / 2) - ($('#navleft').height() / 2) + "px");

			$('#navrightzoom').click(function(){
				zoomedImageRight();
			});

			$('#navleftzoom').click(function(){
				zoomedImageLeft();
			});

			$("._mtrGalleryZoom").fadeIn(500);

			var newImageHeight  = $("._mtrGalleryZoom").height();
			var newImageTop 	= mainThis.position().top;
			var newImageWidth   = clickedImage.width() * ( newImageHeight / clickedImage.height() );
			var newImageLeft    = ($("._mtrGalleryZoom").width() - newImageWidth) / 2;

			if(newImageWidth &gt; ( $("._mtrGalleryZoom").width() - 130)){
				newImageWidth = ( $("._mtrGalleryZoom").width() - 130);
				var newImageTop 	= mainThis.position().top;
				var newImageLeft    = ($("._mtrGalleryZoom").width() - newImageWidth) / 2;
				var newImageHeight  = clickedImage.height() * ( newImageWidth / clickedImage.width() );
			}

			$("._zoomMainImage").animate(
			{
				top : newImageTop+"px",
				left : newImageLeft+"px",
				height: newImageHeight+"px",
				
			}, 500, function() 
			{
				phaseOneZoomAddControls();
				$(this).css('cursor', 'url(/lib/images/zoom_in.svg),auto');	
				$(this).click(function(){
					zoomImagePhaseTwo(clickedImage);
				});
			});
			
			$("._mtrGalleryZoom").css({
				'width'	: $(window).width(),
				'height' :newImageHeight,
				"top"	: mainThis.position().top
			});

			$(settings.catHeader).html(origPageTitle + '&lt;br /&gt;&lt;span style="margin-left:120px;font-size:15px;"&gt;' + clickedImage.data('pagetitle')+"&lt;/span&gt;");
		}

		function zoomedImageRight(){
			$(".galleryCaptionClose").click();

			$('#navleftzoom').css('opacity',"1");
			var nextImage = zoomedImage.next();
			var nextNextImage = nextImage.next();
			
			if(nextNextImage.attr("class") == undefined){
				$('#navrightzoom').css('opacity',"0.2");
			}

			if(nextImage.attr('src') == undefined){
				return;
			}
			
			console.log(nextImage.attr('src'));
			zoomRemoveControls();
			$("._zoomMainImage").animate(
			{
				left: -+$(this).width()
				
			}, 300, function() 
			{
				$('#navright').click();

				zoomedImage = nextImage;
				$(this).remove();

				var imagePosLeft 	= nextImage.offset().left;
				var imagePosTop  	= nextImage.offset().top;
				var imageWidth   	= nextImage.width();
				var imageHeight   	= nextImage.height();

				$('body').prepend("&lt;img class='_zoomMainImage' src='"+nextImage.attr('src')+"' style='position:absolute;left:"+imagePosLeft+"px;top:"+imagePosTop+"px;height:"+imageHeight+"px;z-index:10001;' /&gt;");
				$('._zoomMainImage').attr('src',(nextImage.data('mediumsource')));
				
				$(settings.catHeader).html(origPageTitle + '&lt;br /&gt;&lt;span style="margin-left:120px;font-size:15px;"&gt;' + nextImage.data('pagetitle')+"&lt;/span&gt;");

				resetPhaseOneZoomSizes();
				setTimeout(function(){
					phaseOneZoomAddControls();
				},100);
				

				$('._zoomMainImage').css('cursor', 'url(/lib/images/zoom_in.svg),auto');	
				$('._zoomMainImage').click(function(){
					zoomImagePhaseTwo(nextImage);
				});
				
			});
					
		}

		function zoomedImageLeft(){
			$(".galleryCaptionClose").click();
			$('#navrightzoom').css('opacity',"1");
			if(playingVideo){
				$(playingVideo).stop();
				removeVideo();
			}

			var nextImage = zoomedImage.prev();
			var nextNextImage = nextImage.prev();

			if(nextNextImage.attr("class") == undefined){
				$('#navleftzoom').css('opacity',"0.2");
			}

			if(nextImage.attr('src') == undefined){
				return;
			}
			
			console.log(nextImage.attr('src'));
			zoomRemoveControls();
			$("._zoomMainImage").animate(
			{
				left: $(window).width()+$(this).width()
				
			}, 300, function() 
			{
				$('#navleft').click();

				
				$(this).remove();

				var imagePosLeft 	= zoomedImage.offset().left;
				var imagePosTop  	= zoomedImage.offset().top;
				var imageWidth   	= zoomedImage.width();
				var imageHeight   	= zoomedImage.height();

				zoomedImage = nextImage;

				$('body').prepend("&lt;img class='_zoomMainImage' src='"+nextImage.attr('src')+"' style='position:absolute;left:"+imagePosLeft+"px;top:"+imagePosTop+"px;height:"+imageHeight+"px;z-index:10001;' /&gt;");
				$('._zoomMainImage').attr('src',(nextImage.data('mediumsource')));
				
				$(settings.catHeader).html(origPageTitle + '&lt;br /&gt;&lt;span style="margin-left:120px;"&gt;' + nextImage.data('pagetitle')+"&lt;/span&gt;");

				$('._zoomMainImage').css('cursor', 'url(/lib/images/zoom_in.svg),auto');	
				$('._zoomMainImage').click(function(){
					zoomImagePhaseTwo(nextImage);
				});

				resetPhaseOneZoomSizes();
				setTimeout(function(){
					phaseOneZoomAddControls();
				},100);
				
			});
		}

		function resetPhaseOneZoomSizes(){
			console.log(mainThis.height());

			$("._mtrGalleryZoom").css({
				'width'	: $(window).width(),
				'height' : mainThis.height() + (settings.thumbHeight + 10),
				"top"	: mainThis.position().top,
			});

			var newImageHeight  = $("._mtrGalleryZoom").height();
			
			var newImageWidth   = zoomedImage.width() * ( newImageHeight / zoomedImage.height() );
			var newImageLeft    = ($("._mtrGalleryZoom").width() - newImageWidth) / 2;

			if(newImageWidth &gt; ( $("._mtrGalleryZoom").width() - 130)){
				newImageWidth = ( $("._mtrGalleryZoom").width() - 130);
				var newImageTop 	= mainThis.position().top;
				var newImageLeft    = ($("._mtrGalleryZoom").width() - newImageWidth) / 2;
				var newImageHeight  = zoomedImage.height() * ( newImageWidth / zoomedImage.width() );
			}


			if(phaseTwoZoom == 0){
				$("._zoomMainImage").css({height:newImageHeight,left:newImageLeft,width:"auto"})
			}
			
			$('._mtrGalleryPhaseOneZoomControlsBack').css({
				"left" : (($('._zoomMainImage').offset().left + ($('._zoomMainImage').width() / 2)) - ( $('._mtrGalleryPhaseOneZoomControlsBack').width() / 2))+"px",
				"top" : (( $('._zoomMainImage').offset().top + $('._zoomMainImage').height() ) + 10)+"px"
			});

			$('._mtrGalleryPhaseOneZoomControlsPrint').css({
				"left" : ($('._zoomMainImage').offset().left + $('._mtrGalleryPhaseOneZoomControlsDownload').width()) + 10 + "px",
				"top" : (( $('._zoomMainImage').offset().top + $('._zoomMainImage').height() ) + 5)+"px"
			});

			$('._mtrGalleryPhaseOneZoomControlsPdf').css({
				"left" : ($('._zoomMainImage').offset().left + $('._mtrGalleryPhaseOneZoomControlsDownload').width()) + 43 + "px",
				"top" : (( $('._zoomMainImage').offset().top + $('._zoomMainImage').height() ) + 5)+"px"
			});
			
			$('._mtrGalleryPhaseOneZoomControlsDownload').css({
				"left" : $('._zoomMainImage').offset().left +"px",
				"top" : (( $('._zoomMainImage').offset().top + $('._zoomMainImage').height() ) + 10)+"px"
			});


			$('._mtrGalleryCaption').css({
				"left" : (($('._zoomMainImage').offset().left + ($('._zoomMainImage').width() )) - ( $('._mtrGalleryCaption').width() ))+"px",
				"top" : (( $('._zoomMainImage').offset().top + $('._zoomMainImage').height() ) + 10)+"px"
			});
			
			$("._mtrGalleryZoom").css({
				'width'	: $(window).width(),
				'height' :newImageHeight,
				"top"	: mainThis.position().top
			});

		}

		function phaseOneZoomAddControls(){
			$("body").prepend("&lt;span class='_mtrGalleryCaption' style=\"position:absolute;left:0px;top:0px;display:none;font-family:'Asap';font-size:12px;cursor:pointer;z-index:99999999;\"&gt;caption&lt;/span&gt;");
			$("body").prepend("&lt;span class='_mtrGalleryPhaseOneZoomControlsBack' style=\"position:absolute;left:0px;top:0px;display:none;font-family:'Asap';font-size:12px;cursor:pointer;z-index:99999999;\"&gt;back to series&lt;/span&gt;");
			$("body").prepend("&lt;div class='_mtrGalleryPhaseOneZoomControlsPrint' style=\"position:absolute;left:0px;top:0px;display:none;cursor:pointer;\"&gt;&lt;img src='/lib/images/Printer-icon.svg' style='z-index:99999999;width:24px;height:25px;'/&gt;&lt;/div&gt;");
			$("body").prepend("&lt;div class='_mtrGalleryPhaseOneZoomControlsPdf' style=\"position:absolute;left:0px;top:0px;display:none;cursor:pointer;\"&gt;&lt;img src='/lib/images/print_as_pdf.svg' style='z-index:99999999;width:24px;height:25px;'/&gt;&lt;/div&gt;");
			$("body").prepend("&lt;a href='/download/"+zoomedImage.data('uid')+"' class='_mtrGalleryPhaseOneZoomControlsDownload' style=\"z-index:99999999;color:#000000;position:absolute;left:0px;top:0px;display:none;font-family:'Asap';font-size:12px;cursor:pointer;\"&gt;save as&lt;/a&gt;");
			
			setTimeout(function(){
				$('._mtrGalleryPhaseOneZoomControlsDownload').css({
					"left" : $('._zoomMainImage').offset().left +"px",
					"top" : (( $('._zoomMainImage').offset().top + $('._zoomMainImage').height() ) + 10)+"px"
				});

				$('._mtrGalleryCaption').css({
					"left" : (($('._zoomMainImage').offset().left + ($('._zoomMainImage').width() )) - ( $('._mtrGalleryCaption').width() ))+"px",
					"top" : (( $('._zoomMainImage').offset().top + $('._zoomMainImage').height() ) + 10)+"px"
				});



				$('._mtrGalleryPhaseOneZoomControlsPrint').css({
					"left" : ($('._zoomMainImage').offset().left + $('._mtrGalleryPhaseOneZoomControlsDownload').width()) + 10 + "px",
					"top" : (( $('._zoomMainImage').offset().top + $('._zoomMainImage').height() ) + 5)+"px"
				});

				$('._mtrGalleryPhaseOneZoomControlsPdf').css({
					"left" : ($('._zoomMainImage').offset().left + $('._mtrGalleryPhaseOneZoomControlsDownload').width()) + 43 + "px",
					"top" : (( $('._zoomMainImage').offset().top + $('._zoomMainImage').height() ) + 5)+"px"
				});


				$('._mtrGalleryPhaseOneZoomControlsBack').css({
					"left" : (($('._zoomMainImage').offset().left + ($('._zoomMainImage').width() / 2)) - ( $('._mtrGalleryPhaseOneZoomControlsBack').width() / 2))+"px",
					"top" : (( $('._zoomMainImage').offset().top + $('._zoomMainImage').height() ) + 10)+"px"
				});
				
				

				if(zoomedImage.data('caption') == "YES"){
					$('._mtrGalleryCaption').fadeIn('fast');
				}
				$('._mtrGalleryPhaseOneZoomControlsBack').fadeIn('fast');
				$('._mtrGalleryPhaseOneZoomControlsPrint').fadeIn('fast');

				if(zoomedImage.data('pdf') &gt; 0){
					$('._mtrGalleryPhaseOneZoomControlsPdf').fadeIn('fast');
				}
				$('._mtrGalleryPhaseOneZoomControlsDownload').fadeIn('fast');

			},100);

			$("._mtrGalleryPhaseOneZoomControlsBack").click(function(){
				zoomOutImagePhaseTwo(zoomedImage);
			});

			$('._mtrGalleryCaption').click(function(){

				displayCaption(zoomedImage.data('uid'));
			});

			$('._mtrGalleryPhaseOneZoomControlsPdf').click(function(){
				window.open('/downloadpdf/'+zoomedImage.data('uid'));
			});

			$('._mtrGalleryPhaseOneZoomControlsPrint').click(function(){

				var iframe = document.createElement('iframe');
				iframe.setAttribute("style", "display:none");
				iframe.setAttribute("id","printf");
				iframe.setAttribute("name","printf");

				iframe.src = '/print/'+zoomedImage.data('uid');
				$("body").prepend(iframe);

				window.frames["printf"].focus();
				//window.frames["printf"].print();

				setTimeout(function(){
					iframe.remove();
				},300);

			});
		}

		function displayCaption(uId){
			var captionTop = 0;
			var captionRight = 0;
			if($(window).width() &gt; 1280){
				captionTop = zoomedImage.offset().top + 25	
				captionRight = 95;
			}
			else if($(window).width() &gt; 1024){
				captionTop = zoomedImage.offset().top + 25	
				captionRight = 50;
			}else{
				captionTop = zoomedImage.offset().top - 100;	
				captionRight = 40;
			}
			
			$('.galleryCaption').css('top',captionTop+"px");
			$('.galleryCaption').css('right',captionRight+"px");

			$('.galleryCaptionContent').empty();

			$.get( "/lib/ajax/fetchCaption.php", { uId: uId} ).done(function( data ) {
    			$('.galleryCaptionContent').html(data);
				$('.galleryCaption').fadeIn('fast');
 			});
		}

		function zoomRemoveControls(){
			$("._mtrGalleryPhaseOneZoomControlsBack").remove();
			$("._mtrGalleryPhaseOneZoomControlsPrint").remove();
			$("._mtrGalleryPhaseOneZoomControlsPdf").remove();
			$("._mtrGalleryPhaseOneZoomControlsDownload").remove();
			$("._mtrGalleryCaption").remove();
		}

		function zoomImagePhaseTwo(clickedImage){
			//Remove Controls
			if(clickedImage.hasClass("video")){
				zoomOutImagePhaseTwo(clickedImage);
				playVideo(clickedImage, 1);
				return;
			}
			$('#navleftzoom').hide();
			$('#navrightzoom').hide();


			zoomRemoveControls();

			phaseOneZoom = 0;
			$("body").prepend("&lt;div class='_mtrGalleryZoomPhaseTwoBack' style='width:100%;height:100%;background-color:#f8f8f8;position:fixed;z-index:10000;'&gt;&lt;/div&gt;");
			$("._zoomMainImage").unbind("click");
			$("._mtrGalleryZoom").append("&lt;img class='_mtrHiResImage' src='"+clickedImage.data("sourceimage")+"' style='display:none;'/&gt;");

			$("._mtrHiResImage").load(function(){
				$("._zoomMainImage").attr("src",clickedImage.data("sourceimage"));
			});

			var newImageLeft = (($(document).width() - clickedImage.data("sw")) / 2);
			var newImageTop = 0;

			if(clickedImage.data("sw") &gt; currentWindowWidth){
				var extraPixels = clickedImage.data("sw") - ( currentWindowWidth - 100 );
				var newImageLeft = 50 + ( -1 * extraPixels * (currentMouseX / currentWindowWidth) ) ;
				
			}
			
			if(clickedImage.data("sh") &gt; currentWindowHeight){
				var extraPixels = clickedImage.data("sh") - ( currentWindowHeight - 100 );
				var newImageTop = 50 + ( -1 * extraPixels * (currentMouseY / currentWindowHeight) ) ;
			}

			$("._zoomMainImage").animate(
			{
				height: clickedImage.data("sh")+"px",
				width: clickedImage.data("sw")+"px",
				top: newImageTop,
				left: newImageLeft
				
			}, 500, function() 
			{
				
				$(this).css('cursor', 'url(/lib/images/zoom_out.svg),auto');	
				phaseTwoZoom = 1;
				$("._zoomMainImage").click(function(){
					zoomOutImagePhaseOne(clickedImage);
				});
			});
		}

		function zoomOutImagePhaseOne(clickedImage){	

			$('#navleftzoom').show();
			$('#navrightzoom').show();

			phaseOneZoom = 1;
			phaseTwoZoom = 0;
			$('._mtrGalleryZoomPhaseTwoBack').remove();
			$("._zoomMainImage").unbind("click");
			
			var newImageHeight  = $("._mtrGalleryZoom").height();
			var newImageTop 	= mainThis.position().top;
			var newImageWidth   = clickedImage.width() * ( newImageHeight / clickedImage.height() );
			var newImageLeft    = ($("._mtrGalleryZoom").width() - newImageWidth) / 2;
			
			if(newImageWidth &gt; ( $("._mtrGalleryZoom").width() - 130)){
				newImageWidth = ( $("._mtrGalleryZoom").width() - 130);
				var newImageTop 	= mainThis.position().top;
				var newImageLeft    = ($("._mtrGalleryZoom").width() - newImageWidth) / 2;
				var newImageHeight  = clickedImage.height() * ( newImageWidth / clickedImage.width() );
			}

			$("._zoomMainImage").animate(
			{
				top : newImageTop+"px",
				left : newImageLeft+"px",
				height: newImageHeight+"px",
				width: newImageWidth + "px"
				
			}, 500, function() 
			{
				$(this).css('cursor', 'url(/lib/images/zoom_in.svg),auto');	
				$("._zoomMainImage").attr("src",clickedImage.attr("src"));
				phaseOneZoomAddControls();
				$(this).click(function(){
					zoomImagePhaseTwo(clickedImage);
				});
			});
			
		}

		function zoomOutImagePhaseTwo(clickedImage){
			$('#navleftzoom').remove();
			$('#navrightzoom').remove();
			
			scrollToThumb(mainSelectedImage);
			$('.galleryCaption').fadeOut('fast');
			phaseOneZoom = 0;

			//Destroy some elements
			$('._mtrGalleryPhaseOneZoomControlsBack').remove();
			$("._mtrGalleryPhaseOneZoomControlsPrint").remove();
			$("._mtrGalleryPhaseOneZoomControlsPdf").remove();
			$("._mtrGalleryPhaseOneZoomControlsDownload").remove();
			$("._mtrGalleryCaption").remove();

			$(".mainImage").unbind("click");

			$("._mtrGalleryZoom").fadeOut(500);
			$('#_mtrGalleryThumbContainer').fadeIn('fast');
			$("._zoomMainImage").animate(
			{
				top : clickedImage.offset().top,
				left : clickedImage.offset().left,
				height: clickedImage.height(),
				width: clickedImage.width()
				
			}, 500, function() 
			{
				clickedImage.css('opacity',1);
				$(this).hide();
				$('._zoomMainImage').remove();
				$(".mainImage").click(function(){
					if($(this).hasClass("mtrVisible")){
						zoomImagePhaseOne($(this));
					}
				});

				//Destroy all elements
				$('._mtrGalleryZoom').remove();
				$('._mtrGalleryZoomPhaseTwoBack').remove();
			});
			
			$(settings.catHeader).html(origPageTitle);
			
		}

		function panZoomedImage(mouseX,mouseY){
			var clickedImage = zoomedImage;
			if(clickedImage.data("sw") &gt; currentWindowWidth){
				var extraPixels = clickedImage.data("sw") - ( currentWindowWidth - 100 );
				var newLeft = 50 + ( -1 * extraPixels * (currentMouseX / currentWindowWidth) ) ;
				$("._zoomMainImage").css('left', newLeft + 'px');
			}
			
			if(clickedImage.data("sh") &gt; currentWindowHeight){
				var extraPixels = clickedImage.data("sh") - ( currentWindowHeight - 100 );
				var newTop = 50 + ( -1 * extraPixels * (currentMouseY / currentWindowHeight) ) ;
				$("._zoomMainImage").css('top', newTop + 'px');
			}
		}

		function resetPhaseTwoZoomPositions(){
			var clickedImage = zoomedImage;
			var newImageLeft = (($(document).width() - clickedImage.data("sw")) / 2);
			$("._zoomMainImage").css('left',newImageLeft);
		}

		//Set main image margins
		mainThis.find("img").each(function(){
			$(this).css("margin-right",settings.imageMarginRight);
		});		

		function calculateMainWidth(){
			var width = 0;

			mainThis.find("img").each(function(){
				width = width + getImageSize($(this));
				
				width = width + settings.imageMarginRight;
			});

			return width;
		}

		function getImageSize(imageElem){
			var imageWidth = $(this).width();
			if(imageWidth == 0){
				getImageSize(imageElem);
			}else{
				return imageWidth;
			}
		}

		function resetImageHeight(){
			
			largeImageHeight = containerHeight;
						
			mainThis.find("img").each(function(){
				
				var ratio = $(this).data('pw') / $(this).data('ph');
				
				var newWidth = $(this).data('pw');
				var newHeight = $(this).data('ph');
				if ( newWidth &gt; mainThis.parent().width() ) {
					newWidth = mainThis.parent().width();
					newHeight = newWidth / ratio;
				}
				
				if ( newHeight &gt; $(window).height() - windowMarginBottomMutable ) {
					newHeight = $(window).height() - windowMarginBottomMutable;
					newWidth = newHeight * ratio;
				}
				
				$(this).css('width', newWidth);
				$(this).css('height', newHeight);
				
			});
			
			
			if(playingVideo != ""){
				$(playingVideo).width(zoomedImage.width());
				$(playingVideo).height(zoomedImage.height());
				$(playingVideo).css("left",zoomedImage.offset().left);
				$(playingVideo).css("top",zoomedImage.offset().top);
				if(!zoomedImage.hasClass('mtrVisible')){
					removeVideo();
				}
			}
		}

		//Reset entire window on resize
		$(window).resize(function(){
			
			initSizes();
			//Caption size

			var captionTop = 0;
			var captionRight = 0;
			if($(window).width() &gt; 1280){
				captionTop = zoomedImage.offset().top + 25	
				captionRight = 95;
			}
			else if($(window).width() &gt; 1024){
				captionTop = zoomedImage.offset().top + 25	
				captionRight = 50;
			}else{
				captionTop = zoomedImage.offset().top - 100;	
				captionRight = 40;
			}
			
			$('.galleryCaption').css('top',captionTop+"px");
			$('.galleryCaption').css('right',captionRight+"px");


			if(phaseTwoZoom == 1){
				resetPhaseTwoZoomPositions();
			}

			if(phaseOneZoom == 1 || phaseTwoZoom == 1){
				resetPhaseOneZoomSizes();
			}
			
			setTimeout( function() {
				
				initSizes();

				if(phaseTwoZoom == 1){
					resetPhaseTwoZoomPositions();
				}

				if(phaseOneZoom == 1 || phaseTwoZoom == 1){
					resetPhaseOneZoomSizes();
				}
				
			}, 1000);

		});

		function fastVisibleScan(selectedImage){

			var countingWidth = 0;
			var viewPortWidth = mainThis.parent().width();
			var returnArray = [];
			returnArray["visibleImages"] = [];

			for(i=selectedImage-1;i &lt; selectedImage + (mainThis.find("img").length - selectedImage);i++){
				var newImage = mainThis.children('img').eq(i);
				if( ((countingWidth + newImage.width()) + settings.imageMarginRight) &lt; viewPortWidth){
					
					countingWidth = ((countingWidth + newImage.width()) + settings.imageMarginRight);
					returnArray["visibleImages"].push((i + 1));
				}else{
					break;
				}				
			}
			
			var spaceLeft = viewPortWidth - countingWidth;
			
			returnArray["spaceLeft"] = (spaceLeft / 2);
			
			return returnArray;
		}

		function detectOnScreen(selectedImage){
			
			currentlyLinkedImgs	= new Array();

			var viewPortWidth = mainThis.parent().width();
			var countingWidth = 0;
			var visibleImagesArray = [];

			$(".mainImage").removeClass("mtrVisible");
			var newImage = "";			
			for(i=selectedImage;i &lt; selectedImage + (mainThis.find("img").length - selectedImage);i++){

				newImage = mainThis.children('img').eq(i);
				
				if( ((countingWidth + newImage.width()) + (settings.imageMarginRight - 11)) &lt; viewPortWidth)
				{
					if(selectedImage == 0){
						
						newImage.css("opacity","1");
						newImage.addClass("mtrVisible");

					}else{
						
						newImage.addClass("mtrVisible");
						
						if(newImage.css("opacity") == 0){
							newImage.animate(
							{
								opacity: 1,
								
							}, 500, function() 
							{
						    	
							});
						}
					}
					
					visibleImagesArray.push(i);	

					countingWidth = ((countingWidth + newImage.width()) + settings.imageMarginRight);
					
				}else{
					
					break;
				}				
			}
			if(visibleImagesArray.length == 0){
				newImage.css("opacity","1");
				newImage.addClass("mtrVisible");
			}
			$(".mainImage").not(".mtrVisible").css("opacity","0");
			
			mainThis.find("img.mtrVisible").each(function(){
				$(this).attr("src", $(this).data("mediumsource"));
			});
			
			currentlyLinkedWidth = countingWidth;

			var spaceLeft = viewPortWidth - countingWidth;
			currentSpaceLeft = spaceLeft / 2;
			mainThis.children('img').eq(selectedImage).css("margin-left",( (settings.imageMarginRight / 2) + (spaceLeft / 2) ) + "px");
			
			//Set visible thumbs to greyscale
			$("._mtrGalleryThumbImage").removeClass("mtrVisibleThumb");

			for (var i = 0; i &lt; visibleImagesArray.length; i++) {
				var imageNumber = visibleImagesArray[i];
				imageNumber++;

    			$("._mtrGalleryThumbImage:nth-child("+imageNumber+")").css("-webkit-filter","grayscale(100%)");
    			$("._mtrGalleryThumbImage:nth-child("+imageNumber+")").css("filter","grayscale(100%)");
    			$("._mtrGalleryThumbImage:nth-child("+imageNumber+")").css("opacity","0.5");

    			$("._mtrGalleryThumbImage:nth-child("+imageNumber+")").addClass("mtrVisibleThumb");
			}

			$("._mtrGalleryThumbImage").not(".mtrVisibleThumb").css("-webkit-filter","");
			$("._mtrGalleryThumbImage").not(".mtrVisibleThumb").css("filter","");
			$("._mtrGalleryThumbImage").not(".mtrVisibleThumb").css("opacity","1");

			if(visibleImagesArray.length == 0){
				imageTooWidthshowOne();
			}
		
		}
		
		function createThumbs(){
			var workingContainer = mainThis.parent();
			workingContainer.append("&lt;div id='_mtrGalleryThumbContainer' style='width:"+settings.thumbContainerWidth+"px;margin:auto;height:"+(settings.thumbHeight + 20)+"px;margin-top:20px;text-align:center;overflow:auto;'&gt;&lt;/div&gt;");


			$("#_mtrGalleryThumbContainer").append("&lt;div id='_mtrGalleryThumbLongBar' style='width:2000px;height:"+settings.thumbHeight+"px;'&gt;&lt;/div&gt;");

			var counter = 0;
			var widthCounter = 0;

			mainThis.find("img").each(function(){
				if(counter == 0){
					$("#_mtrGalleryThumbLongBar").append("&lt;img class='_mtrGalleryThumbImage mtrVisibleThumb' data-mtrgalleryimageuid="+counter+" src='"+$(this).data('thumbsource')+"' style='margin:0px;padding:0px;border:0px;height:"+settings.thumbHeight+"px;width:"+$(this).data("tw")+"px;margin-right:"+(settings.imageMarginRight / 2)+"px;cursor:pointer;float:left;display:block;' /&gt;");	
				}else{
					$("#_mtrGalleryThumbLongBar").append("&lt;img class='_mtrGalleryThumbImage' data-mtrgalleryimageuid="+counter+" src='"+$(this).data('thumbsource')+"' style='margin:0px;padding:0px;border:0px;height:"+settings.thumbHeight+"px;width:"+$(this).data("tw")+"px;margin-right:"+(settings.imageMarginRight / 2)+"px;cursor:pointer;float:left;display:block;' /&gt;");
				}
				widthCounter = widthCounter + $(this).data("tw");

				counter++;
			});
			
			widthCounter = widthCounter + (counter * (settings.imageMarginRight / 2));
			

			if(widthCounter &lt; settings.thumbContainerWidth){
				$("#_mtrGalleryThumbContainer").width(widthCounter);
			}

			$("#_mtrGalleryThumbLongBar").css('width',widthCounter+"px");

			$("#_mtrGalleryThumbLongBar").append("&lt;div style='clear:both;'&gt;&lt;/div&gt;");

			$("._mtrGalleryThumbImage").click(function(){
				openImageOnPosition($(this).data("mtrgalleryimageuid"));
			});

			$("#_mtrGalleryThumbContainer").mCustomScrollbar({
    			axis:"x",
    			keyboard: false,
    			theme:"dark",
				scrollButtons:{enable:true}
			});
			
		}

		function scrollToThumb(thumbPos){
			uppedPos = thumbPos +1;
			var thumbLeft = $('._mtrGalleryThumbImage:nth-child('+uppedPos+')').position().left;

			$("#_mtrGalleryThumbContainer").mCustomScrollbar("scrollTo",thumbLeft,0);
		}
		
		function fixImageAspectRatio() {
			
			mainThis.find("img").each(function(){
				
				var w = $(this).data('pw');
				var h = $(this).data('ph');
				
				var newHeight = ( h / w ) * settings.maxWidth;
				
				$(this).css('max-height', newHeight + 'px');
				
			});
			
		}
		
		function openImageOnPosition(positionInt){
		
				if(positionInt == 0){
		    	   $('#navleft').css('opacity','0.2');
		   		}else{
		   			$('#navleft').css('opacity','1.0');
		   		}

		   		if((positionInt + 1) == $(".mainImage").length){
		        	$("#navright").css('opacity','0.2');
		    	}else{
		    		$('#navright').css('opacity','1.0');
		    	}

			scrollToThumb(positionInt);

			if(playingVideo){
				$(playingVideo).stop();
				removeVideo();
			}

			//IF IMAGE IS LEFT FROM VISIBLE
			if(positionInt &lt; mainSelectedImage){

				var moveLeft = settings.maxWidth;

				var uppedPositionInt 	= positionInt+1;
				var scanResultUpcomming = fastVisibleScan(uppedPositionInt);
				var scanResultCurrent 	= fastVisibleScan((mainSelectedImage + 1));


				console.log(scanResultUpcomming["visibleImages"]);
				console.log(scanResultCurrent["visibleImages"]);
				console.log(scanResultUpcomming["spaceLeft"]);

				var newparsedArray = [];
				$(scanResultCurrent["visibleImages"]).each(function(key,val){
					newparsedArray.push("int"+val);
				});
				console.log(newparsedArray);

				var newLeft = scanResultCurrent["spaceLeft"] * -1;
				$(scanResultUpcomming["visibleImages"]).each(function(key,val){

					if(newparsedArray.indexOf("int"+val) == -1){
						console.log("adding image "+val+" to width || "+$(".mainImage:nth-child("+val+")").width());
						console.log("Oldval = "+newLeft);
						newLeft = newLeft + $(".mainImage:nth-child("+val+")").width();
						console.log("newVal = "+newLeft );
						newLeft = newLeft + settings.imageMarginRight;
					}
					
				});
				newLeft = newLeft + scanResultUpcomming["spaceLeft"];
				

				console.log("Old left was " +scanResultCurrent["spaceLeft"]);
				console.log("Move to left " +newLeft);

				moveLeft = newLeft;

				mainThis.animate(
					{
						opacity: 1,
						marginLeft: moveLeft
					}, 300, function() 
					{
						

						for(i=mainSelectedImage; i &gt; positionInt;i--){
							
							$(".mainImage:nth-child("+i+")").show();
						}
						mainSelectedImage = positionInt;
						mainThis.css('margin-left','0px');
						$(".mainImage").css("margin-left","0px");
				    	initSizes();
				    
					}
				);

			}else{
				//IF IMAGE IS RIGHT FROM VISIBLE
			
				var moveLeft = -settings.maxWidth;

				if( $(".mainImage:nth-child("+positionInt+")").hasClass("mtrVisible") ){
					
					var uppedPositionInt = positionInt+1;
					var fastVisibleScanResult = fastVisibleScan(uppedPositionInt);
					var endPositionImage = fastVisibleScanResult["spaceLeft"];
					var startPositionTwo = 0;
					
					var calculatedWidth = 0;
					for(i=positionInt;i&gt;0;i--){
						
						if($(".mainImage:nth-child("+i+")").hasClass("mtrVisible")){
							calculatedWidth = calculatedWidth + $(".mainImage:nth-child("+i+")").width();
							calculatedWidth = calculatedWidth + settings.imageMarginRight;
						}else{
							break;
						}
					}
					calculatedWidth = calculatedWidth + currentSpaceLeft;
					startPositionTwo = calculatedWidth;
					moveLeft = (startPositionTwo * -1) + endPositionImage;
				}

				mainSelectedImage = positionInt;
				
				mainThis.animate(
					{
						opacity: 1,
						marginLeft: moveLeft
					}, 300, function() 
					{
						
						for(i=1;i&lt;(positionInt+1);i++){
							$(".mainImage:nth-child("+i+")").hide();
						}
						mainThis.css('margin-left','0px');
						$(".mainImage").css("margin-left","0px");
				    	initSizes();				    	
					}
				);

			}
		}
	}

})(jQuery);


</pre></body></html>