(function($){

	$.fn.extend({ 
		
		//pass the options variable to the function
		simpleGallery: function(options) {


			//Set the default values, use comma to separate the settings, example:
			var defaults = {
				speed: 3000, // transition speed
				nav: '#gallery-nav', // id or selector string to the navigation triggers
				slideshow: true
			}
				
			var options =  $.extend(defaults, options);

			return this.each(function() {
				var o = options,
					self = $(this); // cache the current selector
				
				var SG = {}; // safely store shit.
				SG.speed = o.speed;
				SG.gallery = $(this);
				SG.nav = $(o.nav);
				SG.hero = SG.gallery.find('img');
				SG.slideshow = o.slideshow;
				SG.totalCount = SG.nav.find('a[data-src]').length;
				
				// triggers setup
				// subnav
				SG.nav.find('a[data-index]').first().addClass('active'); // add the active class on load
				SG.nav.find('a').click(function(e) {
					e.preventDefault();
					if ( this.getAttribute('href') == '#next' ) { 
						//	next button
						nextImg();
					} else if ( this.getAttribute('href') == '#prev' ) {
						// prev button
						prevImg();
					} else {
						// these are the numbered links to images
						gotoImg($(this).data('index'));
					}
				});
				SG.hero.wrap('<div class="main-image"></div>').click(nextImg);
				SG.hero.parent().append('<div class="alt-text"><p></p></div>');
				SG.altText = SG.gallery.find('.alt-text');
				SG.altText.hide();
								
				// helper functions
				function current(number) { // set or retrieve the data-current number
					if ( number ) { 
						SG.gallery.data('current', number);
					} else {
						return SG.gallery.data('current');
					}
				}
				
				
				// main functions to fire
				// slideshow interval
				if (o.slideshow) {
					var interval;
					SG.gallery.data('slideshow', 'running')

					function slideshow() {
						// cycle
						if ( SG.gallery.data('slideshow') == "running") { 
							nextImg();
						} else {
							
						}
					}
					
					// pause on hover
					SG.gallery.hover(function() {
						SG.gallery.data('slideshow','paused');
					
						clearInterval(interval);
					
					}, function() {
					
						if (SG.gallery.data('slideshow') == "running") { setTimeout(nextImg, (o.speed/2)); }
						interval = setInterval(slideshow, o.speed);
						SG.gallery.data('slideshow','running');
						
					});
				
					// turn on the slideshow if option is set
					if ( o.slideshow ) { 
						interval = setInterval(slideshow, o.speed); 
					}

				}
				var alt = "";
				SG.gallery.hover(function() {
					if ( alt === "" ) { 
						SG.altText.slideUp(300);
					} else {
						SG.altText.slideDown(300);
					}
				}, function() {
					if ( alt !== "" ) { 
						SG.altText.stop(true, false).slideUp(600);
					}
				});

				function nextImg() {
					if ( current() == SG.totalCount ) { 
						// if we are on the last image
						gotoImg(1);
					} else {
						// any other image go to next
						gotoImg(current()+1);
					}
				}
				function prevImg() {
					if ( current() == 1 ) { 
						gotoImg(SG.totalCount);
					} else {
						gotoImg(current()-1);
					}
				}
				function gotoImg(imageIndex) {
					SG.src = SG.nav.find('a[data-index="'+imageIndex+'"]').data('src'); // find the next src
					alt = SG.nav.find('a[data-index="'+imageIndex+'"]').data('alt'); // find the next src
					SG.hero.attr('src', SG.src); // set the src of the image
					SG.altText.slideUp('fast');
					SG.hero.next('div').find('p').text(alt); // replace the image text
					if ( alt !== "" ) { 
						 SG.altText.slideDown('fast');
					}
					SG.nav.find('a[data-index]').removeClass('active').end().find('a[data-index="'+imageIndex+'"]').addClass('active');
					current(imageIndex); // update the current number on the container
				}
				
				

			});
		}
	});
	
})(jQuery);
