jQuery.fn.fadeGallery = function(options){
	// default options	
	var options = jQuery.extend({
		gallery : '.gallery',
		prev : 'a.prev, a.btn-prev, a.link-prev',
		next : 'a.next, a.btn-next, a.link-next',
		play : 'a.play',
		pause : 'a.pause, a.stop',
		thumbnails : '.thumbnails',
		generate_thumbnails : false,
		autoheight:false,
		IE:false,
		autoplay : 4000,
		duration : 500
	}, options);
	
	return this.each(function(){
		var holder = $(this),
			gallery = $(options.gallery, holder),
			prev = $(options.prev, holder),
			next = $(options.next, holder),
			play = $(options.play, holder),
			pause = $(options.pause, holder),
			autoplay = options.autoplay,
			thumbnails = $(options.thumbnails, holder),
			slides = gallery.find('> li'),
			thumbs = thumbnails.find('a'),
			autoheight = options.autoheight,
			IE = options.IE,
			timer,
			current;
		// auto numbers
		if (options.generate_thumbnails) {
			thumbnails.html('');
			var i = 0;
			while (i < slides.length) {
				thumbnails.append('<li><a href="#">' + (i+1) + '</a></li>');
				i++;
			}
			var thumbs = thumbnails.find('a');
		}
		
		// set active slide
		if (slides.index(slides.filter('.active')) == -1) {
			current = 0;
			slides.eq(0).addClass('active');
			if (options.thumbnails) thumbs.eq(0).addClass('active');
		} else {
			current = slides.index(slides.filter('.active'));
			if (options.thumbnails) thumbs.eq(current).addClass('active');
		}
		
		// init default css styles
		if (autoheight) gallery.css({height: slides.eq(current).height()});
		slides.css({
			opacity:0,
			display:'none',
			position:'absolute',
			top:0,
			left:0
		});
		slides.eq(current).css({
			opacity:1,
			display:'block',
			position:'relative'
		});
		if (IE && $.browser.msie) {
			slides.css({opacity:'auto'});
		}
		
		// prev/next element functions
		function nextEl() {
			var tmp = current;
			if (tmp < slides.length-1) tmp++;
			else tmp = 0;
			return tmp;
		}
		
		function prevEl() {
			var tmp = current;
			if (tmp > 0) tmp--;
			else tmp = slides.length-1;
			return tmp;
		}
		
		// main animation
		function rotate(next_ind) {
			if (timer) clearTimeout(timer);
			if (IE && $.browser.msie) {
				slides.eq(current).stop().removeClass('active').css({
					position:'absolute',
					zIndex:0,
					display:'none'
				})
				slides.eq(next_ind).stop().addClass('active').css({
					position:'relative',
					zIndex:1,
					display:'block'
				});
			} else {
				slides.eq(current).stop().removeClass('active').animate({
					opacity:0
				},options.duration, function(){
					$(this).css({
						display:'none'
					})
				}).css({
					position:'absolute',
					zIndex:0
				})
				slides.eq(next_ind).stop().addClass('active').css({
					display:'block'
				}).animate({
					opacity:1
				},options.duration).css({
					position:'relative',
					zIndex:1
				});
			}
			if (autoheight) {
				if (gallery.css('height') == 'auto') {
					gallery.stop().css({
						height: slides.eq(current).height()
					}).animate({
						height: slides.eq(next_ind).height()
					}, options.duration , function(){
						$(this).css({
							height:'auto'
						});
					});
				} else {
					gallery.stop().animate({
						height: slides.eq(next_ind).height()
					}, options.duration , function(){
						$(this).css({
							height:'auto'
						});
					});
				}
			}
				
			if (options.thumbnails) {
				thumbs.eq(current).removeClass('active');
				thumbs.eq(next_ind).addClass('active');
			}
			current = next_ind;
			
			if (timer) {
				timer = setTimeout(function(){
					rotate(nextEl());
				}, autoplay);
			}
		}
	
		// events
		next.click(function(){
			rotate(nextEl());
			return false;
		});
		prev.click(function(){
			rotate(prevEl());
			return false;
		});
		play.click(function(){
			if (!timer) {
				timer = setTimeout(function(){
					rotate(nextEl());
				}, autoplay);
			} 
			return false;
		});
		pause.click(function(){
			if (timer) {
				clearTimeout(timer);
				timer = false;
			}
			return false;
		});
		
		// thumbnails
		thumbs.each(function(i){
			$(this).click(function(){
				clearTimeout(timer);
				if (current !== i) {
					rotate(i)
				} else {
					if (autoplay) {
						timer = setTimeout(function(){
							rotate(nextEl());
						}, autoplay);
					}
				}
				return false;
			})
		});
		
		// autoplay
		if (autoplay) {
			timer = setTimeout(function(){
				rotate(nextEl());
			},autoplay);
		}
	});
}
