/**
 * @author Piotr Fugiel
 */

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($('arrowLeft'), 'click', doGalleryLeft);
		Event.observe($('arrowRight'), 'click', doGalleryRight);
	},
	
	getImagesFromContainer: function(objContainer) {
		this.arrImgList = $A( objContainer.getElementsByClassName('imgThumb') );
		this.arrImgList.each(function(imgItem){
			imgItem.setOpacity(0.6);

			Event.observe(imgItem, 'click', function( e ){
				e.stop();
				objGallery.doShowImage(imgItem);
			});

			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 doGalleryLeft() {
//	alert(objGallery.sliderIndex);
	if ( objGallery.sliderIndex < 0 ) {
		new Effect.Move(objGallery.strContainer, { 
			x: 165, 
			y: 0, 
			duration: 0.5,
			mode: 'relative', 
			beforeStart:function(){ 
				Event.stopObserving($('arrowLeft'), 'click', doGalleryLeft);
				objGallery.sliderIndex += 1;
			},
			afterFinish:function(){
				Event.observe($('arrowLeft'), 'click', doGalleryLeft);
			}
		});
	}
	
	return false;
}

function doGalleryRight(strContainer) {
if ( objGallery.sliderIndex > (-objGallery.imagesCount + 3) ) {
		new Effect.Move (objGallery.strContainer, { 
			x: -165, 
			y: 0,
			duration: 0.5,
			mode: 'relative', 
			beforeStart:function(){
				Event.stopObserving($('arrowRight'), 'click', doGalleryRight);
				objGallery.sliderIndex -= 1;
			},
			afterFinish:function(){
				Event.observe($('arrowRight'), 'click', doGalleryRight);
			}
		});
	}
	return false;
}
