PARKWENG.ui.gallery.Gallery = new Class({
	
	_backdrop: null,
	
	_container: null,
	
	_buttonClose: null,
	
	_image: null,
	
	_images: [],
	
	_thumbnails: [],
	
	_imageSize: null,
	
	_thumbnailsSize: null,
	
	_activeImage: null,
	
	initialize: function(config) {
		this._backdrop		 = new NETVISION.ui.UIComponent('pw__gallery');
		this._container		 = new NETVISION.ui.UIComponent('pw__galleryContent');
		this._buttonClose	 = new NETVISION.ui.UIComponent('pw__galleryClose');
		this._image			 = new NETVISION.ui.UIComponent('pw__galleryImgBig');
		
		this._imageSize		 = ($defined(config.imageSize)) ? config.imageSize : {x: 902, y: 455};
		this._thumbnailsSize = ($defined(config.thumbnailsSize)) ? config.thumbnailsSize : {x: 142, y: 67};
		
		if(null !== $('pw__galleryImgBig')) {
			this._activeImage = $('pw__galleryImgBig').get("src").replace("/dbFile/", "");
			this._activeImage = this._activeImage.substr(0, this._activeImage.indexOf('/'));
			this._activeImage = parseInt(this._activeImage, 10);
		}
		
		this._images		 = this._fetchImages();
		this._thumbnails	 = this._fetchThumbnails();
		
		this._positionThumbnailsContainer();
		
		this._buttonClose.element.addEvent("click",function(){
			this.hide();
		}.bind(this));
	},
	
	show: function(image) {
		image = parseInt(image,10);
		this._setImage(image);
		this._activeImage = image;
		
		var size = this._backdrop.getParent("div").getSize();
		
		this._backdrop.setStyles({
			width: size.x,
			height: size.y,
			opacity: 0.8,
			display: 'block'
		});
		
		this._container.setStyles({
			display: 'block'
		});
		
		this._getThumbnailByImageId(image).activate();
	},
	
	hide: function() {
		this._container.setStyle("display","none");
		this._backdrop.setStyle("display","none");
	},
	
	_setImage: function(id) {
		var img = $('pw__galleryImgBig'), 
			src = img.src.replace("/"+this._activeImage+"/","/"+id+"/"); 
		
		img.set("src",src);
	},
	
	
	_fetchImages: function() {
		var images = [];
		
		$$('.pw__galleryImageThumbnails').each(function(image){
			images.push(this._getImageIdFromElementId(image));
		}.bind(this));
		
		return images;
	},
	
	_fetchThumbnails: function() {
		var thumbs = [];
		var self   = this;
		$$('.pw__galleryImageThumbnails').each(function(th){
			var id = this._getImageIdFromElementId(th);
			thumbs.push(new PARKWENG.ui.gallery.Thumbnail(th,id));
		}.bind(this));
		
		thumbs.each(function(th){
			th.addEvent('click',function(){
				th.activate();
				self._setImage(th.getImageId());
				self._activeImage = th.getImageId();
			});
		});
		
		return thumbs;
	},
	
	_getImageIdFromElementId: function(el) {
		return parseInt(el.id.substr(el.id.lastIndexOf('_')+1),10);
	},
	
	_positionThumbnailsContainer: function() {
		var tcWidth = this._images.length*this._thumbnailsSize.x;
		var left = parseInt(($('pw__mainContainer').getSize().x-tcWidth)/2,10);
		$('pw__galleryImgPreview').setStyles({'left':left,'width':tcWidth});
	},
	
	_getThumbnailByImageId: function(id) {
		var th = null;
		
		this._thumbnails.each(function(thumb){
			if(id === thumb.getImageId()) {
				th = thumb;
				return;
			}
		}.bind(this));
		
		return th;
	}
});
