(function($){ 
	$.fn.lbgallery = function(options) {
		var $this, opts, options;

		options = $.extend({},$.fn.lbgallery.defaults,options);

		return this.each(function(){
			$this = $(this);
			opts = $.meta ? $.extend({}, options, $this.data()) : options;
			
			_init();
		});

		function _init() {
			opts.pointer = 0;
	
			opts.elements = $this.find(opts.elSelector);
			$(opts.elements).each(function(k,v){
				$(this).show().css('z-index',opts.elements.length-k);
				var $overlay = $(this).find(opts.overlaySelector);
				$overlay.show();
				if ($overlay.hasClass('top')) {
					$overlay.css('top','-'+$overlay.height()+'px');
				} else {
					$overlay.css('bottom','-'+$overlay.height()+'px');
				}
	
				if (k != 0) {
					$(this).hide();
				}
			});		
	
			if (opts.useFancybox && $.fn.fancybox) {
				$(opts.elements).find('a').fancybox({
					callbackOnStart : fancyboxStartCallback,
					callbackOnShow : fancyboxShowCallback,
					callbackOnClose : fancyboxCloseCallback
				});				
			}
	
			_showOverlay();
			opts.timeoutFunction = setTimeout(function(){
		  	_hideOverlay();
	  	}, opts.pause);
	
		}

		function _showOverlay() {
			var $overlay = $(opts.elements[opts.pointer]).find(opts.overlaySelector);
			if ($overlay.hasClass('top')) {
				$overlay.animate({top:0},'slow');
			} else {
				$overlay.animate({bottom:0},'slow');
			}
		}
		
		function _hideOverlay() {
			//console.log(opts.pause);

			if(!opts.elements.length || opts.elements.length < 2) {
				clearTimeout(opts.timeoutFunction);
				return;
			}

			var $overlay = $(opts.elements[opts.pointer]).find(opts.overlaySelector);
			if ($overlay.hasClass('top')) {
				$overlay.animate({top:'-'+$overlay.height()+'px'},'slow',_fader());
			} else {
				$overlay.animate({bottom:'-'+$overlay.height()+'px'},'slow',_fader());
			}

			opts.timeoutFunction = setTimeout(function(){
		  	_hideOverlay();
	  	}, opts.pause);
		}
		
		function _fader() {
			var next;
			var maxOffset = opts.elements.length - 1;
			
			if(opts.pointer == maxOffset) {
				nextPointer = 0;
			} else {
				nextPointer = opts.pointer + 1;
			}

			next = opts.elements[nextPointer];
			
			$(opts.elements[opts.pointer]).fadeOut(opts.speed);
			$(next).fadeIn(opts.speed,function(){
				opts.pointer = nextPointer;
				_showOverlay();
			});
			
		}

		function fancyboxStartCallback() {
			clearTimeout(opts.timeoutFunction);
		}
		
		function fancyboxShowCallback() {
			var $overlay = $(opts.elements[opts.pointer]).find(opts.overlaySelector);
			if ($overlay.hasClass('top')) {
				$overlay.css('top','-'+$overlay.height()+'px');
			} else {
				$overlay.css('bottom','-'+$overlay.height()+'px');
			}

			$(opts.elements[opts.pointer]).hide();

			opts.pointer = this.itemCurrent;
			$(opts.elements[this.itemCurrent]).show();
			var $overlay = $(opts.elements[opts.pointer]).find(opts.overlaySelector);
			if ($overlay.hasClass('top')) {
				$overlay.css('top',0);
			} else {
				$overlay.css('bottom',0);
			}
		}
		
		function fancyboxCloseCallback() {
			opts.timeoutFunction = setTimeout(function(){
				_hideOverlay();
			}, opts.pause);
		}
		
		

	};

	$.fn.lbgallery.defaults = {
		speed : 'slow',
		pause : 1000,
		elSelector : 'li',
		overlaySelector : '.overlay',
		useFancybox : true
	};

})(jQuery);