(function($) {
	$.fn.eside_slide_2 = function(options) {
		var 
			defaults = {
				effect_duration: 700,
				button_prev: 'prev',
				button_next: 'next',
				controls_box_id: 'eside_slide_1_controls',
				txt_of: 'of',
				play: 0,
				timeout: 5000
			},
			options = $.extend(defaults, options),
			list_container = this.find('.pages').children().filter('ul'),
			list = list_container.children(),
			pages = list.length,
			in_effect = false,
			page_width = list.first().width(),
			width = pages * page_width,
			current = 0,
			interval,
			stop_on_click = 0;
		
		list_container.width(width);
		if(!list.length) return;
		
		function change_slide(current, prev) {
			in_effect = true;
			
			if(prev) {
				current -= 1;
				if(current < 0) {
					in_effect = false;
					return 0;
				}
			} else {
				current += 1;
				if(current == pages) {
					in_effect = false;
					return pages - 1;
				}	
			}
			
			var left = -current * page_width;
			
			list_container.animate({
				left: left
			}, options.effect_duration, function() {
				in_effect = false;
			});
			
			return current;
		}
		
		this.append(
			'<div id="' + options.controls_box_id + '">' +
				'<a href="javascript:void(0)" class="eside_slide_2_prev">' + options.button_prev + '</a>' +
				'&nbsp;&nbsp;&nbsp;<span class="eside_slide_2_current">' + (current + 1) + '</span> ' + options.txt_of + ' ' + pages + '&nbsp;&nbsp;&nbsp;' + 
				'<a href="javascript:void(0)" class="eside_slide_2_next">' + options.button_next + '</a>' +
			'</div>'
		);
		
		var button_next = this.find('.eside_slide_2_next');
		var button_prev = this.find('.eside_slide_2_prev');
		var span_current = this.find('.eside_slide_2_current');
		
		button_next.click(function(event, auto) {
			if(!auto && interval) clearInterval(interval);
			
			if(!in_effect) current = change_slide(current);
			span_current.html(current + 1);
		})
		
		button_prev.click(function(event) {
			if(!in_effect) current = change_slide(current, true);
			span_current.html(current + 1);
		});
		
		if(options.play && list.length > 1) {
			interval = setInterval(function() {
				if(current == list.length - 1) {
					current = 0;
					list_container.animate({
						left: 0
					}, options.effect_duration, function() {
						in_effect = false;
					});
					
					span_current.html(current + 1);
				} else button_next.trigger('click', 1);
			}, options.timeout);
		}
	}
})(jQuery);
