/** Carousel script for Main Flash
*
* @author Matt Campbell
* @date 2009-07-28
* @version 1.0
* @revision $Id: $
*/
(function() {
	var	content = document.getElementById("flash_content"),
		images = [], blocks = [], allImages = [], changeID = -1,
		speed = 0.05, timeoutID = -1,j,currentSlide = 0,
		setAlpha = function(elmt,alpha) {
			alpha = +alpha;
			if (!isNaN(+alpha)) {
				alpha = Math.max(0,Math.min(1,alpha)); // force alpha to stay in 0-1 range
				elmt.style.opacity = ""+alpha; // Gecko, Webkit, Opera, etc.
				elmt.style.filter = "alpha(opacity="+(alpha*100)+")"; // IE
			}
		},
		getAlpha = function(elmt) {
			if (isNaN(+elmt.style.opacity)) {
				return 0;
			}
			return +elmt.style.opacity;
		},
		increaseAlpha = function(elmt,amt) {
			setAlpha(elmt,getAlpha(elmt)+amt);
		},
		decreaseAlpha = function(elmt,amt) {
			increaseAlpha(elmt,-amt);
		},
		updateBlocks = function() {
			for (var j = 0;j < blocks.length;j+=1) {
				if (j === currentSlide) {
					blocks[j].className = "active";
				}
				else {
					blocks[j].className = "inactive";
				}
			}
		},
		changeSlide = function() {}, /* Defined early to keep JSLint happy */
		fadeIn = function() {
			clearTimeout(timeoutID);
			increaseAlpha(content,speed);
			
			if (getAlpha(content) === 1) {
				timeoutID = -1;
				changeID = setTimeout(function() {
					changeSlide(currentSlide+1);
				},5000);
				return;
			}
			timeoutID = setTimeout(fadeIn,25);
		},
		fadeOut = function() {
			clearTimeout(timeoutID);
			decreaseAlpha(content,speed);
			
			if (getAlpha(content) === 0) {
				// Time to switch the images ..
				
				/* Move images out of visible area */
				for (var j = 0;j < images.length;j+=1) {
					images[j].style.left = "-1000px";
				}
				
				/* Make current image visible */
				images[currentSlide].style.left = "0px"; 
				
				/* Update navigation */
				updateBlocks();
				
				fadeIn();
				return;
			}
			timeoutID = setTimeout(fadeOut,25);
		};
		
	changeSlide = function(id) {
		if (timeoutID === -1) { // make sure we're not in the middle of animation
			clearTimeout(changeID); // abort pending change
			
			if (id < 0 || id >= images.length) {
				id = 0;
			}
			currentSlide = id;
			
			/* And go .. */
			setAlpha(content,1);
			fadeOut();
		}
	};
		
	/* Step 1: Find the images in the flash area */
	allImages = document.getElementsByTagName("A");
	for (j = 0;j < allImages.length;j+=1) {
		if (allImages[j].parentNode === content) {
			images.push(allImages[j]);
		}
	}
	
	/* Step 2: Move all images out of the visible area and setup blocks */
	for (j = 0;j < images.length;j+=1) {
		images[j].style.left = "-1000px";
		blocks[j] = document.getElementById("block"+(j+1));
		(function(n) {
			blocks[n].onclick = function() {
				changeSlide(n);
				this.blur();
				return false;
			};
		}(j));
	}
	images[0].style.left = "0px"; // make first image visible
	
	updateBlocks(); // the clickable boxes for switching slides
	
	/* Step 3: Start the carousel */
	changeID = setTimeout(function() {
		changeSlide(1);
	},5000);
}());
