jQuery.fn.flide = function(options) {
	
	var defaults = {
		speed		 	: 400,
		ease_type	: 'easeInOutExpo',
		width		 	: 500,
		height	 	: 300,
		active	 	: 'active',
		clickable	: true
	};
	
	// From the jquery.easing plugin
	// http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js
	$.fn.extend( $.easing, {
		easeInOutExpo: function (x, t, b, c, d) {
			if (t===0) return b;
			if (t===d) return b+c;
			if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
			return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
		}
	});
	
	return this.each(function(){
		
		var s = $.extend({}, defaults, options);
		
		var list = $(this),
				slides = $('li', this).length,
				flide_w = s.width * slides,
				current_slide = 0;
		
		list.wrap('<div class="flide_wrapper" style="width: ' + s.width + 'px; height: ' + s.height + 'px;">').css('width', flide_w).after('<ul class="flide_nav" />').children('li').each(function(i) {
			var nav_class = i + 1 === slides ? ' class="last"' : '';
			$(this).parent().siblings('.flide_nav').append('<li' + nav_class + '><a href="#">'+ ( i + 1 ) + '</a></li>');
		}).css({
			width		: s.width,
			height	: s.height
		});
		list.after('<span class="ir prev">«</span><span class="ir next">»</span>');
		
		var nav_items = $(this).siblings('.flide_nav').children('li');
		
		nav_items.eq(0).addClass(s.active);
		
		if ( s.clickable ) {
			list.click(function(e) {
				e.preventDefault();
				current_slide++;
				slide();
			});
		}
		
		$('.flide_wrapper .prev, .flide_wrapper .next').live('click', function(e) {
			e.preventDefault();
			if ( $(this).hasClass('next') ) {
				current_slide++;
			} else {
				current_slide--;
			}
			slide();
		});
		
		function slide() {
			current_slide = current_slide === slides ? 0 : current_slide;
			current_slide = current_slide === -1 ? slides-1 : current_slide;
			nav_items.removeClass(s.active).eq(current_slide).addClass(s.active);
			list.animate({
				left: - ( s.width * current_slide )
			}, {
				duration	: s.speed,
				queue			: false,
				easing		: s.ease_type
			});
		}
		
		$('.flide_nav li', this.parentNode).live('click', function(e) {
			e.preventDefault();
			current_slide = $(this).prevAll().length;
			slide();
		});
		
		function check_key(e){
			switch (e.keyCode) {
				case 37: // Left arrow key
					current_slide--;
					slide();
				break;
				case 39: // Right arrow key
					current_slide++;
					slide();
				break; 
			}
		}
		
		$(document).keydown(check_key);
		
	});
};
