/**
 * @author dj stonka
 */

var Gallery = Class.create();
Gallery.prototype = {
	
	initialize: function(strContainer) {
		this.imagesCount = 0;
		this.sliderIndex = 0;
		this.imagesPath = 'images/';
		
		var objContainer = $(strContainer);
		this.strContainer = strContainer;
		this.objContainer = objContainer;
		
		this.getImagesFromContainer(objContainer);
		
		Event.observe($('arrowUp'), 'click', doGalleryUp);
		Event.observe($('arrowBottom'), 'click', doGalleryBottom);
	},
	
	getImagesFromContainer: function(objContainer) {
		this.arrImgList = $A( objContainer.getElementsByClassName('imgThumb') );
		this.arrImgList.each(function(imgItem){
			imgItem.setOpacity(0.6);
			Event.observe(imgItem, 'click', function( objEvent ){
				gallery.doShowImage(imgItem);
                objEvent.stop();
			});
			Event.observe(imgItem, 'mouseover', function(){
				new Effect.Opacity(imgItem, { 
					duration: 0.2, 
					transition: Effect.Transitions.linear, 
					from: 0.6, 
					to: 1,
					beforeStart:function(){ 
						Event.stopObserving(imgItem, 'mouseover', function(){});
					},
					afterFinish:function(){
						Event.observe(imgItem, 'mouseover', function(){});
					}
				});
			});
			Event.observe(imgItem, 'mouseout', function(){
				new Effect.Opacity(imgItem, { duration: 0.7, transition: Effect.Transitions.linear, from: 1, to: 0.6 });
			});
		});
	},
	
	doShowImage: function(imgItem) {
		$('imageOverlay').setStyle({
			display: 'block',
			opacity: 0,
			position: 'relative',
			top: ((-($('mainGalleryImage').height))-1) + 'px',
			left: 1+'px',
			width: $('mainGalleryImage').width + 'px',
			height: $('mainGalleryImage').height + 'px',
			backgroundColor: '#000'
		});
		
		var imagesPath = this.imagesPath;
		
		new Effect.Opacity($('imageOverlay'), {
			duration:0.5, 
			from:0, 
			to:0.7,
			afterFinish: function() { 
				var urlNewImage = imagesPath + imgItem.alt + '_medium.jpg'
				var newImage = new Image();
				newImage.onload = function() {
					new Effect.Opacity($('imageOverlay'), {
							duration:0.5, 
							from:0.7, 
							to:0.0,
							afterFinish: function(){ 
								$('imageOverlay').setStyle({display: 'none'});
								$('imageOverlay').setStyle({width: '0px'});
								$('imageOverlay').setStyle({height: '0px'});
							}
					});
					$('mainGalleryLink').href = imagesPath + imgItem.alt + '.jpg'
				}
				newImage.src = urlNewImage;
				$('mainGalleryImage').src = newImage.src;
			}
		});
	}

} // class Gallery

function doGalleryUp() {
	if ( gallery.sliderIndex >= 1 ) {
		new Effect.Move(gallery.strContainer, { 
			x: 0, 
			y: 134, 
			duration: 0.5,
			mode: 'relative', 
			beforeStart:function(){ 
				Event.stopObserving($('arrowUp'), 'click', doGalleryUp);
				gallery.sliderIndex -= 1;
			},
			afterFinish:function(){
				Event.observe($('arrowUp'), 'click', doGalleryUp);
			}
		});
	}
	
	return false;
}
	
function doGalleryBottom(strContainer) {
	if ( gallery.sliderIndex <= (gallery.imagesCount - 4) ) {
		new Effect.Move (gallery.strContainer, { 
			x: 0, 
			y: -134,
			duration: 0.5,
			mode: 'relative', 
			beforeStart:function(){
				Event.stopObserving($('arrowBottom'), 'click', doGalleryBottom);
				gallery.sliderIndex += 1;
			},
			afterFinish:function(){
				Event.observe($('arrowBottom'), 'click', doGalleryBottom);
			}
		});
	}
	return false;
}
