$(document).ready(function() {

	$('#featureleftB').fadeTo(0, 0);
	$('#featureleftC').fadeTo(0, 0);
	$('#featurerightB').fadeTo(0, 0);
	$('#featurerightC').fadeTo(0, 0);
	
	$('#mmheada').fadeTo(0, 0);
	$('#mmheadb').fadeTo(0, 0);
	$('#mmheadc').fadeTo(0, 0);
	$('#mmcopya').fadeTo(0, 0);
	$('#mmcopyb').fadeTo(0, 0);
	
	$('#tileholderb').fadeTo(0, 0);
	
	var whichanimation = 1;
	var whichtile = 2;
	var juststarted = 1;
	var thecasestudylink = "case_armstrong.html";
	var howmanyanimations = 9;
	
	function animation(){
		
		if (whichanimation < howmanyanimations) {
			whichanimation++;
			} else {
			whichanimation = 1;
			}
		
		if (whichtile<2) {
			whichtile++;
			} else {
			whichtile = 1;
			}	

		if (juststarted<2) {

		setTimeout( function (){
			$("#featureleftB").animate({ opacity: 1 }, 2000);
		}, 4000);
	
		setTimeout( function(){
			$("#featureleftC").animate({ opacity: 1 }, 2000);
		}, 6000);
	
		setTimeout( function(){
			$("#featureleftC").animate({ opacity: 0 }, 1000);
		}, 12500);
	
		setTimeout( function(){
			$("#featurerightB").animate({ opacity: 1 }, 2000);
		}, 12500);
		
		var storethevar = whichanimation;
		
		if (whichanimation < howmanyanimations) {
			var storethevarforphoto = whichanimation+1;
		} else {
			var storethevarforphoto = 1;
		}
		var storethetile = whichtile;	
				
		setTimeout( function(){
			$("#featureleftA").html('<a href="'+thecasestudylink+'"><img src="images/hp/'+whichanimation+'_L1.jpg" /></a>');
			$("#featurerightA").html('<a href="'+thecasestudylink+'"><img src="images/hp/'+whichanimation+'_R1.jpg" /></a>');
			$("#featurerightC").animate({ opacity: 1 }, 2000);
			var images = [
				'images/hp/'+storethevar+'_L2.jpg',
				'images/hp/'+storethevar+'_R2.jpg',
				'images/hp/'+storethevar+'_L3.jpg',
				'images/hp/'+storethevar+'_R3.jpg',
			];

			$(images).each(function() {
				var image = $('<img />').attr('src', this);
			});
		}, 14500);
		
		setTimeout( function(){
			$("#featureleftB").animate({ opacity: 0 }, 2000);
			$("#featurerightB").animate({ opacity: 0 }, 2000);
			$("#featurerightC").animate({ opacity: 0 }, 2000);
			if (storethetile<2) {
				$("#tileholderb").animate({ opacity: 1 }, 0);
				$("#tileholdera").animate({ opacity: 0 }, 2000, function() {
					$("#tileholdera").html('<img src="images/tiles/hp_'+storethevarforphoto+'.jpg" height="1172" width="60"  />');
					$("#tileholdera").attr("class", "thetilebehind");
					$("#tileholderb").attr("class", "thetileinfront");
				});
				} else {
				$("#tileholdera").animate({ opacity: 1 }, 0);
				$("#tileholderb").animate({ opacity: 0 }, 2000, function() {
					$("#tileholderb").html('<img src="images/tiles/hp_'+storethevarforphoto+'.jpg" height="1172" width="60"  />');
					$("#tileholderb").attr("class", "thetilebehind");
					$("#tileholdera").attr("class", "thetileinfront");
				});
			}
		}, 21000);
		setTimeout( function(){
			$("#featureleftB").html('<a href="'+thecasestudylink+'"><img src="images/hp/'+storethevar+'_L2.jpg" /></a>');
			$("#featurerightB").html('<a href="'+thecasestudylink+'"><img src="images/hp/'+storethevar+'_R2.jpg" /></a>');
			$("#featureleftC").html('<a href="'+thecasestudylink+'"><img src="images/hp/'+storethevar+'_L3.jpg" /></a>');
			$("#featurerightC").html('<a href="'+thecasestudylink+'"><img src="images/hp/'+storethevar+'_R3.jpg" /></a>');
			juststarted = 5;
		}, 23000);	

			} else {

		setTimeout( function (){
			$("#featureleftB").animate({ opacity: 1 }, 2000);
		}, 2000);
	
		setTimeout( function(){
			$("#featureleftC").animate({ opacity: 1 }, 2000);
		}, 4000);
	
		setTimeout( function(){
			$("#featureleftC").animate({ opacity: 0 }, 1000);
		}, 10500);
	
		setTimeout( function(){
			$("#featurerightB").animate({ opacity: 1 }, 2000);
		}, 10500);
			
		var storethevar = whichanimation;
		
		if (whichanimation < howmanyanimations) {
			var storethevarforphoto = whichanimation+1;
		} else {
			var storethevarforphoto = 1;
		}
		var storethetile = whichtile;
		var storethecase = whichanimation-1;
		
		if (storethecase==1){
			thecasestudylink = "case_archer.html";
			} else if (storethecase==2) {
			thecasestudylink = "case_armstrong.html";
			} else if (storethecase==3) {
			thecasestudylink = "case_temple.html";
			} else if (storethecase==4) {
			thecasestudylink = "case_sevenhills.html";
			} else if (storethecase==5) {
			thecasestudylink = "case_cwsl.html";
			} else if (storethecase==6) {
			thecasestudylink = "case_leelanau.html";
			} else if (storethecase==7) {
			thecasestudylink = "case_messiah.html";
			} else if (storethecase==8) {
			thecasestudylink = "case_georgiasouthern.html";
			} else  {
			thecasestudylink = "case_portsmouthabbey.html";
			}
		
		setTimeout( function(){
			$("#featureleftA").html('<a href="'+thecasestudylink+'"><img src="images/hp/'+whichanimation+'_L1.jpg" /></a>');
			$("#featurerightA").html('<a href="'+thecasestudylink+'"><img src="images/hp/'+whichanimation+'_R1.jpg" /></a>');
			$("#featurerightC").animate({ opacity: 1 }, 2000);
			var images = [
				'images/hp/'+storethevar+'_L2.jpg',
				'images/hp/'+storethevar+'_R2.jpg',
				'images/hp/'+storethevar+'_L3.jpg',
				'images/hp/'+storethevar+'_R3.jpg',
			];

			$(images).each(function() {
				var image = $('<img />').attr('src', this);
			});
		}, 12500);
		
		setTimeout( function(){
			$("#featureleftB").animate({ opacity: 0 }, 2000);
			$("#featurerightB").animate({ opacity: 0 }, 2000);
			$("#featurerightC").animate({ opacity: 0 }, 2000);
			if (storethetile<2) {
				$("#tileholderb").animate({ opacity: 1 }, 0);
				$("#tileholdera").animate({ opacity: 0 }, 2000, function() {
					$("#tileholdera").html('<img src="images/tiles/hp_'+storethevarforphoto+'.jpg" height="1172" width="60"  />');
					$("#tileholdera").attr("class", "thetilebehind");
					$("#tileholderb").attr("class", "thetileinfront");
				});
				} else {
				$("#tileholdera").animate({ opacity: 1 }, 0);
				$("#tileholderb").animate({ opacity: 0 }, 2000, function() {
					$("#tileholderb").html('<img src="images/tiles/hp_'+storethevarforphoto+'.jpg" height="1172" width="60"  />');
					$("#tileholderb").attr("class", "thetilebehind");
					$("#tileholdera").attr("class", "thetileinfront");
				});
			}
		}, 19000);
		setTimeout( function(){
			$("#featureleftB").html('<a href="'+thecasestudylink+'"><img src="images/hp/'+storethevar+'_L2.jpg" /></a>');
			$("#featurerightB").html('<a href="'+thecasestudylink+'"><img src="images/hp/'+storethevar+'_R2.jpg" /></a>');
			$("#featureleftC").html('<a href="'+thecasestudylink+'"><img src="images/hp/'+storethevar+'_L3.jpg" /></a>');
			$("#featurerightC").html('<a href="'+thecasestudylink+'"><img src="images/hp/'+storethevar+'_R3.jpg" /></a>');
		}, 21000);
		}	
	};
	
	animation();
	
	setInterval(function(){
	animation();
	}, 21000);

		
});
