var PreviewSlider = new Class({
	initialize: function(el,options){
	
		this.speed = options.speed?options.speed: 1000;
		this.direction = options.direction?options.direction: 'vertical';
		this.minElements = options.minElements?options.minElements: 4;

		//Get Elements
		this.el = el;
		this.ul = this.el.getFirst('.bvf-catalogue-bookslideshow-container'); 
		this.items = this.el.getElements('.bvf-catalogue-bookslideshow-item');
		this.prev = this.el.getFirst('.scrollprev');
		this.next = this.el.getFirst('.scrollnext');

		if(this.items.length>=this.minElements){
			//set size of container
			var w = 0;
			var h = 0;
			
			
			if(this.direction.toLowerCase()=='horizontal') {
				h = this.ul.getSize().y;
	
				var minW = 0;
				this.items.each(function(li,index) {
					w += li.getSize().x;
					minW = (minW<w)?w:minW;
				});
				w = this.ul.getSize().x + minW;
			} else {
				w = this.ul.getSize().x;
				this.items.each(function(li,index) {
					h += li.getSize().y;
				});
			}
	
			//Set style of container
			this.ul.setStyles({
				position: 'absolute',
				top: 0,
				left: 0,
				width: w,
				height: h
			});
	
			//initiate FX
			this.fxNext = new Fx.Morph(
					this.ul, 
					{
						duration:this.speed,
						onComplete:function() {
							var i = (this.current==0)?this.items.length:this.current;
							this.items[i-1].injectInside(this.ul);
							this.ul.setStyles({
								left:0,
								top:0
							});
							this.scrolling = false;
					}.bind(this)
				});
	
			this.fxPrev = new Fx.Morph(
					this.ul,
					{
						duration:this.speed,
						onComplete:function() {
							this.scrolling = false;
						}.bind(this)
					}
				);
	
			//initiate vars
			this.current = 0;
			this.scrolling = false;
			
			//Setup Functions
			this.prev.addEvent('click',function(){this.scrollPrev()}.bind(this));
			this.next.addEvent('click',function(){this.scrollNext()}.bind(this));
		} else {
			this.prev.setStyles({
				display:'none'
			});
			this.next.setStyles({
				display:'none'
			});
		}
	},

	scrollPrev: function() {
		if(!this.scrolling){
			this.scrolling = true;

			this.current--;

			if (this.current < 0) {
				this.current = this.items.length-1;
			}

			var pos = this.items[this.current];

			pos.inject(this.ul,'top');

			this.ul.setStyles({
				left:-pos.getSize().x,
				top:0
			});

			this.fxPrev.start({
				top: pos.offsetTop,
				left: pos.offsetLeft
			});		
		}	
	},

	scrollNext: function() {
		if(!this.scrolling){
			this.scrolling = true;

			this.current++;
			if (this.current >= this.items.length) this.current = 0;

			var pos = this.items[this.current];

			this.fxNext.start({
				top: -pos.offsetTop,
				left: -pos.offsetLeft
			});
		}
	}
});

var Slideshows = [];

window.addEvent('domready', function(){
	//Get all Slideshows
	var items = $$('.bvf-catalogue-bookslideshow');
	
	//Setup Slideshows
	items.each(function(element, index) {
		Slideshows.push(new PreviewSlider(element,{speed:2000,direction:'horizontal',minElements:5}));
	});

});




