var timeOuts = new Array();
var firstWidths = new Array();
var isOnFirst = true;

	function kickThingsOff($img) {
		$gallery = $img.closest('.rotatingGallery');
		var blockId = $gallery.attr('id').replace('rotateBlock','');
		firstWidths[blockId] = $gallery.width();
		window.setTimeout(function() { rapidResizeGallery($gallery); }, 100);

		showSlide($img.closest('.rotatingBox'));
		// Start the slideshow now that the first one's loaded
		nextSlide($gallery);
	}

	// The loading images in other columns could screw this up for the 
	// first image. Keep rapid firing.
	function rapidResizeGallery($gallery) {
		if( !isOnFirst ) return; 
		var w = $gallery.width();
		var blockId = $gallery.attr('id').replace('rotateBlock','');
		if( w != firstWidths[blockId] ) {
			firstWidths[blockId] = w;
			resizeImages($gallery);
		}
		window.setTimeout(function() { rapidResizeGallery($gallery); }, 100);
	}

	function nextSlide($gallery) {
		var blockId = $gallery.attr('id').replace('rotateBlock','');
		if( timeOuts[blockId] ) { clearTimeout(timeOuts[blockId]); }

		var $curr = $gallery.find('.current');
		var $next = $curr.eq(0).next();
		if( !$next.size() ) {
			$next = $curr.siblings().eq(0); // wrap
		}
		var success = true;
		if( $next.size() ) { success = showSlide($next); }

		// Prep the next queued slide...
		if( success && overflow[blockId] ) { 
			var photo = overflow[blockId].pop();
			if( photo ) { 
				var $newSlide = $('<div style="z-Index: -1;" class="rotatingBox fauxHidden"><div class="cycleMine"><img src="' + photo['url'] + '"><span class="highlight2"><a href="/mod/gallery/view-photo.php?photo_id=' + photo['photo_id'] + '"></a>' + photo['name'] + '</span></div></div>');
				$newSlide.css({opacity: 0}).appendTo($gallery);
			}
		}

		timeOuts[blockId] = window.setTimeout(function() {
			nextSlide($gallery);
		}, rotateSecs*1000);
	}

	function showSlide($slide) {
		// make sure it's there
		if( !resizeImage($slide.find('img')) ) { return false; } // Not ready

		$slide.siblings().css('z-Index',1).not('.current').css({opacity: 0});
		$slide.siblings().filter('.current').removeClass('current').animate({opacity: 0}, 1500);
		$slide.css('z-Index',2).addClass('current').animate({opacity: 1}, 1500);
		return true;
	}

	function dbg(msg) {
		$('<div>' + msg + '</div>').appendTo('#dbg');
	}

	function resizeImages($gallery) {
		$gallery.find('img').each(function() {
			resizeImage($(this));
		});
	}
	function resizeImage($img) {
		var $gallery = $img.closest('.rotatingGallery');
		var divW = $gallery.width(); // 20100130 - Too wide for first hit
		var divH = $gallery.height();

		// If narrow, shrink the height
		var maxH = Math.round(divW / rotateRatio);
		if( maxH > 340 ) { maxH = 340; }
		//dbg(maxH + ' / ' + divH);
		if( divH != maxH ) {
			//dbg("Rdz from " + divH + ' to ' + maxH);
			$gallery.height(maxH);
			divH = maxH;
		}

		var w = $img.width();
		var h = $img.height();
		if( w == 0 || h == 0 ) {
			return; }

		// Start by making it as wide as the box
		var newW = divW;
		var newH = h*(newW/w);

		// If it's too tall, scale back
		if( newH > maxH ) {
			newW = newW * maxH/newH;
			newH = maxH;
		}
		$img.width(newW);
		$img.height(newH);
		$img.parent().find('p').width(newW);
		var marginLeft = (divW-newW)/2;

	//dbg("RSZ: " + divW + 'x' + divH + ', ' + $img.attr('src') + ", " + $gallery.attr('id'));

		$img.closest('.rotatingBox')
			.css('margin-left', '' + marginLeft + 'px')
			.css('border', '1px solid black')
			.find('span').css({opacity: .7});
		return true;
	}
$(document).ready(function() {
	$(window).bind('resize',function() {
		$('.rotatingGallery').each(function() {
			resizeImages($(this));
		});
	});
	$('.rotatingGallery').each(function() {
		var $gallery = $(this);
		var $img = $gallery.find('.first DIV IMG');
		if( $img.get(0).complete ) {
			// is already cached, won't fire load event
			kickThingsOff($img);
		} else { 
			$img.bind('load',function() { kickThingsOff($(this)); } );
		}
	}).click(function() {
		var url = $(this).find('.current A').attr('href');
		if( url ) { document.location = url; }
		return false;
	}).hover(	
		function() { $(this).addClass('pointer'); },
		function() { $(this).removeClass('pointer'); }
	);
});
$(window).load(function() {
	resizeImages($(this));
	isOnFirst = false;
});

