$(window).load(function() {

	// Grab the slides for the hidden ul in the page.
	var slides = $('#careershow li img');
	var timerValue;
	var timerInterval = 6000; // Switch every 6 seconds
	var totalSlides = slides.size();
	var fadeSpeed = 1000;

	// Pointers to the next, current, and previous slides in the show.
	var nextIndex = 1
	var thisIndex = 0
	var prevIndex = totalSlides - 1;

	// Deal with a random quote
	randomQuote();

	// Fire everything up once the page is loaded.
	init();

	// Set the links on the navigation arrows.
	navArrowSetup();

	// Select one of the employee quotes at "random".
	function randomQuote() {
		var quotes = $('.careers_quote');
		var quoteSize = quotes.size();
		var quotePick = Math.floor(Math.random() * quoteSize);
		$(quotes[quotePick]).css({ 'display': 'block' });
	}

	// Binds the mouse events to the navigation arrows.
	// The mouse events result in animation of the arrows.
	function navArrowSetup() {
		$('#careerdiv').mouseenter(function() {
				$('#careers_prev_arrow').stop(true, true).animate({
						opacity: 1
					}, 500);
				$('#careers_next_arrow').stop(true, true).animate({
						opacity: 1
					}, 500);
			});
		$('#careerdiv').mouseleave(function() {
				$('#careers_prev_arrow').stop(true, true).animate({
						opacity: 0
					}, 500);
				$('#careers_next_arrow').stop(true, true).animate({
						opacity: 0
					}, 500);
			});
	}

	// Fire everything up.
	function init() {
		// All the slides except the first one need to be faded out
		$(slides).each(function(idx) { if (idx > 0) { $(this).appendTo('#careerdiv'); $(this).fadeOut(1); } });
		// Now append the first slide for viewing
		$('#careerdiv').append(slides[thisIndex]);
		$(slides[thisIndex]).css({ 'position': 'absolute', 'left': 0, 'top': 0 });
		$(slides[thisIndex]).fadeIn(fadeSpeed);
		setArrowLinks();
		timerValue = setTimeout(function() { swapSlides(thisIndex, nextIndex); }, timerInterval);
	}

	// Do the actual fade in and out, with an option parm to stop
	// the automatic fading from one slide to the next.
	function swapSlides(outIndex, inIndex, stopAnim) {
		$('#careerdiv').append(slides[inIndex]);
		// Reset the style for a smooth fade
		$(slides[inIndex]).css({ 'position': 'absolute', 'left': 0, 'top': 0 });
		// Concurrent fade in and out
		$(slides[inIndex]).fadeIn(fadeSpeed);
		$(slides[outIndex]).fadeOut(fadeSpeed);
		// Set up for the next slide fade
		if (!stopAnim) {
			// Keep animating
			incrementIndex();
			timerValue = setTimeout(function() { swapSlides(thisIndex, nextIndex); }, timerInterval);
		}
		// Reset the links on the navigation arrows.
		setArrowLinks();
	}

	// Reset the links on the navigation arrows.
	// Stops all automatic fading, then fades in the next or previous
	// slide, depending on the arrow clicked.
	function setArrowLinks() {
		 $('.slideshow_arrow_prev').unbind('click');
		$('.slideshow_arrow_prev').click(function() {
				clearTimeout(timerValue);
				fadeSpeed = 600;
				swapSlides(thisIndex, prevIndex, true);
				decrementIndex();
				setArrowLinks();
				 $('#careerdiv').unbind('mouseenter');
				 $('#careerdiv').unbind('mouseleave');
				return false;
			});
		 $('.slideshow_arrow_next').unbind('click');
		$('.slideshow_arrow_next').click(function() {
				clearTimeout(timerValue);
				fadeSpeed = 600;
				swapSlides(thisIndex, nextIndex, true);
				incrementIndex();
				setArrowLinks();
				 $('#careerdiv').unbind('mouseenter');
				 $('#careerdiv').unbind('mouseleave');
				return false;
			});
	}

	// Set up the next slide in the show (automatic fade, or when the
        // next arrow is clicked).
	function incrementIndex() {
		thisIndex++;
		if (thisIndex == totalSlides) {
			thisIndex = 0;
		}
		nextIndex++;
		if (nextIndex == totalSlides) {
			nextIndex = 0;
		}
		prevIndex++;
		if (prevIndex == totalSlides) {
			prevIndex = 0;
		}
	}

	// Set up the previous slide in the show (when the previous arrow
	// is clicked).
	function decrementIndex() {
		thisIndex--;
		if (thisIndex < 0) {
			thisIndex = totalSlides - 1;
		}
		nextIndex--;
		if (nextIndex < 0) {
			nextIndex = totalSlides - 1;
		}
		prevIndex--;
		if (prevIndex < 0) {
			prevIndex = totalSlides - 1;
		}
	}

});

