var gallery = new Class({
	
	Implements: [Options, Events],
	
	options: {
		container: 'gallery',		
		direction: 'LTR',
		itemWidth: 115,
		fx: null,		
		totalImages: 0,
		scrollTimeout: 2000,
		clickTimeout: 1500,
		timerScroll: null,
		timerClick: null,
		ulTags: null,
		liTags: null,
		scrollContainer: null,
		btnPrevious: null,
		btnNext: null		
	},
	
	initialize: function(options){		
		this.setOptions(options);
		this.initGallery();	
		if(this.options.totalImages >= 8){
			this.setAutoRun();
		}
	},	
	
	initGallery: function(){
		var that = this;
		that.options.container = $(that.options.container);
		if(!that.options.container) return;
		that.options.scrollContainer = that.options.container.getElement('div.listLogo');
		that.options.btnPrevious	= that.options.container.getElement('p.btnPrev');
		that.options.btnNext	= that.options.container.getElement('p.btnNext');		
		if(!that.options.container || !that.options.scrollContainer || !that.options.btnPrevious || !that.options.btnNext){
			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;
			}
			window.clearInterval(that.options.timerScroll);
			window.clearTimeout(that.options.timerClick);
			that.options.direction = 'RTL';
			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;
			}
			window.clearInterval(that.options.timerScroll);
			window.clearTimeout(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 < 8){
			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 * 8);	
			return;
		}	
		that.options.fx = new Fx.Scroll(that.options.scrollContainer, {
			link: 'ignore',					
			onComplete: function(){		
				if(that.options.currentIndex <= 1){					
					that.options.currentIndex = that.options.totalImages + 1;
					that.options.fx.set(that.options.itemWidth * that.options.currentIndex, 0);					
				}
				if(that.options.currentIndex > that.options.totalImages + 2){				
					that.options.currentIndex = 3;	
					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.liTags.each(function(li){
			//li.getElement('p').setOpacity(0.6);
		});
		
		that.options.currentIndex = 5
		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[3].clone().inject(that.options.ulTags, 'bottom');		
		that.options.liTags[4].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');				
		that.options.liTags[that.options.totalImages - 4].clone().inject(that.options.ulTags, 'top');				
		that.options.liTags[that.options.totalImages - 5].clone().inject(that.options.ulTags, 'top');				
	},
	
	toLi: function(index){
		var that = this;				
		that.options.fx.toElement(that.options.liTags[index]);			
	},
		
	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();
	new imagePreset();	
});
