function bannerSlideShow() {
	var TRANSITION_TIME = 6000;		// number of milliseconds to spend on transition
	var DISPLAY_TIME = 4000;		// number of milliseconds to display after transition
	
	
	var loadedImages = [];
	var slideIndex = -1;
	
	function randomSort( a, b )
	{
		return Math.random() - 0.4;
	}
	
	function initSlideShow()
	{
		// Replace these with your own images.
		var images = [
			'/just/banner/banner_1.jpg',
			'/just/banner/banner_2.jpg', 
			'/just/banner/banner_3.jpg', 
			'/just/banner/banner_4.jpg', 
			'/just/banner/banner_5.jpg',
			'/just/banner/banner_6.jpg'
		];
		var loaded = {};
		var gallery = $('gallery');
		images.sort( randomSort );
	
		new Asset.images(images, {
			onProgress: function(i) {
				this.setStyles({
					'position': 'absolute',
					'opacity': 0,
					'left': 0,
					'top': 0
				});
				if( !loaded[this.src] )
				{
					loaded[this.src] = 1;
					loadedImages.push( this );
					this.inject( gallery );
				}
			},
			onComplete: function() {
				gallery.ondblclick = skipSlide;
				showNextSlide();
			}
		});
		
		document.getElementById('gallery').style.background = "white";
	}
	
	var slideTimer = false;
	
	function showNextSlide()
	{
		if( slideIndex != -1 )
		{ // hide the old image after the new one has transitioned in
			var oldSlide = slideIndex % loadedImages.length;
			loadedImages[oldSlide].setStyle('z-index', 1);
			var disappear = function() { this.setStyles({'display': 'none', 'opacity': '0'}); };
			disappear.delay( TRANSITION_TIME, loadedImages[oldSlide] );
			if( oldSlide + 1 == loadedImages.length )
			{ // reshuffle when we get through the list.  avoid repeats.
				var f = loadedImages.shift();
				var l = loadedImages.pop();
				loadedImages.sort( randomSort );
				loadedImages.push( l );
				loadedImages.push( f );
			}
		}
	
		var nextSlide = ++slideIndex % loadedImages.length;
		// fade in the next image
		loadedImages[nextSlide].setStyles({'display': 'block', 'z-index': 2});
		loadedImages[nextSlide].effect('opacity', {'duration': TRANSITION_TIME}).start(1);
	
		var nextTime = TRANSITION_TIME + DISPLAY_TIME;
		if( nextSlide + 1 == loadedImages.length ) // last slide
			nextTime *= 28;
		slideTimer = window.setTimeout( showNextSlide, nextTime );
	}
	
	function skipSlide()
	{
		window.clearTimeout( slideTimer );
		showNextSlide();
	}
	
	initSlideShow();
}



