(function($) {
	$.fn.eside_slide_1 = function(options) {
		var 
			defaults = {
				play: 1,
				effect_duration: 700,
				timeout: 5000,
				button_play: 'play',
				button_pause: 'pause',
				button_prev: 'prev',
				button_next: 'next',
				controls: true,
				controls_box_id: 'eside_slide_1_controls',
				bianco: 1
			},
			options = $.extend(defaults, options),
			interval,
			list = this.children().filter('ul').children(),
			current = list.first(),
			in_effect = false;
			
		$(this).find('li').hide();
		
		if(options.bianco) options.effect_duration /= 2;
		
		function change_slide(el, prev) {
			if(!options.bianco) return change_slide_nobianco(el, prev);
			
			in_effect = true;
			
			var next;
			
			if(prev) {
				next = el.prev();
				if(!next.length) next = list.last();
			} else {
				next = el.next()
				if(!next.length) next = list.first();
			}
			
			el.fadeOut(options.effect_duration, function() {
				next.fadeIn(options.effect_duration, function() {
					in_effect = false;
				});
			});
			
			return next;
		}
		
		function change_slide_nobianco(el, prev) {
			in_effect = true;
			
			var next;
			
			if(prev) {
				next = el.prev();
				if(next.length) {
					next.show();
					el.fadeOut(options.effect_duration, function() {
						in_effect = false;
						el.hide();
					});
				} else {
					next = list.last();
					next.fadeIn(options.effect_duration, function() {
						in_effect = false;
						el.hide();
					});
				}
			} else {
				next = el.next()
				if(next.length) {
					next.fadeIn(options.effect_duration, function() {
						in_effect = false;
						el.hide();
					});
				} else {
					next = list.first();
					next.show();
					el.fadeOut(options.effect_duration, function() {
						in_effect = false;
						el.hide();
					});
				}
			}
			
			/*next.fadeIn(options.effect_duration, function() {
				in_effect = false;
				el.hide();
			});*/
			
			return next;
		}
		
		function play() {
			if(!interval) interval = setInterval(function() {
				current = change_slide(current);
			}, options.timeout);
		}
		
		function pause() {
			clearInterval(interval);
			interval = undefined;
		}
		
		if(options.controls) {
			this.append(
				'<div id="' + options.controls_box_id + '">' +
					'<a href="javascript:void(0)" class="eside_slide_1_prev">' + options.button_prev + '</a>' +
					'<a href="javascript:void(0)" class="eside_slide_1_pause">' + options.button_pause + '</a>' +
					'<a href="javascript:void(0)" class="eside_slide_1_play">' + options.button_play + '</a>' +
					'<a href="javascript:void(0)" class="eside_slide_1_next">' + options.button_next + '</a>' +
				'</div>'
			);
			
			var button_play = this.find('.eside_slide_1_play');
			var button_pause = this.find('.eside_slide_1_pause');
			var button_next = this.find('.eside_slide_1_next');
			var button_prev = this.find('.eside_slide_1_prev');
			
			button_play.click(function() {
				play();
			});
			
			button_pause.click(function() {
				pause();
			});
			
			button_next.click(function() {
				if(!in_effect) {
					pause();
					current = change_slide(current);
				}
			})
			
			button_prev.click(function() {
				if(!in_effect) {
					pause();
					current = change_slide(current, true);
				}
			});
		}
		
		current.fadeIn(options.effect_duration);
		
		if(options.play && list.length > 1) {
			play();
		}
	}
})(jQuery);
