
/**************************************************************

	Script		: SlideShow
	Version		: 1.3
	Authors		: Samuel Birch
	Desc		: 
	Licence		: Open Source MIT Licence

**************************************************************/

var SlideShow = new Class({
	
	getOptions: function(){
		return {
			duration: 500,
			transition: Fx.Transitions.linear
		};
	},

	initialize: function(container, images){
		this.setOptions(this.getOptions());
		
		this.container = $(container);
		this.container.setStyles({
			position: 'relative',
			overflow: 'hidden'
		});
		
		
		
		var imageList = [];
		$$('.'+images).each(function(el){
			imageList.push(el.href);
			el.style.display = 'none';
		},this);
		this.images = imageList;
		
		if (this.images.length < 2){
			$('slidbtnprev').style.display = 'none';
			$('slidbtnnext').style.display = 'none';
		}
			
		
		
		this.movingdiv = new Element('div').setStyles({
			position: 'absolute',
			top: 0,
			left: 0,
			zIndex: 3,
			//display: 'none',
			width: (this.container.getStyle('width').toInt()*2),
			height: this.container.getStyle('height')
		}).inject(this.container);
		
		this.oldImage = new Element('div').setStyles({
			position: 'absolute',
			overflow: 'hidden',
			top: 0,
			left: 0,
			opacity: 1,
			width: this.container.getStyle('width'),
			height: this.container.getStyle('height'),
			'background-image': 'url('+this.images[0]+')',
			'background-position': '50% 50%',
			'background-repeat': 'no-repeat'
		}).inject(this.movingdiv);
		
		this.newImage = new Element('div').setStyles({
			position: 'absolute',
			overflow: 'hidden',
			top: 0,
			left: this.container.getStyle('width'),
			opacity: 1,
			width: this.container.getStyle('width'),
			height: this.container.getStyle('height'),
			'background-image': 'url('+this.images[1]+')',
			'background-position': '50% 50%',
			'background-repeat': 'no-repeat'
		}).inject(this.movingdiv);
		
		
		
		this.canplay = true;
		this.timer = 0;
		this.image = 0;
		this.imageLoaded = 0;
		this.stopped = true;
		this.started = false;
		this.animating = false;
	},
	
	next: function(wait){
	
		if ( !this.canplay ) return;
		this.movingdiv.setStyles({
			left : '0px'
		});
		
		this.oldImage.setStyles({
			'background-image': 'url('+this.images[this.image]+')'
		});
		
		this.image++;
		if ( this.image > this.images.length-1 ){
			this.image = 0;
		}
		
		this.newImage.setStyles({
			'background-image': 'url('+this.images[this.image]+')'
		});
		
		this.resetAnimation.delay(this.options.duration+100,this);
		
		this.canplay = false;
		new Fx.Morph(this.movingdiv, {
			duration: this.options.duration,
			transition: this.options.transition
		}).start({
			top: [0,0],
			left: [0,-300]
		});
		
		return false;
	},
	
	previous: function(){
		
		if ( !this.canplay ) return;
		this.movingdiv.setStyles({
			left : '-300px'
		});
		
		this.newImage.setStyles({
			'background-image': 'url('+this.images[this.image]+')'
		});
		
		this.image--;
		if ( this.image < 0 ){
			this.image = this.images.length-1;
		}
		
		this.oldImage.setStyles({
			'background-image': 'url('+this.images[this.image]+')'
		});
		
		this.resetAnimation.delay(this.options.duration+100,this);
		
		this.canplay = false;
		new Fx.Morph(this.movingdiv, {
			duration: this.options.duration,
			transition: this.options.transition
		}).start({
			top: [0,0],
			left: [-300,0]
		});
		return false;
	},
	
	resetAnimation: function(){
		this.canplay = true;
	}
	
});
SlideShow.implement(new Options);
SlideShow.implement(new Events);


/*************************************************************/