function FeatureStories()
{
	var currentFeature = 0;
	var previousFeature = 0;
	var urlFeature = false;
	var featureIndexBtnBG = -1;
	var features = [[]]; // array with element 0 being an empty array.
	var autoTimer = false;
	var featuresFocused = false;
	var featureFrozen = false;
	var minheight = 0;
	var maxheight = 250;
	var extraheight = 0;
	var featureCover = document.createElement('div');
	var resize_effect = 0;

	function activateFeatureStories()
	{
		if( !featuresFocused )
		{
			//alert('activating');
			featuresFocused = true;
			morphHeight();
		} //else alert('not activating');
	}
	
	function standbyFeatureStories()
	{
		featuresFocused = false;
		if( !featureFrozen )
			window.setTimeout( deactivateFeatureStories, 1000 );
	}
	
	function deactivateFeatureStories()
	{
		if( !featuresFocused )
		{
			morphHeight();
		}
	}
	
	function updateFeatureHeight( feature )
	{
		var h = feature['feature'].getStyle('height');
		var v = parseInt( h );
		feature['height'] = v;
		//alert(feature['id'] + ' height == ' + h);
		if( v > maxheight )
			maxheight = v;
		return v;
	}

	var featureStories = document.getElementById('featureStories');
	if( featureStories )
	{
		var headertag = $(featureStories.getElementsByTagName('h2')[0]);
		extraheight = parseInt(headertag.getStyle('height'))
					+ parseInt(headertag.getStyle('padding-top'))
					+ parseInt(headertag.getStyle('padding-bottom')) + 1;
		featureStories = $(featureStories);
		featureStories.removeClass('featureStoriesStatic');
		featureStories.onmouseover = activateFeatureStories;
		featureStories.onmouseout = standbyFeatureStories;
		maxheight = minheight = parseInt(featureStories.getStyle('height'));
		resize_effect = featureStories.effect('height', {'fps': 30, 'link': 'cancel', 'duration': 750, 'wait': false});
		var featuresIndex = document.getElementById('featuresIndex');
		if( featuresIndex )
		{
			var featuresIndexLinks = featuresIndex.getElementsByTagName('li');
			for( var i=0; i < featuresIndexLinks.length; i++ )
			{
				var li = featuresIndexLinks.item(i);
				var a_list = li.getElementsByTagName('a');
				if( a_list.length )
				{
					var li_a = a_list[0];
					if( li_a.hash && li_a.hash.charAt(0)=='#' )
					{
						featureID = li_a.hash.substring(1);
						if( (location.hash && location.hash == li_a.hash) && !urlFeature )  // if the page has a #featureID in the url,
							urlFeature = li_a;  // make it the default one to start on.

						var feature = $(featureID);
						if( feature )
						{
							features[0].push( featureID );
							feature.addClass('featureInactive');
							feature.setStyle('padding-top', extraheight);
							var span = $(li_a.firstChild);
							if( featureIndexBtnBG == -1 )
								featureIndexBtnBG = span.getStyle('background-color');
							features.push( {
								'id': featureID,
								'li': li,
								'li_a': li_a,
								'feature': feature,
								'idx': i + 1,
								'idx-effect': span.effect('background-color', {'fps': 30, 'link': 'cancel', 'duration': 750, 'wait': false}),
								'story-effect': feature.effect('opacity', {'fps': 30, 'link': 'cancel', 'duration': 500, 'wait': false})
							} );
							li_a.onclick = function() { return freezeStory(this); };
							li.className = 'featuresIndexInactive';
						}
					}
				}
			}
			var featureDivider = document.getElementById('featureDivider');
			if( featureDivider )
			{
				featureDivider.style.position = 'absolute';
				featureDivider.style.zIndex = features.length - 2;
			}
			featureCover.className = 'featureCover';
			if( urlFeature )
				freezeStory( urlFeature );
			else
				morphToNextStory();
			//alert('maxheight = ' + maxheight);
		}
		//else alert("#featuresIndex not found.  :(");
	}
	//else alert("#featureStories not found.  :(");
	
	function getFeature( idx )
	{
		if( idx > 0 && idx < features.length )
			return features[idx];
		return false;		
	}
	function getCurrentFeature()
	{
		return getFeature( currentFeature );
	}

	function changeStory( idx, hurry )
	{
		var oldfeature = getCurrentFeature();
		var nextfeature = getFeature( idx );
		var old_idx = currentFeature;
		
		if( oldfeature !== nextfeature )
		{
			if( featureCover.parentNode )
				featureCover = featureCover.parentNode.removeChild( featureCover );
		
			if( oldfeature )
			{
				//oldfeature['li'].className = 'featuresIndexInactive';
				oldfeature['idx-effect'].start(featureIndexBtnBG);
				oldfeature['feature'].removeClass('featureActive');
				oldfeature['feature'].addClass('featurePrevious');
			}
			if( nextfeature )
			{
				featureFrozen = false;
				var span = $(nextfeature['li_a'].firstChild);
				//nextfeature['li'].className = 'featuresIndexActive';
				var newcolor = $(nextfeature['li']).getStyle('background-color');
				if( hurry )
				{
					nextfeature['idx-effect'].stop();
					nextfeature['idx-effect'].set(newcolor);
				}
				else
				{
					nextfeature['idx-effect'].start(newcolor);
				}
				nextfeature['story-effect'].stop();
				nextfeature['story-effect'].set(0);
				nextfeature['story-effect'].start(1.0);
				//if( featureFocused )
				//{
					//nextfeature['feature'].setStyle('height', maxheight);
					//alert('h = ' + nextfeature['height'] + ', x = ' + extraheight);
				//}
				nextfeature['feature'].removeClass('featureInactive');
				nextfeature['feature'].addClass('featureActive');
				var h = updateFeatureHeight(nextfeature);
				//alert('featureCover.style.height = ' + (maxheight - h) + 'px');
				featureCover.style.height = (maxheight - h) + 'px';
				nextfeature['feature'].appendChild( featureCover );
				currentFeature = idx;
				morphHeight();
			}
			for(var i=1; i<features.length; i++)
			{
				var f = features[i]['feature'];
				if( i == idx )
				{
					f.style.zIndex = features.length;
				}
				else
				{
					if( f.style.zIndex > 0 )
						f.style.zIndex--;
					if( i == previousFeature )
					{
						f.removeClass('featurePrevious');
						f.addClass('featureInactive');
						f.style.zIndex--;
					}
				}
			}
			previousFeature = old_idx;
		}
	}

	function morphToNextStory()
	{
		if( !featuresFocused )
		{
			var nextidx = currentFeature % (features.length-1) + 1;  // looks odd, but it's not 0 indexed.
			changeStory( nextidx, false );
		}
		scheduleNextMorph( 1000 * 15 + 3 * (features.length - currentFeature) );  // older stories get less time
	}
	
	function scheduleNextMorph( howsoon )
	{
		if( autoTimer )
			window.clearTimeout( autoTimer );
		autoTimer = window.setTimeout( morphToNextStory, howsoon );
	}
	
	function morphHeight()
	{
		var f = getCurrentFeature();
		var h = minheight;
		var fh = f['height'];
		if( featuresFocused )
			h = Math.max( fh, minheight );
		h += extraheight;
		var cur_h = parseInt(featureStories.getStyle('height'));
		if( cur_h != h )
		{
			//alert('resizing story');
			resize_effect.start( h );
		} //else alert('not resizing. feature.height == ' + fh + ', cur_h = ' + cur_h + ', req_h == ' + h);
	}
	
	function freezeStory( anchor )
	{
		var idx = 0;
		for(var i=1; i < features.length; i++ )
		{
			if( features[i]['li_a'] === anchor )
			{
				idx = i;
				break;
			}
		}
		if( idx )
		{
			changeStory( idx, true );
			scheduleNextMorph( 1000 * 60 * 5 );
			featureFrozen = true;
			return false; // tell browser don't follow href
		}
		//else alert("can't freeze a non-story, sorry.");
		
		return true; // browser might as well follow link
	}
}


window.addEvent('domready', bannerSlideShow);
window.addEvent('domready', FeatureStories);

