var LIGHTGALLERY = new Class({
	initialize: function() {
		var _self = this;
		this.sID = null;
		
		this.images = new Object();
		this.imagePath = '/images/';
		this.ajaxPath = '/ajax/getImages.php';
		this.activeImage = 0;
		this.diashowTimer = null;
		this.diashowTime = 5000;
		this.diashow = false;
		
		this.timer = null;
		this.fadeTime = 3000;
		
		this.options = {
			imagePath:'/images/',
			name:null,
			directory:null,
			description:null,
			withDate:false,
			lightgalleryID:'lightgallery',
			controlPanelWrapperID:'controlpanel-wrapper',
			diashowWrapperID:'diashow-wrapper',
			diashowButtonPlaySrc:'button-diashow-play.gif',
			diashowButtonPauseSrc:'button-diashow-pause.gif',
			closeButtonWrapperID:'close-button-wrapper',
			closeButtonSrc:'button-close.gif',
			dateWrapperID:'date-wrapper',
			galleryInformationWrapperID:'gallery-information-wrapper',
			imageIndexID:'image-index',
			scrollFactor:5,
			scrollLeftButtonWrapperID:'scroll-left-button-wrapper',
			scrollLeftButtonID:'scroll-left-button',
			scrollLeftButtonSrc:this.imagePath + 'button-scroll-left.gif',
			scrollRightButtonWrapperID:'scroll-right-button-wrapper',
			scrollRightButtonID:'scroll-right-button',
			scrollRightButtonSrc:this.imagePath + 'button-scroll-right.gif',
			thumbnailWrapperID:'thumbnail-wrapper',
			thumbnailsID:'thumbnails',
			thumbnailHeight:60,
			thumbnailWidth:60,
			thumbnailBorder:0,
			thumbnailBorderBottom:3,
			thumbnailMarginRight:5,
			thumbnailMorphDown:10,
			previewWidth:200,
			previewHeight:200,
			previewBorder:3,
			imageWrapperID:'image-wrapper',
			imageWrapperHeight:0,
			imageBorder:0,
			imageOffsetHeight:60,
			currentImageGroup: 'rest'
		};
		
		this.elements = {
			lightgallery:null,
			controlPanelWrapper:null,
			diashowWrapper:null,
			diashow:null,
			diashowLabel:null,
			closeButtonWrapper:null,
			closeButtonLabel:null,
			closeButton:null,
			scrollLeftButtonWrapper:null,
			scrollLeftButton:null,
			scrollRightButtonWrapper:null,
			scrollRightButton:null,
			thumbnailWrapper:null,
			thumbnails:null,
			imageWrapper:null,
			dateWrapper:null,
			dateSelectbox:null
		};
		
		this.fx = {
			diashowWrapper:null,
			timeDiashowWrapperFadeIn:300,
			timeDiashowWrapperFadeOut:300,
			closeButtonWrapper:null,
			timeCloseButtonWrapperFadeIn:300,
			timeCloseButtonWrapperFadeOut:300,
			scrollLeft:null,
			timeScrollLeft:500,
			scrollLeftButton:null,
			timeScrollLeftButtonFadeIn:300,
			timeScrollLeftButtonFadeOut:100,
			scrollRight:null,
			timeScrollRight:500,
			scrollRightButton:null,
			timeScrollRightButtonFadeIn:300,
			timeScrollRightButtonFadeOut:100
		};
		
		this.labels = {
			playDiashow:'Diashow starten',
			pauseDiashow:'Pause',
			closeLightgallery:'schlie\u00dfen',
			image:'Bild',
			imageOf:'von',
			scrollToLeft:'nach links scrollen',
			scrollToRight:'nach rechts scrollen'
		};
		
		this.docMouseMoveFunc = function() {
			if(_self.timer != null) window.clearTimeout(_self.timer);
			_self.timer = window.setTimeout(function() {_self.hidePrevNextButtons();}, _self.fadeTime);
		}
		document.addEvent('mousemove', this.docMouseMoveFunc);
		
		document.addEvent('keyup', function(event) {
			switch(event.code) {
				case 32:
					_self.elements.diashow.fireEvent('click');
					break;
				case 37:
					_self.stopDiashowTimer();
					_self.showPrevImage();
					break;
				case 39:
					_self.stopDiashowTimer();
					_self.showNextImage();
					break;
				default:
					break;
			}
		});
		
		window.addEvent('resize', function() {
			_self.resize();
		});
	},
	
	generateWrapper: function() {
		var bodyElement = document.body;
		
		this.elements.lightgallery = new Element('div', {'id':this.options.lightgalleryID}).setStyles({'width':window.getWidth().toInt()});
		this.elements.controlPanelWrapper = new Element('div', {'id':this.options.controlPanelWrapperID});
		
		// Top Wrapper
		var wrapper = new Element('div').setStyles({'height':40, 'line-height':40, 'border-bottom':'1px solid #eeeeee'});
		
		// Diashow Wrapper
		this.elements.diashowWrapper = new Element('div', {'id':this.options.diashowWrapperID}).setStyles({'opacity':0});
		
		// Diashow Image
		this.elements.diashow = new Element('img', {'alt':this.labels.playDiashow, 'src':this.options.imagePath + this.options.diashowButtonPlaySrc, 'title':this.labels.playDiashow}).setStyles({'cursor':'pointer', 'height':30, 'margin':'0px 5px -11px 0px', 'width':30});
		this.setEventsForDiashow();
		
		// Diashow Label
		this.elements.diashowLabel = new Element('span').setStyles({'cursor':'pointer'}).set('html', this.labels.playDiashow);
		this.setEventsForDiashowLabel();
		
		this.elements.diashowWrapper.grab(this.elements.diashow).grab(this.elements.diashowLabel);
		
		// close-button-Wrapper
		this.elements.closeButtonWrapper = new Element('div', {'id':this.options.closeButtonWrapperID}).setStyles({'opacity':0});
		
		// Close-Button-Image
		this.elements.closeButton = new Element('img', {'alt':this.labels.closeLightgallery, 'src':this.options.imagePath + this.options.closeButtonSrc, 'title':this.labels.closeLightgallery}).setStyles({'cursor':'pointer', 'margin':'0px 0px -11px 5px'});
		this.setEventsForCloseButton();
		
		// Close-Button-Label
		this.elements.closeButtonLabel = new Element('span').setStyles({'cursor':'pointer'}).set('html', this.labels.closeLightgallery);
		this.setEventsForCloseButtonLabel();
		
		this.elements.closeButtonWrapper.grab(this.elements.closeButtonLabel).grab(this.elements.closeButton);
		
		// Date-Select
		this.elements.dateWrapper = new Element('div', {'id':this.options.dateWrapperID}).setStyles({'opacity':0});
		this.elements.dateSelectbox = new Element('select', {}).setStyles({'cursor':'pointer', 'margin':'0px 0px -11px 5px'});
		var dateText = new Element('span').set('text','Auswahl Datum: ');
		this.elements.dateWrapper.grab(dateText).grab(this.elements.dateSelectbox);
		this.setEventsSelectbox();
		
		// gallery-information
		this.elements.galleryInformationWrapper = new Element('div', {'id':this.options.galleryInformationWrapperID}).setStyles({'opacity':0});
		
		// Alles in den Wrapper einbinden und Wrapper zum Panel einbinden
		wrapper.grab(this.elements.diashowWrapper);
		wrapper.grab(this.elements.closeButtonWrapper);
		wrapper.grab(this.elements.dateWrapper);
		wrapper.grab(this.elements.galleryInformationWrapper).grab(new Element('div', {'class':'clear'}));
		this.elements.controlPanelWrapper.grab(wrapper);
		
		// scroll-left-button
		this.elements.scrollLeftButtonWrapper = new Element('div', {'id':this.options.scrollLeftButtonWrapperID});
		this.elements.scrollLeftButton = new Element('img', {'alt':this.labels.scrollToLeft, 'id':this.options.scrollLeftButtonID, 'title':this.labels.scrollToLeft}).setStyles({'cursor':'pointer', 'height':60, 'margin-right':this.options.thumbnailMarginRight, 'margin-top':this.options.thumbnailBorder, 'opacity':0, 'width':25});
		this.setEventsScrollLeftButton();
		this.elements.scrollLeftButton.src = this.options.scrollLeftButtonSrc;
		this.elements.scrollLeftButtonWrapper.grab(this.elements.scrollLeftButton);
		
		// scroll-right-button
		this.elements.scrollRightButtonWrapper = new Element('div', {'id':this.options.scrollRightButtonWrapperID}).setStyles({'margin-left':this.options.thumbnailMarginRight});
		this.elements.scrollRightButton = new Element('img', {'alt':this.labels.scrollToRight, 'id':this.options.scrollRightButtonID, 'title':this.labels.scrollToRight}).setStyles({'cursor':'pointer', 'height':60, 'margin-top':this.options.thumbnailBorder, 'opacity':0, 'width':25});
		this.setEventsScrollRightButton();
		this.elements.scrollRightButton.src = this.options.scrollRightButtonSrc;
		this.elements.scrollRightButtonWrapper.grab(this.elements.scrollRightButton);
		
		// thumbnail-wrapper
		this.elements.thumbnailWrapper = new Element('div', {'id':this.options.thumbnailWrapperID}).setStyles({'height':this.options.thumbnailHeight + 2 * (this.options.thumbnailBorder + this.options.thumbnailMorphDown)});
		this.elements.thumbnails = new Element('div', {'id':this.options.thumbnailsID});
		
		this.elements.thumbnailWrapper.grab(this.elements.thumbnails);
		
		this.elements.controlPanelWrapper.grab(this.elements.scrollLeftButtonWrapper).grab(this.elements.scrollRightButtonWrapper).grab(this.elements.thumbnailWrapper);
		this.elements.lightgallery.grab(this.elements.controlPanelWrapper);
		
		this.elements.imageWrapper = new Element('div', {'id':this.options.imageWrapperID}).setStyles({'top':this.elements.controlPanelWrapper.getHeight().toInt()});
		this.elements.lightgallery.grab(this.elements.imageWrapper);
		
		bodyElement.appendChild(this.elements.lightgallery);
	},
	
	setEventsSelectbox: function() {
		var _self = this;
		this.elements.dateSelectbox.addEvent('change', function(ev) {
			_self.options.currentImageGroup = ev.target.value;
			_self.elements.thumbnails.empty();
			_self.elements.imageWrapper.empty();
			_self.show();
		});
	},
	
	setEventsScrollRightButton: function() {
		var _self = this;
		this.elements.scrollRightButton.addEvent('mouseover', function() {
			if(_self.fx.scrollRightButton != null) _self.fx.scrollRightButton.cancel();
			_self.fx.scrollRightButton = new Fx.Morph(this, {duration:_self.fx.timeScrollRightButtonFadeIn}).start({'opacity':1});
		});
		this.elements.scrollRightButton.addEvent('mouseout', function() {
			if(_self.fx.scrollRightButton != null) _self.fx.scrollRightButton.cancel();
			_self.fx.scrollRightButton = new Fx.Morph(this, {duration:_self.fx.timeScrollRightButtonFadeIn}).start({'opacity':0.4});
		});
		this.elements.scrollRightButton.addEvent('click', function() {
			_self.scrollToRight();
		});
		this.elements.scrollRightButton.addEvent('load', function() {
			if(_self.fx.scrollRightButton != null) _self.fx.scrollRightButton.cancel();
			_self.fx.scrollRightButton = new Fx.Morph(this, {duration:_self.fx.timeScrollRightButtonFadeIn}).start({'opacity':0.4});
		});
	},
	
	setEventsScrollLeftButton: function() {
		var _self = this;
		this.elements.scrollLeftButton.addEvent('mouseover', function() {
			if(_self.fx.scrollLeftButton != null) _self.fx.scrollLeftButton.cancel();
			_self.fx.scrollLeftButton = new Fx.Morph(this, {duration:_self.fx.timeScrollLeftButtonFadeIn}).start({'opacity':1});
		});
		this.elements.scrollLeftButton.addEvent('mouseout', function() {
			if(_self.fx.scrollLeftButton != null) _self.fx.scrollLeftButton.cancel();
			_self.fx.scrollLeftButton = new Fx.Morph(this, {duration:_self.fx.timeScrollLeftButtonFadeIn}).start({'opacity':0.4});
		});
		this.elements.scrollLeftButton.addEvent('click', function() {
			_self.scrollToLeft();
		});
		this.elements.scrollLeftButton.addEvent('load', function() {
			if(_self.fx.scrollLeftButton != null) _self.fx.scrollLeftButton.cancel();
			_self.fx.scrollLeftButton = new Fx.Morph(this, {duration:_self.fx.timeScrollLeftButtonFadeIn}).start({'opacity':0.4});
		});
	},
	
	setEventsForCloseButtonLabel: function() {
		var _self = this;
		this.elements.closeButtonLabel.addEvent('mouseover', function() {
			this.setStyles({'text-decoration':'underline'});
		});
		this.elements.closeButtonLabel.addEvent('mouseout', function() {
			this.setStyles({'text-decoration':'none'});
		});
		this.elements.closeButtonLabel.addEvent('click', function() {
			new Fx.Morph(_self.elements.thumbnails, {duration:500}).start({'opacity':0});
			new Fx.Morph(_self.elements.controlPanelWrapper, {duration:500}).start({'opacity':0});
			new Fx.Morph(_self.elements.imageWrapper, {duration:500}).start({'opacity':0}).chain(function() {
				_self.elements.lightgallery.destroy();
				document.removeEvents();
				_self.initialize();
				if(typeof dialog == 'object') dialog.hide();
			});
		});
	},
	
	setEventsForCloseButton: function() {
		var _self = this;
		this.elements.closeButton.addEvent('click', function() {
			new Fx.Morph(_self.elements.thumbnails, {duration:500}).start({'opacity':0});
			new Fx.Morph(_self.elements.controlPanelWrapper, {duration:500}).start({'opacity':0});
			new Fx.Morph(_self.elements.imageWrapper, {duration:500}).start({'opacity':0}).chain(function() {
				_self.elements.lightgallery.destroy();
				document.removeEvents();
				_self.initialize();
				if(typeof dialog == 'object') dialog.hide();
			});
		});
	},
	
	setEventsForDiashow: function() {
		var _self = this;
		this.elements.diashow.addEvent('click', function() {
			if(_self.diashow) {
				_self.stopDiashowTimer();
			} else {
				_self.diashow = true;
				this.src = _self.imagePath + _self.options.diashowButtonPauseSrc;
				_self.elements.diashowLabel.set('html', _self.labels.pauseDiashow);
				_self.startDiashowTimer();
			}
		});
	},
	
	setEventsForDiashowLabel: function() {
		var _self = this;
		this.elements.diashowLabel.addEvent('mouseover', function() {
			this.setStyles({'text-decoration':'underline'});
		});
		this.elements.diashowLabel.addEvent('mouseout', function() {
			this.setStyles({'text-decoration':'none'});
		});
		this.elements.diashowLabel.addEvent('click', function() {
			if(_self.diashow) {
				_self.stopDiashowTimer();
			} else {
				_self.diashow = true;
				_self.elements.diashow.src = _self.imagePath + _self.options.diashowButtonPauseSrc;
				_self.elements.diashowLabel.set('html', _self.labels.pauseDiashow);
				_self.startDiashowTimer();
			}
		});
	},
	
	getImages: function(sID) {
		this.generateWrapper();
		var _self = this;
		new Request.JSON({
			url:this.ajaxPath,
			onComplete: function(response) {
				if(response.images) {
					if(typeof dialog == 'object') dialog.show();
					_self.sID = sID;
					if(response.gallery.name) _self.options.name = response.gallery.name;
					if(response.gallery.directory) _self.options.directory = response.gallery.directory;
					if(response.gallery.description) _self.options.description = response.gallery.description;
					if(response.gallery.withDate) _self.options.withDate = response.gallery.withDate;
					_self.setImages(response.images);
				} else _self.showError(0);
			}
		}).get({'sid':sID});
	},
	
	setImages: function(imageGroups) {
		var _self = this;
		var first = true;
		var options = '';
		$H(imageGroups).each(function(images, group){
			if(first) {
				_self.options.currentImageGroup = group;
				first = false;
			}
			_self.images[group] = new Array();
			if(_self.options.withDate) {
				if(group=='rest') options += '<option value="' + group + '">ohne Datum</option>';
				else options += '<option value="' + group + '">' + group + '</option>';
			}
			for(var i = 0; i < images.length; i++) {
				var last = (i == images.length -1 ? true : false);
				_self.images[group].push(new IMAGE(i, _self.options, images[i], last));
			}
		});
		
		if(this.options.withDate) {
			this.elements.dateSelectbox.empty();
			this.elements.dateSelectbox.set('html',options);
		}
		this.show();
	},
	
	show: function() {
		var _self = this;
		var thisImages = this.images[this.options.currentImageGroup];
		
		this.elements.galleryInformationWrapper.set('html', this.labels.image + '&nbsp;<span id="' + this.options.imageIndexID + '"></span>&nbsp;' + this.labels.imageOf + '&nbsp;' + (thisImages.length).toString().zerofill(3));
		this.elements.thumbnails.setStyles({
			'height':this.options.thumbnailHeight + 2 * this.options.thumbnailBorder + this.options.thumbnailMorphDown,
			'width':((this.options.thumbnailWidth + 2 * this.options.thumbnailBorder) * thisImages.length) + (this.options.thumbnailMarginRight * (thisImages.length - 1))
		});
		this.setDimensions();

		var images = new Array();
		
		for(var i = 0; i < thisImages.length; i++) {
			images.push(thisImages[i].filenameThumb);
			thisImages[i].showThumbnailWrapper();
		}

		
		new Asset.images(images, {
			onProgress: function(counter, index) {
				thisImages[index].showThumbnail();
			},
			onComplete: function() {
				//for(var i = 0; i < thisImages.length; i++) if(thisImages[i].options.isActive) _self.activeImage = i;
				for(var i = 0; i < thisImages.length; i++) {
					thisImages[i].options.isPreviewLoaded = false;
					thisImages[i].options.isImageLoaded = false;
				}
				_self.activeImage = 0;
				
				//if(_self.activeImage == 0) {
					thisImages[_self.activeImage].fx.thumbnailWrapper = new Fx.Morph(thisImages[_self.activeImage].elements.thumbnailWrapper, {duration:thisImages[_self.activeImage].fx.timeThumbnailWrapperFadeIn}).start({'border-color':'#eee', 'margin-top':thisImages[_self.activeImage].options.thumbnailMarginTop});
					thisImages[_self.activeImage].showImage();
				//}
			}
		});
	},
	
	setDimensions: function() {
		if($(this.options.controlPanelWrapperID.toString()) && $(this.options.imageWrapperID.toString())) {
			this.elements.lightgallery.setStyles({'width':window.getWidth().toInt()});
			var totalHeight = window.getHeight();
			this.options.imageWrapperHeight = totalHeight - $(this.options.controlPanelWrapperID.toString()).getHeight();
			if(this.elements.imageWrapper != null) this.elements.imageWrapper.setStyle('height', this.options.imageWrapperHeight);
		}
	},
	
	setActiveImageInactive: function(indx) {
		this.activeImage = indx;
		if($(this.options.imageIndexID.toString())) {
			$(this.options.imageIndexID.toString()).set('html', (indx + 1).toString().zerofill(3));
			if($(this.options.diashowWrapperID.toString()).getStyle('opacity') == 0) new Fx.Morph(this.options.diashowWrapperID.toString(), {duration:500}).start({'opacity':1});
			if($(this.options.galleryInformationWrapperID.toString()).getStyle('opacity') == 0) new Fx.Morph(this.options.galleryInformationWrapperID.toString(), {duration:500}).start({'opacity':1});
			if($(this.options.closeButtonWrapperID.toString()).getStyle('opacity') == 0) new Fx.Morph(this.options.closeButtonWrapperID.toString(), {duration:500}).start({'opacity':1});
			if(this.options.withDate) {
				if($(this.options.dateWrapperID.toString()).getStyle('opacity') == 0) new Fx.Morph(this.options.dateWrapperID.toString(), {duration:500}).start({'opacity':1});
				if(this.elements.dateSelectbox.getStyle('opacity') == 0) new Fx.Morph(this.elements.dateSelectbox, {duration:500}).start({'opacity':1});
			}
		}
		var thisImages = this.images[this.options.currentImageGroup];
		for(var i = 0; i < thisImages.length; i++) if(thisImages[i].options.isActive && thisImages[i].indx != indx) thisImages[i].hideImage();
	},
	
	scrollToLeft: function() {
		var left = this.elements.thumbnails.getStyle('left').toInt();
		var distance = this.options.scrollFactor * (this.options.thumbnailWidth + 2 * this.options.thumbnailBorder + this.options.thumbnailMarginRight);
		if(left + distance > 0) distance = Math.abs(left);
		
		if(this.fx.scrollLeft != null) this.fx.scrollLeft.cancel();
		this.fx.scrollLeft = new Fx.Morph(this.elements.thumbnails, {duration:this.fx.timeScrollLeft}).start({'left':left + distance});
	},
	
	scrollToRight: function() {
		var left = this.elements.thumbnails.getStyle('left').toInt();
		var distance = this.options.scrollFactor * (this.options.thumbnailWidth + 2 * this.options.thumbnailBorder + this.options.thumbnailMarginRight);
		var thumbnailsWidth = this.elements.thumbnails.getWidth();
		var thumbnailWrapperWidth = this.elements.thumbnailWrapper.getWidth();
		if(Math.abs(left) + distance + thumbnailWrapperWidth > thumbnailsWidth) distance = thumbnailsWidth - thumbnailWrapperWidth - Math.abs(left);
		if(thumbnailsWidth <= thumbnailWrapperWidth) distance = 0;
		
		if(this.fx.scrollRight != null) this.fx.scrollRight.cancel();
		this.fx.scrollRight = new Fx.Morph(this.elements.thumbnails, {duration:this.fx.timeScrollRight}).start({'left':left - distance});
	},
	
	showPrevImage: function() {
		var indx = this.activeImage - 1;
		var thisImages = this.images[this.options.currentImageGroup];
		if(indx < 0) indx = thisImages.length - 1;
		
		this.checkPosition(indx);
		
		if(thisImages[indx].fx.thumbnailWrapper != null)thisImages[indx].fx.thumbnailWrapper.cancel();
		thisImages[indx].fx.thumbnailWrapper = new Fx.Morph(thisImages[indx].elements.thumbnailWrapper, {duration:thisImages[indx].fx.timeThumbnailWrapperFadeIn}).start({'border-color':'#eee', 'margin-top':thisImages[indx].options.thumbnailMarginTop});
		thisImages[indx].showImage();
	},
	
	showNextImage: function() {
		var indx = this.activeImage + 1;
		var thisImages = this.images[this.options.currentImageGroup];
		if(indx >= thisImages.length) indx = 0;
		
		this.checkPosition(indx);
		
		if(thisImages[indx].fx.thumbnailWrapper != null) thisImages[indx].fx.thumbnailWrapper.cancel();
		thisImages[indx].fx.thumbnailWrapper = new Fx.Morph(thisImages[indx].elements.thumbnailWrapper, {duration:thisImages[indx].fx.timeThumbnailWrapperFadeIn}).start({'border-color':'#eee', 'margin-top':thisImages[indx].options.thumbnailMarginTop});
		thisImages[indx].showImage();
	},
	
	checkPosition: function(indx) {
		var left = this.elements.thumbnails.getStyle('left').toInt();
		var thumbnailWidth = this.options.thumbnailWidth + 2 * this.options.thumbnailBorder + this.options.thumbnailMarginRight;
		var thumbnailsWidth = this.elements.thumbnails.getWidth();
		var thumbnailWrapperWidth = this.elements.thumbnailWrapper.getWidth();
		var thumbnailLeft = indx * thumbnailWidth;
		var distance = this.options.scrollFactor * thumbnailWidth;
		
		if(thumbnailLeft - Math.abs(left) + this.options.thumbnailWidth + 2 * this.options.thumbnailBorder > thumbnailWrapperWidth) {
			// thumbnail is out of range (right side) - scroll right
			if(Math.abs(left) + distance + thumbnailWrapperWidth > thumbnailsWidth) distance = thumbnailsWidth - thumbnailWrapperWidth - Math.abs(left);
			if((indx + 1) * thumbnailWidth - Math.abs(left)- thumbnailWrapperWidth > distance) distance = (indx + 1) * thumbnailWidth - Math.abs(left) - thumbnailWrapperWidth;
			
			if(this.fx.scrollRight != null) this.fx.scrollRight.cancel();
			this.fx.scrollRight = new Fx.Morph(this.elements.thumbnails, {duration:this.fx.timeScrollRight}).start({'left':left - distance});
		} else if(Math.abs(left) > thumbnailLeft) {
			// thumbnail is out of range (left side) - scroll left
			if(left + distance > 0) distance = Math.abs(left);
			if(Math.abs(left) - thumbnailLeft > distance) distance = Math.abs(left) - thumbnailLeft;
			
			if(this.fx.scrollLeft != null) this.fx.scrollLeft.cancel();
			this.fx.scrollLeft = new Fx.Morph(this.elements.thumbnails, {duration:this.fx.timeScrollLeft}).start({'left':left + distance});
		}
	},
	
	hidePrevNextButtons: function() {
		if(!$H(this.images).hasValue()) return;
		var thisImages = this.images[this.options.currentImageGroup];
		if(typeof(thisImages[this.activeImage]) == 'object') thisImages[this.activeImage].hidePrevNextButtons();
	},
	
	startDiashowTimer: function() {
		var _self = this;

		if(this.diashow) {
			this.diashowTimer = (function() {
				_self.showNextDiashowImage();
			}).delay(this.diashowTime);
		}
	},
	
	stopDiashowTimer: function() {
		this.diashow = false;
		this.elements.diashow.src = this.imagePath + this.options.diashowButtonPlaySrc;
		this.elements.diashowLabel.set('html', this.labels.playDiashow);
		this.diashowTimer = $clear(this.diashowTimer);
	},
	
	showNextDiashowImage: function() {
		if(this.diashow) this.showNextImage();
	},
	
	resize: function() {
		this.diashowTimer = $clear(this.diashowTimer);
		
		if(this.elements.thumbnails != null && this.elements.thumbnailWrapper != null) {
			var _self = this;
			var thisImages = this.images[this.options.currentImageGroup];
			var left = this.elements.thumbnails.getStyle('left').toInt();
			var thumbnailsWidth = this.elements.thumbnails.getWidth();
			var thumbnailWrapperWidth = this.elements.thumbnailWrapper.getWidth();

			thisImages[this.activeImage].fx.image = new Fx.Morph(thisImages[this.activeImage].elements.image, {duration:100}).start({'opacity':0}).chain(function() {
				if(thisImages[_self.activeImage].subtitle != '') thisImages[_self.activeImage].elements.subtitleWrapper.setStyles({'opacity':0});
				thisImages[_self.activeImage].elements.prevButtonWrapper.setStyles({'display':'none'});
				thisImages[_self.activeImage].elements.nextButtonWrapper.setStyles({'display':'none'});
			});
			this.setDimensions();
			if((thumbnailsWidth - Math.abs(left) < thumbnailWrapperWidth) && (thumbnailsWidth >= thumbnailWrapperWidth)) this.elements.thumbnails.setStyles({'left':-(thumbnailsWidth - thumbnailWrapperWidth)});
			this.checkPosition(this.activeImage);
			for(var i = 0; i < thisImages.length; i++) thisImages[i].options.isImageLoaded = false;
			thisImages[this.activeImage].showImage();
		}
	},
	
	showError: function(errorCode) {
		switch(errorCode) {
			case 0:
				console.log('Bad response. No Image-Object found.');
				break;
			default:
				console.log('Unknown error.');
				break;
		}
	},
	
	containsDOM: function(container, containee) {
		var isParent = false;
		do {
			if((isParent = container == containee)) break;
			containee = containee.parentNode;
		}
		while (containee != null);
		return isParent;
	},
	  
	checkMouseOut: function(element, evt) {
		if(element.contains && evt.toElement) {
			return !element.contains(evt.toElement);
		}
		else if (evt.relatedTarget) {
			return !this.containsDOM(element, evt.relatedTarget);
		}
	}
});

String.prototype.zerofill = function(len) {
	var str = this;
	while(str.length < len) str = '0' + str;
	return str.toString();
}

var dialog = null;
var lightgallery = null;

window.addEvent('load', function() {
	dialog = new DIALOG();
	lightgallery = new LIGHTGALLERY();
});