var gallery = new Class({
	
	Implements: [Options, Events],
	
	options: {
		container: 'gallery',		
		direction: 'LTR',
		itemWidth: 152,
		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 > 4){
			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.scrollContainer');
		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 < 4){
			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 * 4);	
			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.currentIndex = 4;
		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[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');		
	},
	
	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);
	}
});

function initFadeAds(container){
	var container = $(container);
	if(!container) return;
	var adsContents = container.getElements('.adsContents');	
	if(!adsContents.length) return;
	var bgCurrent = 0;
	var objTweenBg = [];
	adsContents.each(function(item, index){
		if(index)
			item.setStyle('display', 'none').removeClass('hidden');
		objTweenBg[index] = new Fx.Tween(item,{
			duration: 8000
		});					
	});				
	var bgTimer = setInterval(function(){			
		objTweenBg[bgCurrent].cancel().set('display', 'none');
		bgCurrent = (bgCurrent < adsContents.length - 1)?(bgCurrent + 1):0;
		objTweenBg[bgCurrent].cancel().set('display','block');
	}, 8000);	
}

window.addEvent('domready', function(){
	initFadeAds('banner');
	initFadeAds('context');
	new gallery();		
	new gallery({container: 'gallery2'});		
});