var gallery = new Class({
	
	Implements: [Options, Events],
	
	options: {
		container: 'photoGallery',		
		direction: 'LTR',
		itemWidth: 57,
		fx: null,
		totalImages: 0,
		scrollTimeout: 3000,
		clickTimeout: 1500,
		timerScroll: null,
		timerClick: null,
		ulTags: null,
		liTags: null,
		previewContainer: null,
		scrollContainer: null,
		btnPrevious: null,
		btnNext: null,
		imageContainer: null	
	},
	
	initialize: function(options){		
		this.setOptions(options);
		this.initGallery();
		this.setAutoRun();
	},	
	
	initGallery: function(){
		var that = this;
		that.options.container = $(that.options.container);
		that.options.previewContainer = that.options.container.getElement('p.preview');
		that.options.scrollContainer = that.options.container.getElement('div.scrollContainer');
		that.options.btnPrevious	= that.options.container.getElement('p.btnPrev a');
		that.options.btnNext	= that.options.container.getElement('p.btnNext a');
		that.options.imageContainer = that.options.previewContainer.getElement('img');		
		if(!that.options.container || !that.options.previewContainer || !that.options.scrollContainer || !that.options.btnPrevious || !that.options.btnNext || !that.options.imageContainer){
			return;
		}
		that.options.ulTags = that.options.scrollContainer.getElement('ul');
		that.options.liTags = that.options.ulTags.getElements('li');
		that.options.totalImages = that.options.liTags.length;
		that.options.btnPrevious.enabled = true;
		that.options.btnPrevious.addEvent('click', function(e){
			if(e){
				e.stop();
			}			
			if(!that.options.btnPrevious.enabled){
				return;
			}
			$clear(that.options.timerScroll);
			$clear(that.options.timerClick);
			that.options.direction = 'LTR';
			that.toLi(--that.options.currentIndex);
			that.options.timerClick = window.setTimeout(function(){
				that.setAutoRun();
			}, that.options.clickTimeout);
		});
		that.options.btnNext.enabled = true;
		that.options.btnNext.addEvent('click', function(e){
			if(e){
				e.stop();
			}
			if(!that.options.btnNext.enabled){
				return;
			}
			$clear(that.options.timerScroll);
			$clear(that.options.timerClick);
			that.options.direction = 'LTR';			
			that.toLi(++that.options.currentIndex);
			that.options.timerClick = window.setTimeout(function(){
				that.setAutoRun();
			}, that.options.clickTimeout);
		});
		
		if(that.options.totalImages <= 3){
			that.options.btnNext.enabled = false;
			that.options.btnPrevious.enabled = false;
			that.options.btnPrevious.setStyle('opacity', '0.5');
			that.options.btnNext.setStyle('opacity', '0.5');
			that.options.ulTags.setStyle('width', that.options.itemWidth * 3);
			return;
		}	
		that.options.fx = new Fx.Scroll(that.options.scrollContainer, {
			link: 'ignore',						
			onStart: function(){
				if(that.lastActive){
					that.lastActive.removeClass('current');
				}
			},
			onComplete: function(){	
				if(that.options.currentIndex <= 1){
					that.options.currentIndex = 2 + that.options.totalImages - 1;					
					that.options.fx.set(that.options.itemWidth * that.options.currentIndex, 0);					
				}
				if(that.options.currentIndex > that.options.totalImages + 1){
					that.options.currentIndex = 2;						
					that.options.fx.set(that.options.itemWidth * that.options.currentIndex ,0);					
				}												
			}
		});			
		that.swapItem();
		that.options.ulTags = that.options.scrollContainer.getElement('ul');
		that.options.liTags = that.options.ulTags.getElements('li');		
		that.options.currentIndex = that.options.totalImages;
		that.options.fx.set(that.options.itemWidth * that.options.currentIndex, 0);					
		that.toLi(that.options.currentIndex);				
	},
	
	swapItem: function(){
		var that = this;
		that.options.liTags[0].clone().inject(that.options.ulTags, 'bottom');
		that.options.liTags[1].clone().inject(that.options.ulTags, 'bottom');
		that.options.liTags[2].clone().inject(that.options.ulTags, 'bottom');
		that.options.liTags[that.options.totalImages - 1].clone().inject(that.options.ulTags, 'top');		
		that.options.liTags[that.options.totalImages - 2].clone().inject(that.options.ulTags, 'top');		
		that.options.liTags[that.options.totalImages - 3].clone().inject(that.options.ulTags, 'top');		
	},
	
	toLi: function(index){
		var that = this;				
		that.options.fx.cancel().toElement(that.options.liTags[index]);			
		that.setPreview(index);	
		that.setOpacity(index);				
	},
			
	setPreview: function(index){
		var that = this;		
		var x = that.options.totalImages - that.options.currentIndex + 1;
		if(that.options.currentIndex == that.options.totalImages + 1){
			x = that.options.totalImages;	
		}
		else if(that.options.currentIndex == that.options.totalImages + 2){
			x = that.options.totalImages - 1;
		}		
		var y =	that.options.totalImages;
		var targetImage = that.options.liTags[index + 1].getElement('img');
		that.options.imageContainer.set('src', targetImage.get('src').replace('_thumb', '_large'));		
	},
		
	setOpacity: function(index){
		var that = this;
		if(that.lastActive){
			that.lastActive.removeClass('current');
		}
		that.options.liTags.each(function(li, pos){
			li.removeClass('current');
		});
		that.options.liTags.each(function(li, pos){			
			if(index == that.options.totalImages + 2){
				that.options.liTags[3].set('opacity', 1);
				that.lastActive = that.options.liTags[3];
				that.lastActive.addClass('current');
			}
			else if(index == 1){
				that.options.liTags[that.options.totalImages + 2].set('opacity', 1);
				that.lastActive = that.options.liTags[that.options.totalImages + 2];
				that.lastActive.addClass('current');
			}
			if(index == pos - 1){				
				li.set('opacity', 1);
				that.lastActive = li;
				that.lastActive.addClass('current');
			}			
		});		
	},
		
	setAutoRun: function(){
		var that = this;
		var fn = $empty;
		if(that.options.direction == 'LTR'){
			fn = that.options.btnNext;	
		}
		else{
			fn = that.options.btnPrevious;
		}
		that.options.timerScroll = window.setInterval(function(){
			fn.fireEvent('click');
		}, that.options.scrollTimeout);
	}
});

window.addEvent('domready', function(){
	new gallery();		
});
