var IMAGE = new Class({
	initialize: function(indx, parentOptions, imageData, last) {
		this.indx = indx;
		this.parentOptions = parentOptions;
		this.ID = (imageData.id ? imageData.id : null);
		this.filename = (imageData.filename ? parentOptions.directory + imageData.filename : null);
		this.subtitle = imageData.subtitle;
		this.last = last;
		this.imagePath = 'http://www.quick-mix.com/ajax/image-manipulation.php';
		this.filenameThumb = this.imagePath + '?filename=' + this.filename.replace(/\.jpg$/, '-thumb.jpg') + '&manipulation=[{"action":"squarefromcenter","edgelength":' + this.parentOptions.thumbnailWidth + '}]';
		this.filenamePreview = this.imagePath + '?filename=' + this.filename + '&manipulation=[{"action":"resize","width":' + this.parentOptions.previewWidth + ',"height":' + this.parentOptions.previewHeight + '}]';
		this.previewLeftPosition = 0;
		this.showPreviewEnabled = false;
		this.filenameImage = null;
		this.imageLeftPosition = 0;
		this.imageTopPosition = 0;
		
		this.options = {
			thumbnailWrapperID:'thumbnail-' + this.ID,
			loaderSrc:'ajax-loader.gif',
			isActive:false,
			isPreviewLoaded:false,
			isImageLoaded:false,
			thumbnailMarginTop:10,
			previewIndexActive:25,
			previewIndexInactive:20,
			imageIndexActive:10,
			imageIndexInactive:5,
			subtitleWrapperTop:0,
			subtitleWrapperUp:true
		};
		
		this.elements = {
			thumbnailWrapper:null,
			thumbnail:null,
			loader:null,
			preview:null,
			imageWrapper:null,
			imageWrapperRelative:null,
			image:null,
			prevButtonWrapper:null,
			nextButtonWrapper:null,
			subtitleWrapper:null,
			subtitleToggleButtonWrapper:null,
			subtitleToggleButton:null,
			subtitle:null
		};
		
		this.fx = {
			thumbnailWrapper:null,
			timeThumbnailWrapperFadeIn:300,
			thumbnail:null,
			timeThumbnailFadeIn:1000,
			loader:null,
			timeLoaderFadeIn:300,
			preview:null,
			timePreviewFadeIn:50,
			timePreviewFadeOut:300,
			imageWrapper:null,
			timeImageWrapperFadeIn:1000,
			image:null,
			prevButtonWrapper:null,
			timePrevButtonWrapperFadeIn:200,
			timePrevButtonWrapperFadeOut:200,
			nextButtonWrapper:null,
			timeNextButtonWrapperFadeIn:200,
			timeNextButtonWrapperFadeOut:200,
			subtitleWrapper:null,
			timeSubtitleWrapperSlideUp:300,
			timeSubtitleWrapperSlideDown:300,
			subtitleToggleButtonWrapper:null,
			timeSubtitleToggleButtonWrapperFadeIn:200,
			timeSubtitleToggleButtonWrapperFadeOut:300,
			subtitle:null,
			timeSubtitleFadeIn:200,
			timeSubtitleFadeOut:300
		};
	},
	
	showThumbnailWrapper: function() {
		var _self = this;
		if(this.filename != null && $(this.parentOptions.thumbnailsID.toString())) {
			this.elements.thumbnailWrapper = new Element('div', {'class':'thumbnail-wrapper-inactive'});
			this.elements.thumbnailWrapper.setStyles({
				'border-width':this.parentOptions.thumbnailBorder,
				'height':this.parentOptions.thumbnailHeight,
				'margin-right':(this.last ? 0 : this.parentOptions.thumbnailMarginRight),
				'opacity':0,
				'width':this.parentOptions.thumbnailWidth
			});
			this.elements.thumbnail = new Element('img', {'alt':this.subtitle.replace(/<br \/>/g, ' - ').stripTags(), 'class':'thumbnail', 'title':this.subtitle.replace(/<br \/>/g, ' - ').stripTags()}).setStyles({'opacity':0});
			this.elements.thumbnail.addEvent('load', function() {
				_self.fx.thumbnail = new Fx.Morph(this, {duration:_self.fx.timeThumbnailFadeIn}).start({'opacity':1}).chain(function() {
					_self.fx.loader = new Fx.Morph(_self.elements.loader, {duration:_self.fx.timeLoaderFadeIn}).start({'opacity':0});
				});
			});
			this.elements.thumbnail.addEvent('mouseover', function() {
				if(!_self.options.isActive) {
					_self.showPreviewEnabled = true;
					if(_self.fx.thumbnailWrapper != null) _self.fx.thumbnailWrapper.cancel();
					_self.fx.thumbnailWrapper = new Fx.Morph(_self.elements.thumbnailWrapper, {duration:_self.fx.timeThumbnailWrapperFadeIn}).start({'border-color':'#f58220', 'margin-top':_self.options.thumbnailMarginTop});
					_self.showPreview();
				}
			});
			this.elements.thumbnail.addEvent('mouseout', function() {
				if(!_self.options.isActive) {
					_self.showPreviewEnabled = false;
					if(_self.fx.thumbnailWrapper != null) _self.fx.thumbnailWrapper.cancel();
					_self.fx.thumbnailWrapper = new Fx.Morph(_self.elements.thumbnailWrapper, {duration:_self.fx.timeThumbnailWrapperFadeIn}).start({'border-color':'#222', 'margin-top':0});
					_self.hidePreview();
				}
			});
			this.elements.thumbnail.addEvent('click', function() {
				if(!_self.options.isActive) {
					_self.showImage();
					if(typeof lightgallery == 'object') lightgallery.stopDiashowTimer();
				}
			});
			
			this.elements.loader = new Element('img', {'alt':'', 'class':'loader', 'src':this.parentOptions.imagePath + this.options.loaderSrc, 'title':''}).setStyles({'opacity':0});
			
			this.elements.thumbnailWrapper.grab(this.elements.thumbnail).grab(this.elements.loader);
			$(this.parentOptions.thumbnailsID.toString()).grab(this.elements.thumbnailWrapper);
			
			this.elements.preview = new Element('img', {'alt':this.subtitle.replace(/<br \/>/g, ' - ').stripTags(), 'class':'preview', 'title':this.subtitle.replace(/<br \/>/g, ' - ').stripTags()}).setStyles({'border-width':this.parentOptions.previewBorder, 'opacity':0});
			$(this.parentOptions.imageWrapperID.toString()).grab(this.elements.preview);
			// image-wrapper
			this.elements.imageWrapper = new Element('div', {'class':'image-wrapper'}).setStyles({'opacity':0});
			this.elements.imageWrapperRelative = new Element('div').setStyles({'overflow':'hidden', 'position':'relative'});
			// image
			this.elements.image = new Element('img', {'alt':this.subtitle.replace(/<br \/>/g, ' - ').stripTags(), 'class':'image', 'title':this.subtitle.replace(/<br \/>/g, ' - ').stripTags()}).setStyles({'border-width':this.parentOptions.imageBorder, 'opacity':0});
			this.elements.imageWrapperRelative.grab(this.elements.image)
			// prev-button
			this.elements.prevButtonWrapper = new Element('a', {'class':'prev-button-wrapper', 'href':'#'}).setStyles({'opacity':0.8});
			this.elements.prevButtonWrapper.addEvent('mouseover', function() {
				_self.elements.prevButtonWrapper.setStyles({'background':'transparent url(\'http://www.quick-mix.com/images/button-prev.gif\') no-repeat scroll left center'});
			});
			this.elements.prevButtonWrapper.addEvent('mousemove', function() {
				_self.elements.prevButtonWrapper.setStyles({'background':'transparent url(\'http://www.quick-mix.com/images/button-prev.gif\') no-repeat scroll left center'});
			});
			this.elements.prevButtonWrapper.addEvent('mouseout', function() {
				_self.elements.prevButtonWrapper.setStyles({'background-image':'url(data:image/gif;base64,AAAA)'});
			});
			this.elements.prevButtonWrapper.addEvent('focus', function() {
				this.blur();
			});
			this.elements.prevButtonWrapper.addEvent('click', function() {
				if(typeof lightgallery == 'object') {
					lightgallery.stopDiashowTimer();
					lightgallery.showPrevImage();
				}
			});
			// next-button
			this.elements.nextButtonWrapper = new Element('a', {'class':'next-button-wrapper', 'href':'#'}).setStyles({'opacity':0.8});
			this.elements.nextButtonWrapper.addEvent('mouseover', function() {
				_self.elements.nextButtonWrapper.setStyles({'background':'transparent url(\'http://www.quick-mix.com/images/button-next.gif\') no-repeat scroll right center'});
			});
			this.elements.nextButtonWrapper.addEvent('mousemove', function() {
				_self.elements.nextButtonWrapper.setStyles({'background':'transparent url(\'http://www.quick-mix.com/images/button-next.gif\') no-repeat scroll right center'});
			});
			this.elements.nextButtonWrapper.addEvent('mouseout', function() {
				_self.elements.nextButtonWrapper.setStyles({'background-image':'url(data:image/gif;base64,AAAA)'});
			});
			this.elements.nextButtonWrapper.addEvent('focus', function() {
				this.blur();
			});
			this.elements.nextButtonWrapper.addEvent('click', function() {
				if(typeof lightgallery == 'object') {
					lightgallery.stopDiashowTimer();
					lightgallery.showNextImage();
				}
			});
			this.elements.imageWrapperRelative.grab(this.elements.prevButtonWrapper).grab(this.elements.nextButtonWrapper);
			// subtitle
			if(this.subtitle != '') {
				this.elements.subtitleWrapper = new Element('div', {'class':'subtitle-wrapper'});
				this.elements.subtitleToggleButtonWrapper = new Element('div').setStyles({'opacity':0, 'padding-left':40});
				this.elements.subtitleToggleButton = new Element('img', {'src':'http://www.quick-mix.com/images/button-slide-down.gif'}).setStyles({'cursor':'pointer', 'display':'block'});
				this.elements.subtitleToggleButton.addEvent('click', function() {
					if(_self.options.subtitleWrapperUp) {
						// slideDown
						_self.options.subtitleWrapperUp = false;
						if(_self.fx.subtitleWrapper != null) _self.fx.subtitleWrapper.cancel();
						_self.fx.subtitleWrapper = new Fx.Morph(_self.elements.subtitleWrapper, {duration:_self.fx.timeSubtitleWrapperSlideDown}).start({'top':_self.options.subtitleWrapperTop + _self.elements.subtitleWrapper.getHeight().toInt() - _self.elements.subtitleToggleButtonWrapper.getHeight()}).chain(function() {
							_self.elements.subtitleToggleButton.set('src', 'http://www.quick-mix.com/images/button-slide-up.gif');
						});
					} else {
						// slideUp
						_self.options.subtitleWrapperUp = true;
						if(_self.fx.subtitleWrapper != null) _self.fx.subtitleWrapper.cancel();
						_self.fx.subtitleWrapper = new Fx.Morph(_self.elements.subtitleWrapper, {duration:_self.fx.timeSubtitleWrapperSlideUp}).start({'top':_self.options.subtitleWrapperTop}).chain(function() {
							_self.elements.subtitleToggleButton.set('src', 'http://www.quick-mix.com/images/button-slide-down.gif');
						});
					}
				});
			
				this.elements.subtitleToggleButtonWrapper.grab(this.elements.subtitleToggleButton);
			
				this.elements.subtitle = new Element('div', {'class':'subtitle'}).setStyles({'opacity':0}).set('html', this.subtitle);
				this.elements.subtitleWrapper.grab(this.elements.subtitleToggleButtonWrapper).grab(this.elements.subtitle);
				this.elements.imageWrapperRelative.grab(this.elements.subtitleWrapper);
			}
			
			this.elements.imageWrapper.grab(this.elements.imageWrapperRelative);
			$(this.parentOptions.imageWrapperID.toString()).grab(this.elements.imageWrapper);
			
			this.fx.thumbnailWrapper = new Fx.Morph(this.elements.thumbnailWrapper, {duration:this.fx.timeThumbnailWrapperFadeIn + this.indx * 100}).start({'opacity':1});
		}
	},
	
	showThumbnail: function() {
		this.elements.thumbnail.src = this.filenameThumb;
	},
	
	showPreview: function() {
		var _self = this;

		if(this.fx.preview != null) this.fx.preview.cancel();
		if(this.options.isPreviewLoaded) {
			this.elements.preview.setStyles({'left':_self.getPreviewLeftPosition(), 'z-index':this.options.previewIndexActive});
			if(this.showPreviewEnabled) this.fx.preview = new Fx.Morph(this.elements.preview, {duration:this.fx.timePreviewFadeIn}).start({'opacity':1});
		} else {
			this.elements.preview.addEvent('load', function() {
				_self.options.isPreviewLoaded = true;
				_self.elements.preview.setStyles({'left':_self.getPreviewLeftPosition(), 'z-index':_self.options.previewIndexActive});
				if(_self.showPreviewEnabled) _self.fx.preview = new Fx.Morph(_self.elements.preview, {duration:_self.fx.timePreviewFadeIn}).start({'opacity':1});
			});
			this.elements.preview.src = this.filenamePreview;
		}
	},
	
	hidePreview: function() {
		if(this.fx.preview != null) this.fx.preview.cancel();
		this.elements.preview.setStyles({'z-index':this.options.previewIndexInactive});
		this.fx.preview = new Fx.Morph(this.elements.preview, {duration:this.fx.timePreviewFadeOut}).start({'opacity':0});
	},
	
	getPreviewLeftPosition: function() {
		var scrollLeftWrapperWidth = $(this.parentOptions.scrollLeftButtonWrapperID.toString()).getWidth();
		var left = $(this.parentOptions.thumbnailsID.toString()).getStyle('left').toInt();
		
		this.previewLeftPosition = this.indx * (this.parentOptions.thumbnailWidth + (2 * this.parentOptions.thumbnailBorder) + this.parentOptions.thumbnailMarginRight) + parseInt(this.parentOptions.thumbnailWidth / 2) + this.parentOptions.thumbnailBorder + 10 - parseInt(this.elements.preview.getWidth() / 2);
		this.previewLeftPosition = this.previewLeftPosition + scrollLeftWrapperWidth + left;
		
		if(this.previewLeftPosition < 0) this.previewLeftPosition = 0;
		if(this.previewLeftPosition > ($(this.parentOptions.controlPanelWrapperID.toString()).getWidth() - this.elements.preview.getWidth()))
			this.previewLeftPosition = $(this.parentOptions.controlPanelWrapperID.toString()).getWidth() - this.elements.preview.getWidth();
		
		return this.previewLeftPosition;
	},
	
	showImage: function() {
		var _self = this;
		this.options.isActive = true;
		this.fx.loader = new Fx.Morph(this.elements.loader, {duration:this.fx.timeLoaderFadeIn}).start({'opacity':0.4});
		var maxHeight = parseInt($(this.parentOptions.imageWrapperID.toString()).getHeight() - (2 * this.parentOptions.imageBorder) - this.parentOptions.imageOffsetHeight);
		if(maxHeight < 0) maxHeight = 0;
		this.filenameImage = this.imagePath + '?filename=' + this.filename + '&manipulation=[{"action":"fittoheight","height":' + maxHeight + '},{"action":"fittowidth","width":' + parseInt($(this.parentOptions.imageWrapperID.toString()).getWidth() - (2 * this.parentOptions.imageBorder)) + '}]';
		
		if(this.options.isImageLoaded) {
			if(typeof lightgallery == 'object') lightgallery.setActiveImageInactive(this.indx);
			this.hidePreview();
			this.fx.imageWrapper = new Fx.Morph(this.elements.imageWrapper, {duration:this.fx.timeImageWrapperFadeIn}).start({'opacity':1, 'z-index':this.options.imageIndexActive});
			this.fx.image = new Fx.Morph(this.elements.image, {duration:this.fx.timeImageWrapperFadeIn}).start({'opacity':1}).chain(function() {
				_self.elements.prevButtonWrapper.setStyles({'display':'block'});
				_self.elements.nextButtonWrapper.setStyles({'display':'block'});
				if(_self.fx.loader != null) _self.fx.loader.cancel();
				_self.fx.loader = new Fx.Morph(_self.elements.loader, {duration:_self.fx.timeLoaderFadeIn}).start({'opacity':0});
				if(_self.subtitle != '') {
					_self.elements.subtitleWrapper.setStyles({'opacity':1, 'visibility':'visible'});
					_self.elements.subtitleToggleButtonWrapper.setStyles({'display':'block'});
					_self.elements.subtitle.setStyles({'display':'block'});
					if(_self.fx.subtitleToggleButtonWrapper != null) _self.fx.subtitleToggleButtonWrapper.cancel();
					_self.fx.subtitleToggleButtonWrapper = new Fx.Morph(_self.elements.subtitleToggleButtonWrapper, {duration:_self.fx.timeSubtitleToggleButtonWrapperFadeIn}).start({'opacity':1});
					if(_self.fx.subtitle != null) _self.fx.subtitle.cancel();
					_self.fx.subtitle = new Fx.Morph(_self.elements.subtitle, {duration:_self.fx.timeSubtitleFadeIn}).start({'opacity':0.6});
				}
				if(typeof lightgallery == 'object') lightgallery.startDiashowTimer();
			});
		} else {
			this.elements.image.onload = function() {
				_self.options.isImageLoaded = true;
				
				_self.imageLeftPosition = parseInt(($(_self.parentOptions.imageWrapperID.toString()).getWidth().toInt() - this.getWidth().toInt()) / 2);
				_self.imageTopPosition = parseInt(($(_self.parentOptions.imageWrapperID.toString()).getHeight().toInt() - this.getHeight().toInt()) / 2);
				
				if(typeof lightgallery == 'object') lightgallery.setActiveImageInactive(_self.indx);
				_self.elements.imageWrapper.setStyles({
					'height':this.getHeight().toInt(),
					'left':_self.imageLeftPosition,
					'top':_self.imageTopPosition,
					'width':this.getWidth().toInt()
				});
				_self.elements.imageWrapperRelative.setStyles({'height':this.getHeight().toInt(), 'width':this.getWidth().toInt()});
				_self.elements.prevButtonWrapper.setStyles({'height':this.getHeight().toInt(), 'left':0, 'width':parseInt(this.getWidth() / 2)});
				_self.elements.nextButtonWrapper.setStyles({'background-position':(parseInt(this.getWidth() / 2) - 50) + 'px center', 'height':this.getHeight().toInt(), 'right':0, 'width':parseInt(this.getWidth() / 2)});
				if(_self.subtitle != '') {
					_self.elements.subtitleWrapper.setStyles({'height':'auto', 'width':this.getWidth().toInt()});
					_self.options.subtitleWrapperTop = this.getHeight().toInt() - _self.elements.subtitleWrapper.getHeight().toInt();
					var startSlide = 0;
					if(_self.options.subtitleWrapperUp) startSlide = this.getHeight().toInt() - _self.elements.subtitleWrapper.getHeight().toInt();
					else startSlide = this.getHeight().toInt() - _self.elements.subtitleToggleButtonWrapper.getHeight().toInt();
					_self.elements.subtitleWrapper.setStyles({'top':startSlide});
				}
				_self.hidePreview();
				_self.fx.imageWrapper = new Fx.Morph(_self.elements.imageWrapper, {duration:_self.fx.timeImageWrapperFadeIn}).start({'opacity':1, 'z-index':_self.options.imageIndexActive});
				_self.fx.image = new Fx.Morph(_self.elements.image, {duration:_self.fx.timeImageWrapperFadeIn}).start({'opacity':1}).chain(function() {
					_self.elements.prevButtonWrapper.setStyles({'display':'block'});
					_self.elements.nextButtonWrapper.setStyles({'display':'block'});
					if(_self.fx.loader != null) _self.fx.loader.cancel();
					_self.fx.loader = new Fx.Morph(_self.elements.loader, {duration:_self.fx.timeLoaderFadeIn}).start({'opacity':0});
					if(_self.subtitle != '') {
						_self.elements.subtitleWrapper.setStyles({'opacity':1, 'visibility':'visible'});
						_self.elements.subtitleToggleButtonWrapper.setStyles({'display':'block'});
						_self.elements.subtitle.setStyles({'display':'block'});
						if(_self.fx.subtitleToggleButtonWrapper != null) _self.fx.subtitleToggleButtonWrapper.cancel();
						_self.fx.subtitleToggleButtonWrapper = new Fx.Morph(_self.elements.subtitleToggleButtonWrapper, {duration:_self.fx.timeSubtitleToggleButtonWrapperFadeIn}).start({'opacity':1});
						if(_self.fx.subtitle != null) _self.fx.subtitle.cancel();
						_self.fx.subtitle = new Fx.Morph(_self.elements.subtitle, {duration:_self.fx.timeSubtitleFadeIn}).start({'opacity':0.6});
					}
					if(typeof lightgallery == 'object') lightgallery.startDiashowTimer();
				});
			};
			this.elements.image.src = this.filenameImage;
		}
	},
	
	hideImage: function() {
		var _self = this;
		this.options.isActive = false;
		this.fx.thumbnailWrapper = new Fx.Morph(this.elements.thumbnailWrapper, {duration:this.fx.timeThumbnailWrapperFadeIn}).start({'border-color':'#555', 'margin-top':0});
		this.fx.imageWrapper = new Fx.Morph(this.elements.imageWrapper, {duration:this.fx.timeImageWrapperFadeIn}).start({'opacity':0, 'z-index':this.options.imageIndexInactive});
		this.fx.image = new Fx.Morph(this.elements.image, {duration:this.fx.timeImageWrapperFadeIn}).start({'opacity':0}).chain(function() {
			if(_self.subtitle != '') {
				_self.elements.subtitleToggleButtonWrapper.setStyles({'display':'none', 'opacity':0});
				_self.elements.subtitle.setStyles({'display':'none', 'opacity':0});
			}
			_self.elements.prevButtonWrapper.setStyles({'display':'none'});
			_self.elements.nextButtonWrapper.setStyles({'display':'none'});
		});
	},
	
	hidePrevNextButtons: function() {
		this.elements.prevButtonWrapper.setStyles({'background-image':'url(data:image/gif;base64,AAAA)'});
		this.elements.nextButtonWrapper.setStyles({'background-image':'url(data:image/gif;base64,AAAA)'});
	}
});