;(function($) {

	$.fn.imageGallery = function(settings) {
	  
		// default configuration properties
		var defaults = {
			images: [],
			speed: 	800,
			pause:	2000
		};
		
		var _current_image = 0;
		var _gallery = this;

        this.each(function() {
	
			var ext = $(this);
			var timout;
			
			this.init = function() {
				var _self = this;
				this.create_thumbs_list();
				this.show_first_image();
				// preload images
				for (i = 0; i < this.options.images.length; i++) {
					var img = new Image();
					img.src = this.options.images[i][1];
				}
				timout = setTimeout(function() { _self.show_next_image() }, this.options.pause);
			}
			
			this.show_first_image = function() {
				ext.find('.main-photo-next').hide();
				ext.find('.main-photo').attr('src', this.options.images[0][0]);
				ext.find('h2').html(this.options.images[0][2]);
				ext.find('a.link').attr('href', this.options.images[0][3]).attr('title', this.options.images[0][2]);
			}
			
			this.create_thumbs_list = function() {
				var _self = this;
				ext.find('.photos').html('');
				for (i = 0; i < this.options.images.length; i++) {
					if (i == _current_image) {
						ext.find('.photos').append('<li class="active"><img src="' + this.options.images[i][1] + '" width="47" height="44" /></li>');
					} else {
						ext.find('.photos').append('<li><a href="#" rel="' + i + '"><img src="' + this.options.images[i][1] + '" width="47" height="44" /></a></li>');
					}
				}
				ext.find('.photos li a').click(function() {
					_current_image = new Number($(this).attr('rel'));
					_self.show_image();
					return false;
				});
			}
			
			this.show_next_image = function() {
				var _self = this;
				_current_image = _self.get_next_image();
				ext.find('h2').text(this.options.images[_current_image][2]);
				ext.find('a.link').attr('href', this.options.images[_current_image][3]).attr('title', this.options.images[_current_image][2]);
				ext.find('.main-photo-next').attr('src', this.options.images[_current_image][0]).show();
				_self.create_thumbs_list();
				ext.find('.main-photo').fadeOut(this.options.speed, function() {
					ext.find('.main-photo').attr('src', _self.options.images[_current_image][0]).show();
					ext.find('.main-photo-next').hide();
				});
				timout = setTimeout(function() {_self.show_next_image()}, this.options.pause);
			}
			
			this.show_image = function() {
				var _self = this;
				ext.find('h2').text(this.options.images[_current_image][2]);
				ext.find('a.link').attr('href', this.options.images[_current_image][3]).attr('title', this.options.images[_current_image][2]);
				ext.find('.main-photo-next').attr('src', this.options.images[_current_image][0]).show();
				_self.create_thumbs_list();
				ext.find('.main-photo').fadeOut(this.options.speed, function() {
					ext.find('.main-photo').attr('src', _self.options.images[_current_image][0]).show();
					ext.find('.main-photo-next').hide();
				});
				clearTimeout(timout);
				timout = setTimeout(function() {_self.show_next_image()}, this.options.pause);
			}
			
			this.get_next_image = function() {
				if (_current_image >= (this.options.images.length - 1)) {
					return 0;
				} else {
					return _current_image + 1;
				}
			}
			
			this.options = $.extend({}, defaults, settings);
			
			this.init();
			
			return ext;
		});
	  
		return this;
	};

})(jQuery);