/////////////tabs /////////////
var ShowHideTab = new Class({
	
	initialize: function(container, options){
		this.container = container;
		this.setOptions(options);
		this.initTab();
	},
	
	initTab: function(){
		var that = this;
		if(!$(that.container)) return;
		var tabsContainer = $(that.container);
		var items = tabsContainer.getElements('.tabs li');
		var contents = tabsContainer.getElements('div.tblContent');	
		var activeItem = tabsContainer.getElement('li.current');	
		var activeContent = tabsContainer.getElement('div');		
		//var activeContent = tabsContainer.getElement('div:not(.hidden)');
		//var activeContent = contents.filter(function(){return !this.hasClass('hidden');})
		
		items.each(function(item, index){
			item.addEvent('click', function(evt){
				new Event(evt).stop();
				if(activeItem != this){
					// remove current active item
					if(activeItem){
						var content = activeItem.get('html');					
						activeItem.removeClass('current');
						activeContent.addClass('hidden');
						
					};
					// set active					
					this.addClass('current');
					activeItem = this;
					activeContent = contents[index];
					activeContent.removeClass('hidden');
				}			
			});
		});	
	}	
});
ShowHideTab.implement(new Chain, new Events, new Options);


//////////////////
var Gallery = new Class({
	
	Implements: [Options, Events],
	
	options: {
		container: 'galleryCont',		
		direction: 'LTR',
		itemWidth: 154,
		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.listProducts2');
		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 = 4;	
					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 = 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 intiProductTooltip(){
	var content = $('content');
	if(!content) return;
	
	content.getElements('.listImg').each(function(prodlist){
		var products = prodlist.getChildren();
		if(products.length){
			var tooltip = new Tips();
			products.each(function(prod){
				var target = prod.getElement('p a');
				target.store('tip:text', prod.getElement('.tooltip').removeClass('hidden').dispose());
				
				tooltip.attach(target);
			});
		}
	});
};
//////////////
function initFadeListImages(){
var listImg = $('fadeImg'); 
if(!listImg) return;
var imgs = listImg.getElements('img');
var imgCur = 0;
imgs[0].setStyle('opacity', 1);
imgs[0].fx = new Fx.Tween(imgs[0], {
duration: 3000
});
if(imgs.length > 1){
for(var i = 1; i < imgs.length; i++){
imgs[i].setStyle('opacity', 0).removeClass('hidden');
imgs[i].fx = new Fx.Tween(imgs[i], {
duration: 3000
});
}
var lsTimer = setInterval(function(){ 
imgs[imgCur].fx.cancel().start('opacity',[1,0]);
imgCur = (imgCur < imgs.length - 1)?(imgCur + 1):0;
imgs[imgCur].fx.cancel().start('opacity',[0,1]);
}, 4500); 
} 
}
/////////////

window.addEvent('domready', function(){
	new initFadeListImages();
	new Gallery();
	new ShowHideTab('tabContent');
	intiProductTooltip();
});
