var IndexPage = function() {
	var rotateTimeout = 8000;
	var scrollTimeout = 5000;

	return {
		init : function(){
			$(window).data('queued_rotations', 0);
			$(window).scroll(IndexPage.handleWindowScroll);
			setTimeout(IndexPage.rotateVisibleThumbnails, rotateTimeout);
		},
		handleWindowScroll : function(e, timeout_complete){
			clearTimeout($(window).data('timeout'));
			if (timeout_complete)
			{
				IndexPage.rotateVisibleThumbnails();
			}
			else
			{
				$(window).data('timeout', setTimeout(function(){
					IndexPage.handleWindowScroll(e, true);
				}, scrollTimeout));
			}
		},
		//Find the visible gallery thumbnails and try to fade them to the next 
		//thumbnail in the slideshow (if any)
		rotateVisibleThumbnails : function(){
			var visible_gallery_found = false;
			$('.gallery_with_slideshow').each(function(){
				if ($(window).data('timeout') != undefined)
				{
					clearTimeout($(window).data('timeout'));
					$(window).data('timeout', undefined);
				}
				var is_visible = IndexPage.rotateGallery($(this));
				if (! is_visible && visible_gallery_found == true) {
					return false;
				}
				if (is_visible) {
					$(window).data('queued_rotations', $(window).data('queued_rotations') + 1);
					visible_gallery_found = true;
				}
			});
		},
		//gallery_div is a jquery obj
		rotateGallery : function(gallery_div) {
			if (! IndexPage.isVisible(gallery_div))
			{
				return false;
			}

			var slideshow_values = gallery_div.find('ul.slideshow_values li');
			var curr_slide = slideshow_values.filter('.current');
			if (curr_slide.length == 0)
			{
				if (slideshow_values.length > 1)
				{
					//we were on the first, now move to the second
					curr_slide = slideshow_values.slice(0, 1);
				}
				else
				{
					//zero or just one entry in the slideshow
					return;
				}
			}
			var next_slide = curr_slide.next('li');
			if (next_slide.length == 0)
			{
				next_slide = slideshow_values.slice(0, 1);
			}

			IndexPage.rotateGalleryFinish(gallery_div, curr_slide, next_slide);
			return true;
		},
		rotateGalleryFinish : function(gallery_div, curr_slide, next_slide) {
			next_slide.addClass('next').find('img').css({opacity: 0});
			
			curr_slide.find('img').fadeTo('slow', 0, function(){
				curr_slide.removeClass('current');
			});
			next_slide.find('img').fadeTo('slow', 1, function(){ 
				next_slide.addClass('current');
				next_slide.removeClass('next');
				$(window).data('queued_rotations', $(window).data('queued_rotations') - 1);
				if ($(window).data('queued_rotations') == 0) {
					$(window).data('timeout', setTimeout(IndexPage.rotateVisibleThumbnails, rotateTimeout));
				}
			});
		},
		//takes in a jquery obj
		//just worry about the y values for now
		isVisible : function(gallery_div) {
			var is_visible = true;
			var offset = gallery_div.offset();
			if (offset.top > ($(window).scrollTop() + $(window).height()) )
				is_visible = false;
			else if ((offset.top + gallery_div.height()) < $(window).scrollTop())
				is_visible = false;
			return is_visible;
		}
	};
}();

$(document).ready(IndexPage.init);
