/*******************************************************
Common scripts.
********************************************************/

function toggleSearch(searchField){
	if (searchField.value == 'Search') {
		searchField.value = '';
		searchField.className = 'search_active';
	}
	else {
		if (searchField.value == '') {
			searchField.value = 'Search';
			searchField.className = null;
		}
	}
}



/*******************************************************
********************************************************/

function isFlashInstalled() {
	var hasReqestedVersion = DetectFlashVer(8,0,0);

	if (hasReqestedVersion) {
		return true;
	} else {
		return false;
	}
}


/*******************************************************
Scripts for home page image rotation.
********************************************************/

// Detect if the user is on IE. If so, don't do Scriptaculous transitions because of ghosting effect.
browser = navigator.appName;
if (browser == "Microsoft Internet Explorer") {
	isIE = 1;
} else {
	isIE = 0;
}

// Set transition times (1000 = 1 second). 
timeToPicBlur = 2000;
timeToFirstHeadline = 2000;
timeToSecondHeadline = 4500;
timeToThirdHeadline = 6500;
timeToFadeAll = 9500;

// Set duration of Scriptaculous fade ins and fade outs (1.0 = 1 second).
// Don't use Scriptaculous transitions on sIFR headlines on IE because of ghosting effect.
picFadeInDuration = 1.0;
if (isIE) {
	headlineFadeInDuration = 0;
	headlineFadeOutDuration = 0;
} else {
	// Take out Scriptaculous transitions altogether -- they don't work in Chrome or Safari anymore either.
	//headlineFadeInDuration = 0.3;
	//headlineFadeOutDuration = 0.3;
	headlineFadeInDuration = 0;
	headlineFadeOutDuration = 0;
}

// Set opacity of thumbnails when active. 100 is full opacity for IE, 1.0 is full opacity for other browsers.
activeThumbOpacity = 0.5;
activeThumbOpacityIE = 50;

// Set this so we don't fade out the initial image on the first go-round.
// Also ensures that animation doesn't break if user clicks on a thumbnail before everything's loaded.
// Also loads everything slightly faster on first runthrough.
firstRun = 1;

// Set variables for setTimeouts.
var blurDelay;
var h1Delay;
var h2Delay;
var h3Delay;
var clearHeadlineGroupDelay;
var imagePreloadDelay;

// Function to clear all existing timeouts, all effects in progress or in queue, hide all pics, etc.
function clearDecks(includeBlur) {
	// Clear all timeouts.
	clearInterval(blurDelay);
	clearInterval(h1Delay);
	clearInterval(h2Delay);
	clearInterval(h3Delay);
	clearInterval(clearHeadlineGroupDelay);
	clearInterval(imagePreloadDelay);

	// Cancel all Scriptaculous effects in progress or in queue.
	var queue = Effect.Queues.get('global');
	queue.each(function(effect) { effect.cancel(); });
	
	// Hide all of the headlines and set proper z-indexes.
	$$('.home_feature_h1').each(function(headline) {
		headline.style.display = 'none';
		headline.style.zIndex = 50;
	} );
	$$('.home_feature_h3').each(function(headline) {
		headline.style.display = 'none';
		headline.style.zIndex = 50;
	} );
	
	// Hide all pics, except on the first runthrough
	$$('.home_feature_pic').each(function(image) {
		if (firstRun != 1) {
			image.style.display = 'none';
		}
		image.style.zIndex = 10;
	} );
	
	// Hide all blurs if includeBlurs is set (when user clicks on a thumbnail).
	// Otherwise set z-index and blur will hide when next image appears.
	$$('.home_feature_pic_blur').each(function(blur) {
		if (includeBlur == 'includeBlur') {
			blur.style.display = 'none';
		}
		blur.style.zIndex = 10;
	} );
	
	// Set all thumbnails to full opacity
	$$('.home_feature_thumb').each(function(thumb) {
		thumb.style.opacity = 1.0;
		if (isIE) {
			thumb.filters.alpha.opacity = 100;
		}
	} );
}

