var lastHint = null;
var fto = null; 

function hide2(){
	if(fto != null){
		lastHint.fade({ duration: 0.5});
		clearTimeout(fto);
		fto = null;
	}
}

function hideDescr(){
//	fto = setTimeout(hide2, 2000);
}

function showDescr(){
	if(fto != null){
		clearTimeout(fto);
	}
	this.up("a").adjacent("div").each(function(el){
		if(lastHint && (lastHint != el))
			lastHint.fade({ duration: 0.5});
		el.appear({ duration: 0.5});
		lastHint = el;
	})};

var SliderClass = Class.create();
SliderClass.prototype = {
	initialize: function(elem){
		this.pageSize = 5;
		this.sliderElements = [];
		this.currentPage = 1;
		this.numElems = 0;
		this.container = elem;
		this.prevButton = null;
		this.nextButton = null;
		var els = this.container.select('a[class=prev_button]');
		if(els.length == 1){
			this.prevButton = els[0];
		}
		var els = this.container.select('a[class=next_button]');
		if(els.length == 1){
			this.nextButton = els[0];
		}
		
		var els = this.container.select('ul li');
		this.numElems = els.length;
		for(i=0; i<els.length; i++){
			this.sliderElements.push(els[i]);
		}
		this.addObservers();
		this.hideButtons();
		this.initItems();
		this.showItems();

		var els = elem.select('ul li a img');
		for(i=0; i<els.length; i++){
			els[i].observe('mouseover', showDescr);
			//els[i].observe('click', hideDescr);
		}
	},

	hideButtons: function(){
		if(this.numElems <= this.pageSize){
			this.prevButton.hide();
			this.nextButton.hide();
		} else {
			if(this.currentPage == 1){
				this.prevButton.hide();
			} else {
				this.prevButton.show();
			}
			if(this.currentPage * this.pageSize < this.numElems){
//			if(this.currentPage > (this.numElems / this.pageSize)){
				this.nextButton.show();
			} else {
				this.nextButton.hide();
			}
		}
	},
	
	addObservers: function(){
		this.nextButton.observe('click', this.nextPage.bind(this));
		this.prevButton.observe('click', this.prevPage.bind(this));
	},

	nextPage: function(){
		if(this.currentPage > (this.numElems / this.pageSize)){
			return;
		}
		this.currentPage += 1;
		this.sliderElements.each(function(el){
			if(el.visible()){
				el.fade({ duration: 0.5});
				}
			});
		setTimeout(this.showItems.bind(this), 1500);
		this.hideButtons();
	},
	
	initItems: function(){
		for(i=0; i<this.numElems; i++){
			this.sliderElements[i].hide();
		}
	},
	
	showItems: function(){
		var first = (this.currentPage - 1) * this.pageSize;
		var last = first + this.pageSize;
		if(last > this.numElems){
			last = this.numElems;
		}
		for(i=first; i<last; i++){
			this.sliderElements[i].appear({ duration: 0.5});
		}
	},
	
	prevPage: function(){
		if(this.currentPage == 1){
			return;
		}
		this.currentPage -= 1;
		this.sliderElements.each(function(el){
			if(el.visible()){
				el.fade({ duration: 0.5});
				}
			});
		setTimeout(this.showItems.bind(this), 1500);
		this.hideButtons();
	},
	
	showDescr: function(){
		this.adjacent("div").each(function(el){
			if(!el.visible())
				el.show
		});
	}
};

var Sliders = new Array();
function initSliders()
{
	var numSliders = 0;
	var id = 0;
/*	var sls = $("prodottiEditoriali").select('div[class=mySlider]');
	for (var i=0; i<sls.length; i++){
		Sliders.push(new SliderClass(sls[i]));
	}*/
	$("prodottiEditoriali").select('div[class=mySlider]').each(function(elem){
		Sliders.push(new SliderClass(elem));
	});
	$("prodottiEditoriali").observe('mouseover', hideDescr);
}

Event.observe(window, 'load', function(){
	initSliders();
	
});