
//--- Un-globalize the global functions
(function() {

	// Call the Gallery Object onDomReady
	$(function() {
		Gallery();
	});
	
	/* GALLERY OBJECT */
	function Gallery(config) {
		config = jQuery.extend({
			tiles: $(".gallery-tiles LI")
		}, config || {});
		
		// When the history changes, see if we need to 
		// change the gallery image.
		$.address.change($onChange);
			
		// Add hover styles to the tiles.
		config.tiles.hover(hoverOver, hoverOut);
			
		//--- Initialize Gallery Slideshow
		var slideshowEl = $(".slideshow");
		var isSlideshow = slideshowEl.length !== 0 && slideshowEl.children().length > 1;
		
		var slideShowObj = SlideShow({
			slideshowParent: (isSlideshow) ? slideshowEl : $(".gallery"),
			autoSlideNext: isSlideshow
		});	
		
		function showGallery(galleryId) {
		
			//--- Get the current gallery
			var curr = slideShowObj.current();
			
			//--- Get the new gallery
			var next = $("#gallery-" + galleryId);
			
			// No current slide, no next slide, or we are already on that slide, do nothing
			if(!curr || !next || curr.attr('id') === next.attr('id'))   { return; }
			
			// transition out the old and new ones
			slideShowObj.progress(curr, next);
		}
		
		function $onChange(ev) {
			var gallery = valueFromHashPairs('gallery', ev.value);
			if(!gallery || !gallery.length) { return; }
			showGallery(gallery);
		}	
	}


	function SlideShow(config) {
		
		config = jQuery.extend({
					slideshowParent : $(".slideshow"),
					fadeSpeed: 500,			// .5 seconds
					timeBetweenSlides: 6000,//  5 seconds
					moreSlideSpeed: 500,	// .5 seconds
					autoSlideNext: true
				}, config || {});
				
		//--- Get the slides
		var slides = config.slideshowParent.children();	
		var intervalId = null; 
		var currentAnimationEl = null;
		
		// pause on mouse hover over,out
		slides.hover(stop, function() {
			if(config.autoSlideNext) {
				start();
			}
		});
			
		//--- Lets go, but only if we want to automatically slide.
		if(config.autoSlideNext) {
			start();
		}	
		
		function start() {
			clearInterval(intervalId);
			
			intervalId = setTimeout(function() {
						progress(current());
					}, config.timeBetweenSlides);
		}
			
		function progress(slide, nextSlide) {
			// If there was an interval in progress
			// lets stop that baby, this new run is
			// all you have to care about.
			stop();
			
			currentAnimationEl = $(slide).fadeOut(config.fadeSpeed, function() {
					// No guarentee there aren't other current classes.
					slide.removeClass("current");
					var next = nextSlide || slide.nextOrFirst();
						next.addClass("current");
						
					var more = next.children(".more");
						more.css({bottom: '-97px', opacity: 0});
					
					syncTileSelected(next);
					
					currentAnimationEl = next.fadeIn(config.fadeSpeed, function() {
						
						if(more.length === 0 && config.autoSlideNext) {
							start();
							return;
						}
						
						// Show the More section
						currentAnimationEl = more.animate({ bottom: "0", opacity: 1}, config.moreSlideSpeed, 'swing', function() {
							//--- Start it all over again.
							if(config.autoSlideNext) {
								start();
							}
						});
					});
			
			});
		}
		
		function stop() {
			clearInterval(intervalId);
			
			if(currentAnimationEl) {
				currentAnimationEl.stop(true, true);
			}
		}
		
		function current() {
			var curr = config.slideshowParent.children(".current");
			if(curr.length == 0) {
				return null;
			}
			return curr.eq(0);
		}
		
		function syncTileSelected(selectedGallery) {
			// No more old selected
			$(".gallery-tiles .current").removeClass("current");
			
			// Get our position in the collection
			var index = selectedGallery.selfIndex();
			
			// For that position, get the tile at that index and set it current.
			var tile = $(".gallery-tiles LI").eq(index).addClass("current");
			
			var href = tile.children('a.history').attr('href').replace(/^#/, '').split('/');
			$.address.value(buildHashPair(href[1], href[2]));
		}
		
		return { 
			start: start, 
			stop: stop,
			progress: progress, // slideToHide, slideToShow
			current: current
		};
	};
})();