// The main function to show the image set and headlines.
function showNextImageSet(currentImageSetNum) {
	// Ensure we're starting with a clean slate, except keep previous blur so there's no black in between image sets
	clearDecks('noIncludeBlur');
	
	// Define previous and current image set numbers. Assume there are always seven images.
	if (currentImageSetNum == 1) {
		previousImageSetNum = 7;
	} else {
		previousImageSetNum = currentImageSetNum - 1;
	}
	
	// Define previous images and headlines.
	previousImage = 'home_feature_pic_' + previousImageSetNum + 'a';
	previousImageBlur = 'home_feature_pic_' + previousImageSetNum + 'b';
	previousImageH1 = 'home_feature_headline_' + previousImageSetNum + 'a';
	previousImageH2 = 'home_feature_headline_' + previousImageSetNum + 'b';
	previousImageH3 = 'home_feature_headline_' + previousImageSetNum + 'c';
	previousImageThumb = 'home_feature_thumb_' + previousImageSetNum;
	previousImageHeadlineGroup = 'home_feature_headlines_' + previousImageSetNum;

	// Define current images and headlines.
	currentImage = 'home_feature_pic_' + currentImageSetNum + 'a';
	currentImageBlur = 'home_feature_pic_' + currentImageSetNum + 'b';
	currentImageH1 = 'home_feature_headline_' + currentImageSetNum + 'a';
	currentImageH2 = 'home_feature_headline_' + currentImageSetNum + 'b';
	currentImageH3 = 'home_feature_headline_' + currentImageSetNum + 'c';
	currentImageThumb = 'home_feature_thumb_' + currentImageSetNum;
	currentImageHeadlineGroup = 'home_feature_headlines_' + currentImageSetNum;

	// Set thumbnail opacity.
	$(previousImageThumb).style.opacity = 1.0;
	$(currentImageThumb).style.opacity = activeThumbOpacity;
	if (isIE) {
		$(currentImageThumb).filters.alpha.opacity = activeThumbOpacityIE;
	}
	
	// Set z-index of current images and headlines higher than previous images and headlines.
	$(previousImage).style.zIndex = 10;
	$(previousImageBlur).style.zIndex = 10;
	$(previousImageHeadlineGroup).style.zIndex = 50;
	$(currentImage).style.zIndex = 100;
	$(currentImageBlur).style.zIndex = 100;
	$(currentImageHeadlineGroup).style.zIndex = 150;

	// On the first run-through, shorten the delay by 1.5 seconds so everything loads faster
	// (because the pic has already been up for a couple of seconds while the rest of the page is loading)
	if (firstRun == 1) {
		delayModifier = -1500;
		firstRun = 0;
	} else {
		delayModifier = 0;
	}
	
	// Fade in current image, hide previous image and blur after done
	Effect.Appear(currentImage, { duration: picFadeInDuration, queue: 'end', afterFinish: function() { $(previousImage).style.display = 'none'; $(previousImageBlur).style.display = 'none'; } } );
	
	// Fade in current image blur and headlines
	blurDelay = setTimeout("Effect.Appear(currentImageBlur, { duration: picFadeInDuration, queue: 'end' } );", timeToPicBlur + delayModifier);
	h1Delay = setTimeout("Effect.Appear(currentImageH1, { duration: headlineFadeInDuration, queue: 'end', beforeStart: function() { $(currentImageHeadlineGroup).style.display = 'block'; } } );", timeToFirstHeadline + delayModifier);
	h2Delay = setTimeout("Effect.Appear(currentImageH2, { duration: headlineFadeInDuration, queue: 'end' } );", timeToSecondHeadline + delayModifier);
	h3Delay = setTimeout("Effect.Appear(currentImageH3, { duration: headlineFadeInDuration, queue: 'end' } );", timeToThirdHeadline + delayModifier);
	
	// Increment to next image set num
	if (currentImageSetNum == 7) {
		currentImageSetNum = 1;
	} else {
		currentImageSetNum++;
	}
	
	// Fade out current headlines and start again
	clearHeadlineGroupDelay = setTimeout("Effect.Fade(currentImageHeadlineGroup, { duration: headlineFadeOutDuration, afterFinish: function() { checkForImagePreloading(" + currentImageSetNum + "); }, queue: 'end' } );", timeToFadeAll + delayModifier);
}

// Function that runs when you click on a thumbnail to interrupt normal animation flow and skip to another image set
// Will do nothing if firstRun is set (meaning the images haven't finished loading yet)
function skipToImageSet(numSet) {
	if (firstRun != 1) {
		clearDecks('includeBlur');
		showNextImageSet(numSet);
	} else {
		$('home_feature_thumbs_not_ready_message').style.display = 'block';
	}
}

// Loops through header images to see if they've finished loading yet.
function checkForImagePreloading(currentImageSetNum) {
	areImagesPreloaded = 1;
	// Only check if the current set of images has loaded.
	thisPicSet = '.set_' + currentImageSetNum;
	for (image in $$(thisPicSet)) {
		if (image.complete == 0) {
			areImagesPreloaded = 0;
			break;
		}
	}
	// If images have been loaded, stop repeating the function and display first image set
	if (areImagesPreloaded) {
		clearInterval(imagePreloadDelay);
		$('home_feature_thumbs_not_ready_message').style.display = 'none';
		showNextImageSet(currentImageSetNum);
	// Otherwise, wait 1/10 of a second and try again
	} else {
		setInterval("checkForImagePreloading(currentImageSetNum);",100);
	}
}

// Check if sIFR headlines have been loaded yet.
// These need to load in a non-hidden layer in order to work. So by default they load in a z-index behind black background so they're not visible.
// Once the sIFR headlines have loaded, hide them and set the z-index so they're in front of the images. 
function checkForSIFRLoading() {
	// sIFR headlines need to be displayed in order to work. Once they're loaded, hide them.
	if (FlashDetect.installed) {
		if (sIFR.isActive) {
			$$('.home_feature_h1').each(function(headline) {
				headline.style.display = 'none';
			} );
			$('home_feature_headlines_container').style.zIndex = '150';
			clearInterval(sIFRLoadDelay);
		}
	} else {
		$$('.home_feature_h1').each(function(headline) {
			headline.style.display = 'none';
		} );
		$('home_feature_headlines_container').style.zIndex = '150';
		clearInterval(sIFRLoadDelay);
	}
}

// Main function that fires on page load. Checks for sIFR loading and image preloading 10 times per second until all is loaded.
function rotateHomeImages() {
	sIFRLoadDelay = setInterval("checkForSIFRLoading();", 100);
	imagePreloadDelay = setInterval("checkForImagePreloading(1);", 100);
}