	$(document).ready(function() {
// Define/Cache variables
    $.ajaxSetup ({  
      cache: true  
    }); 
		var $caseStudy = $('#case-study');
		var $container = $('#content-sub1');
		var $caseStudies = ['teifels', 'obrien', 'flegeal'];	// an array of each of the potential case-studies
		var $class = $container.attr('class');
		var studyCounter = 0;
		var intervalTime = 10000; // slide transition
		var fadeTime = 500; // fading timer
		var $listItems = $('.case-study-nav li');
		$listItems.css('opacity', '.5');
		$listItems.hover(function() {
			var $class = $(this).parents('#content-sub1').attr('class');
			if($class != $(this).attr('class')) {
				$(this).stop().fadeTo(fadeTime, 1);
			} else {$(this).addClass('notMe');}
		}, function() {
			if($(this).hasClass('notMe')) {$(this).removeClass('notMe');} else {
				$(this).stop().fadeTo(fadeTime, .5);
			}				
		});
		
// When ready hide default content | load first snippet | fade in content | set interval
		$caseStudy.css('display', 'none');
		transitionStudy();
		$caseStudy.fadeIn(fadeTime);
		var refreshInterval = setInterval(incrementStudy, intervalTime);
		
// This function increments the counter | begins fading out content | calls the ajax function
		function incrementStudy() {
			if(studyCounter > 1) { 
				studyCounter = 0;
			} else {studyCounter++;}
			$class = $container.attr('class');
			$container.find('#more-info').animate({'opacity' : '0'}, 1000);
			$('li.'+$class).animate({'opacity' : '.5'}, 1000);
			var iS = setTimeout(function() {
				$caseStudy.fadeOut(fadeTime, transitionStudy).delay(1200).fadeIn(fadeTime);
			}, 1000);	
		}
// ajax call to static snippet | callback to adjust { class, opacity, animation }
		function transitionStudy() {
			$caseStudy.load('/case-studies/snippet/' + $caseStudies[studyCounter] + '/', function() {
				$container.attr('class', $caseStudies[studyCounter]);
				$class = $container.attr('class');
				$container.find('li.' + $class).animate({'opacity' : '1'}, 1000);
				$container.find('.call-out, #more-info').css('opacity', '0');
				$container.find('blockquote').css({'opacity' : '0', 'right' : '-500px'}).animate({'right' : '0', 'opacity': '1'}, 1500);
				var tS2 = setTimeout(function() {
					$container.find('.call-out, #more-info').animate({'opacity' : '1'}, 1000);
				}, 500);
			});
		}
// on click...
		$listItems.click(function($e) {
			$e.preventDefault(); // prevent default action of the a element inside the li,
			$class = $(this).attr('class'); // store the current li class value,
			if(!$container.hasClass($class)) { // if the class value does not equal the containers class then:
			$(this).siblings('li').stop().fadeTo(500, .5).end().addClass('notMe'); // adjust opacity of list items
				clearInterval(refreshInterval); // stop the interval timer
				$.each($caseStudies, function(index, value) { // loop through the array
					if(value == $class) { // and once you find the array value that matches the links class...
						studyCounter = index; // grab the index and adjust the random number with that value
					}
				});
				$caseStudy.fadeOut(fadeTime, transitionStudy).fadeIn(fadeTime);
				// return to normal transition interval
				refreshInterval = setInterval(incrementStudy, intervalTime);
			}
		});
	});
