(function($) {
	$.fn.slideshow = function(options) {
		$.fn.slideshow.defaults = {
			fadeout:2000,
			fadein:2000,
			sleep:4000
		};
		var self = this;
		var config = $.extend(true,$.fn.slideshow.defaults,options);
		var item = 0;
		var items = $(this).children('div').children('ul').children('li');
		var cont = $(this);
		var count = items.length-1;
		var hovered = false;
		var wwidth = parseInt(items.css('width'));

		self.init = function() {
			//$(this).children('ul').width($(this).width()).height(($(this).height()+1));
			//items.width($(this).width()).height(($(this).height()+1));
			cont.children('div').children('ul').width(items.length*wwidth);
			$(this).children('a.slide_left').click(function() { self.prevImage(); });
			$(this).children('a.slide_right').click(function() { self.nextImage(); });
			$(this).hover(function(e) {
				hovered = true;
			},
			function() { 
				hovered = false;
				$(this).children('a.slide_left').hide();
				$(this).children('a.slide_right').hide();
			});
			$(this).mousemove(function(e) {
				var off = $(this).offset();
				if(e.pageX-off.left <= (wwidth/2)) {
					$(this).children('a.slide_left').show();
				} else {
					$(this).children('a.slide_left').hide();
				}
				
				if(e.pageX-off.left >= (wwidth/2)) {
					$(this).children('a.slide_right').show();
				} else {
					$(this).children('a.slide_right').hide();
				}
			});
			setTimeout(self.changeitem,config.sleep);
		};
		
		self.prevImage = function() {
			clearTimeout();
			item--;
			if(item < 0) { item = count; cont.children('div').scrollLeft((items.length*wwidth)-(wwidth));  } else { self.goTo(item); }
		};	
		
		self.nextImage = function() {
			clearTimeout();
			item++;
			if(item > count) {  item = 0; cont.children('div').scrollLeft('0px'); } else { self.goTo(item); }
		};
		
		self.goTo = function(i) {
			cont.children('div').scrollTo((i*wwidth)+'px',{duration:800,axis:'x'});
			
		};
		
		self.changeitem = function() {
			if(hovered === false) {
				clearTimeout();
				item++;
				if(item > count) {  item = 0; cont.children('div').scrollLeft('0px'); } else { self.goTo(item); }
				self.goTo(item);
				//$(items[item]).fadeIn(config.slidein,function() {
				setTimeout(self.changeitem,config.sleep);
				//});
			} else {
				setTimeout(self.changeitem,config.sleep);
			}
			
		};
		if(count>=0) {
			self.init();
		}
		
	};
})(jQuery);
