//Main functions for after page loads
$(document).ready(function() {
	$(".featured_image .desc").show(); //Show Banner
	$(".featured_image .block").animate({ opacity: 0.85 }, 1 ); //Set Opacity
	
	//Set first tab to 'active'
	$(".featured_thumb ul li:first").addClass('active'); 
	
	//Set tab opacity
	$(".featured_thumb ul li a").animate({ opacity: 0.65 }, 1 ); //Set Opacity
	$(".featured_thumb ul li a:first").animate({ opacity: 1 }, 1 ); //Set Opacity
	
	//Load initial
	var InitialFeaturedDesc = $(".featured_thumb li:first .block").html();
	$(".featured_image .desc .block").html(InitialFeaturedDesc);
	
	var InitialFeaturedImg = $(".featured_thumb li:first a").attr("name");
	$(".featured_image img").attr({src: InitialFeaturedImg})
	
	//var InitialFeaturedImgLink = $(".featured_thumb li:first a").attr("href");
	var InitialFeaturedImgLink = $(".featured_thumb li:first div.picture a").attr("href");
	$(".featured_image a").attr({href: InitialFeaturedImgLink})
	
	//Set speed & start timer for tab rotation
	var speed1 = 5000; //initial
	var speed2 = 5000; //after mouseout
	var rotation = setInterval('tabSwitch()', speed1);
	
	//Hover events for tab list
	$(".featured_thumb ul li").hover(function() { 
		if ($(this).is(".active")) {  //If it's already active, then...
			//Stop the rotation
			clearInterval(rotation);
		}
		else {
			//Stop current annimation
			$(".featured_image img").stop();
			$(".featured_image .block").stop();
			
			//Change tab styling
			$(".featured_thumb ul li").removeClass('active'); //Remove class of 'active' on all tabs
			$(this).addClass('active');  //Add class of 'active' on this tab only
			
			//Set tab opacity
			$(".featured_thumb ul li a").animate({ opacity: 0.65 }, 1 );
			$(".featured_thumb ul li.active a").animate({ opacity: 1 }, 1 );
			
			//Set variables
			var imgAlt = $(this).find('img').attr("alt");	//Get Alt Tag of Image
			var imgTitle = $(this).find('a').attr("name");	//Get Main Image URL
			
			//var imgLink = $(this).find('a').attr("href");	//Get Link Location
			var imgLink = $(this).find('div.picture a').attr("href");	//Get Link Location from hiddden picture
			
			var imgDesc = $(this).find('.block').html(); 	//Get HTML of the "block" container
			var imgDescHeight = $(".featured_image").find('.block').height(); //Calculate height of "block"
			
			//Animate the image & description
			$(".featured_image img").animate({ opacity: 0}, 400 );
			$(".featured_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 400 , function() {
				$(".featured_image .block").html(imgDesc).animate({ opacity: 0.85, marginBottom: "0" }, 400 );
				$(".featured_image img").attr({ src: imgTitle , alt: imgAlt}).animate({ opacity: 1}, 400 );
				//$(".featured_image img").attr({ src: imgTitle , alt: imgAlt});
				$(".featured_image a#imgLink").attr({ href: imgLink });
			});
			
			//Stop the rotation
			clearInterval(rotation);
		}
		
		return false;
	
	}, //Close mouseover -- Start mouseout
		function() {
			rotation = setInterval('tabSwitch()', speed2);
		} //Close mouseout
	);
	
}); //Close main 'document ready' function


//Additional functions
function tabSwitch() {
	//Define new 'active' tab
	var $active = $('.featured_thumb ul li.active').next();
	if ( $active.length == 0 ) $active = $('.featured_thumb ul li:first'); //goes back to start when finishes
	
	//Define previous 'active' tab
	var $prev = $('.featured_thumb ul li.active');
	
	//Change tab styling
	$prev.removeClass('active');
	$active.addClass('active');
	
	//Change tab opacity
	$(".featured_thumb ul li a").animate({ opacity: 0.65 }, 1 );
	$(".featured_thumb ul li.active a").animate({ opacity: 1 }, 1 );
	
	//Set variables
	var imgAlt = $active.find('img').attr("alt");	//Get Alt Tag of Image
	var imgTitle = $active.find('a').attr("name");	//Get Main Image URL
	
	//var imgLink = $active.find('a').attr("href");	//Get Link Location
	var imgLink = $active.find('div.picture a').attr("href");	//Get Link Location from hiddden picture
	
	var imgDesc = $active.find('.block').html();	//Get HTML of the "block" container
	var imgDescHeight = $(".featured_image").find('.block').height(); //Calculate height of "block"
	
	if ($(this).is(".active")) { //If the list item is active/selected, then...
		return false; // Don't click through – Prevents repetitive animations on active/selected list-item
	}
	else { //If not active then...
		//Animate the image & description
		$(".featured_image img").animate({ opacity: 0}, 400 );
		$(".featured_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 400 , function() {
			$(".featured_image .block").html(imgDesc).animate({ opacity: 0.85, marginBottom: "0" }, 400 );
			$(".featured_image img").attr({ src: imgTitle , alt: imgAlt}).animate({ opacity: 1}, 400 );
			$(".featured_image a#imgLink").attr({ href: imgLink });
		});
	}
	
	return false;
}


//More functions that cannot load until page is complete
$(document).ready(function() {
	//NextTab link will switch to next tab
	$("a.NextTab").click(function() {
		tabSwitch();
	});
});

