	isMobile = false;
	var useragent = navigator.userAgent;
    useragent = useragent.toLowerCase();

 
    if (useragent.indexOf('iphone') != -1 || useragent.indexOf('symbianos') != -1 || useragent.indexOf('ipad') != -1 || useragent.indexOf('ipod') != -1 || useragent.indexOf('android') != -1 || useragent.indexOf('blackberry') != -1 || useragent.indexOf('samsung') != -1 || useragent.indexOf('nokia') != -1 || useragent.indexOf('windows ce') != -1 || useragent.indexOf('sonyericsson') != -1 || useragent.indexOf('webos') != -1 || useragent.indexOf('wap') != -1 || useragent.indexOf('motor') != -1 || useragent.indexOf('symbian') != -1 ) {
			isMobile = true;
		}
	else{
		
	}



var SlideShow = new Class({
	Implements: [Options, Events],
	
	options: {
		debug: false,
		random: false,
		container: false,
		progressBar: false,
		rotationSpeed: 5000,
		slideContent: false,
		type: 'fade',
		fxSpeed: 2000,
		fxTransition: Fx.Transitions.Bounce.easeInOut,
		nextElement: null,
		prevElement: null,
		listElement: null
	},
	
	slides: [],
	current: null,
	isSliding:true,
	
	initialize: function(options){
		this.setOptions(options);
		this.getData();
		
		// Check if list is enabled
		if(this.options.listElement){
			this.populateList(this.options.listElement);
		}
		
		this.attachEvents();
		
		// Load first slide
		var firstSlideIndex = this.options.random ? Number.random(0, this.slides.length-1) : 0;
		this.options.container.setStyles({
			'display': 'block',
			'opacity': 0
		});
		this.options.container.fade('in');
		this.rotateSlide(this.slides[firstSlideIndex], false, false);
	},
	
	attachEvents: function(){
		var nextEl = this.options.nextElement;
		var prevEl = this.options.prevElement;
		var listEl = this.options.listElement;
		
		
		if(isMobile){
			this.options.container.addEvent('swipe',function(event){
				if(event.direction == 'left' && nextEl){
					var next = this.getNext();
					clearInterval(this.timer)
					this.rotateSlide(next, false, true);
				}
				
				if(event.direction == 'right' && prevEl){
					var prev = this.getPrev();
					clearInterval(this.timer)
					this.rotateSlide(prev, false, true);
				}
				
			}.bind(this));
	}
		
		
		if(prevEl){
			prevEl.addEvent('click', function(e){
				var next = this.getNext();
				e.stop();
				if(this.isSliding){
				clearInterval(this.timer)
				this.rotateSlide(next, false ,true);
				}
			}.bind(this));
			
		
		}
		
		if(nextEl){

			nextEl.addEvent('click', function(e){
				var prev = this.getPrev();
				e.stop();
			if(this.isSliding){
				clearInterval(this.timer)
				this.rotateSlide(prev, false, true);
				}
			}.bind(this));
		}
		
		if(listEl){
			
		}
		
	},
	
	populateList: function(list){
		this.slides.each(function(slide, index){
			var el = new Element('li');
				el.set('html', slide.name);
				el.set('title', slide.name);
				el.addEvent('click', function(e){
					e.stop();
					
					if(slide !== this.current){
						clearInterval(this.timer)
						this.rotateSlide(slide, false, true);
					}
					
				}.bind(this));
				el.inject(list);
			
			if(!this.options.random && index === 0){
				el.addClass('active');
			}
			
			slide.listItemEl = el;
		}, this);
	},
	
	selectListItem: function(el){
		this.slides.each(function(slide){
			if(slide.listItemEl){
				slide.listItemEl.removeClass('active');
			}
		});
		
		el.addClass('active');
	},
	
	getNext: function(){
		if( (this.current.ID+1) > (this.slides.length-1) )
			var next = this.slides[0];
		else
			var next = this.slides[this.current.ID+1];
		
		return next;
	},
	
	getPrev: function(){
		if( (this.current.ID-1) < 0 )
			var next = this.slides[this.slides.length-1];
		else
			var next = this.slides[this.current.ID-1];
		
		return next;
	},
	
	rotateSlide: function(slide, instant, isClick){

		if( this.options.debug ) console.log( '-- Starting --' );

		if( slide == null ){
			slide = this.slides[0];
			this.showSlide( slide, true);
		} 
		else if(isClick){
			this.showSlide( slide, false, true);
		}
		else {
			this.showSlide( slide );
		}
		
		if(this.options.progressBar){
			var myLoadingBar = this.Loading(slide.element);
		}
		
		var next = this.getNext();
		var imageloading = false;
		
		this.timeleft = this.options.rotationSpeed;
		
		var tmpImage = new Image();
			tmpImage.src = next.filename;
			tmpImage.onload = function(){
				imageloading = true;
				if( this.options.debug ) console.log( next.filename +' loaded !' );
			}.bind(this);
		
		this.timer = (function(){
			if(this.timeleft > 0)
				this.timeleft = this.timeleft - 50;
			
			if(this.options.progressBar == true){
				var loading = 100 - ( (this.timeleft / this.options.rotationSpeed)*100 ).round();
				myLoadingBar.setStyle('width', loading+'%' );
			}
			
			if(this.timeleft <= 0 && this.options.debug) console.log( 'Timer Done, waiting for loading' );
			//if(imageloading == true && this.options.debug) console.log( 'Loading done, waiting for timer('+ this.timeleft +')' );
			
			
			if( this.timeleft <= 0 && imageloading == true )
			{
		
				
				if( this.options.debug ) console.log( 'Timer and Loading done' );
				
				clearInterval(this.timer);
				
				this.rotateSlide(next);
				
			}
			
		}).bind(this).periodical(50);
		
	},
	
	showSlide: function(slide,instant,isClick){
		var delay = this.options.rotationSpeed / 7;
					this.isSliding = false;
		if(isClick){
			delay = 0;
		}
		
		if( this.options.debug ) console.log( 'Showing slide #'+ slide.ID );
		this.options.listElement.show();
		if(this.current != null){
			 this.hideSlide( this.current );
			 	
		}
		
		this.createSlide(slide);
		this.current = slide;
		
		if(this.options.listElement){
			this.selectListItem(slide.listItemEl);
		}
		
		if(instant == true){
			slide.element.setStyle('opacity', 1);
		} else {
			if(this.options.type == 'fade') {
				(function(){
				slide.fx.start({
					'opacity': [0,1]
				});
				}).bind(this).delay(delay/1.5);
				
				(function(){
				this.isSliding = true;
				slide.fx2.start({
					'left':60,
					'opacity': 1
				});
				}).bind(this).delay(delay*1.5);
			} 
		}
		
	},
	
	hideSlide: function(slide){
		if( this.options.debug ) console.log( 'Hiding slide #'+ slide.ID );
		
				slide.fx2.start({
					'left':-500,
					'opacity': 1
				});
					
			
		(function(){ this.removeSlide( slide ) }).bind(this).delay(this.options.rotationSpeed/2);
		
	},
	
	removeSlide: function(slide){
		if( this.options.debug ) console.log( 'Disposing slide #'+ slide.ID );
		
		if( slide.element )
			slide.element.dispose();
	},
	
	createSlide: function(slide){
		if(!slide.element) {
			slide.element = new Element('div', { 'class': 'item' });
			
			slide.extras.each(function(extra){
				if(extra.link != ''){
					new Element('div', {
						'class': extra.classs,
						'html': extra.text,
						'href': extra.link
					}).inject(slide.element);
				} else {
					if(extra.text != ''){
						new Element('div', {
							'class': extra.classs,
							'html': extra.text
						}).inject(slide.element);
					}
				}
			});
			
		}
		
		if(!slide.fx){
			slide.fx = new Fx.Morph(slide.element, {
				duration: this.options.fxSpeed,
				link: 'chain',
				transition: this.options.fxTransition
			});
			
			slide.fx2 = new Fx.Morph(slide.element.getElements('.slidecontent')[0], {
				duration: this.options.fxSpeed,
				link: 'chain',
				transition: this.options.fxTransition
			});
			
		}
	
		if(this.options.slideContent){slide.element.getElement('.slidecontent').setStyles({'left':1500, 'opacity':0})}
		
		slide.element.setStyles({
			'opacity': 0,
			'background-image': 'url('+ slide.filename +')'
		});
		
		
		
		slide.element.inject( this.options.container );
	},
	
	Loading: function(slide){
		var loadings = slide.getChildren('.loading');
		
		if( loadings[0] )
			loadings[0].destroy();
		
		var wrap = new Element('div', { 'class': 'loading' }).inject( slide );
		//var number = new Element('div', { 'class': 'loading-number' }).inject(wrap);
		var bar = new Element('div', { 'class': 'loading-bar' }).inject(wrap);
		
		bar.setStyle('width', 0);
		
		return bar;
		
		/*
		this.LoadingBar.number.set('html', value);
		this.LoadingBar.bar.setStyle('width', value+'%');
		
		if( value == 100 )
			this.LoadingBar.bar.fade('out')
		else
			this.LoadingBar.bar.fade('in')
		*/
	},
	
	getData: function(){
		var items = this.options.container.getChildren('.item');
		
		items.each(function(item,index){
			var image = '';
			var extra = [];
			var divs = item.getChildren('div');
			
			divs.each(function(div){
				
				if( div.hasClass('image') ){
					image = div.get('html');
				} else if( div.get('rel') != '' ) {
					extra.include({
						'classs': div.get('class'),
						'text': div.get('html'),
						'link': div.get('rel')
					});
				} else {
					extra.include({
						'classs': div.get('class'),
						'text': div.get('html')
					});
				}
				
			});
			
			var object = {
							'ID': index,
							name: item.get('data-slideshow-name'),
							'filename': image,
							'extras': extra
						};
		
			this.slides.include(object);
			item.destroy();
		},this);
	}

});
