$(document).ready(function(){
	// Opacity variable
		var overlayOp = 0.85;
		
	$(".main_image .desc").show(); //Show Banner
	$(".main_image .block").animate({ opacity: overlayOp }, 1 ); //Set Opacity
	
	$(".image_thumb ul li:first").addClass('active'); //Add the active class (highlights the very first list item by default)
	
	/* On click function */
	$(".image_thumb ul li").click(function(){
		//Set Variables
		var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
		var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
		var imgDesc = $(this).find('.block').html();  //Get HTML of the "block" container
		var imgDescHeight = $(".main_image").find('.block').height(); //Find the height of the "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 Description and the image
			//$(".main_image img").animate({ opacity: 0}, 100 );
			$(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 150 , function() { //Pull the block down (negative bottom margin of its own height)
				$(".main_image .block").html(imgDesc).animate({ opacity: overlayOp,  marginBottom: "0" }, 400 ); //swap the html of the block, then pull the block container back up and set opacity
				$(".main_image img").attr({ src: imgTitle , alt: imgAlt}).animate({ opacity: 1}, 400 ); //Switch the main image (URL + alt tag)
			});
		}
		//Show active list-item
		$(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all list-items
		$(this).addClass('active');  //Add class of 'active' on the selected list
		return false;

	}) .hover(function(){ //Hover effects on list-item
			$(this).addClass('hover'); //Add class "hover" on hover
		}, function() {
			$(this).removeClass('hover'); //Remove class "hover" on hover out
		});
	
	/* Collapse function not used, WIP, remove or add but don't just leave an invisible link */
	$("a.collapse").click(function(){
		$(".main_banner .block").slideToggle(); //Toggle the description (slide up and down)
		$("a.collapse").toggleClass("show"); //Toggle the class name of "show" (the hide/show tab)
	});
	
	/* For Auto Rotation */
	var interval = 7000;
	
	$(".image_thumb ul li").hover( function(){
		firstRun = false;
		clearInterval(playSlideshow);
	}, function(){
		playSlideshow = setInterval("autoRotate()", interval);
	});
	
	var firstRun = true;
	if (firstRun) {
		$(function() { playSlideshow = setInterval("autoRotate()", interval); });
	}
});
	


	function autoRotate() {
		var $active = $(".image_thumb ul li.active");
		var $next = $active.next();
		
		if ($next.length == 0) 
			$next = $('.image_thumb ul li:first'); // if no more next then goes back to the first
		
		//Set Variables
		var imgAlt = $next.find('img').attr("alt"); //Get Alt Tag of Image
		var imgTitle = $next.find('a').attr("href"); //Get Main Image URL
		var imgDesc = $next.find('.block').html();  //Get HTML of the "block" container
		var imgDescHeight = $(".main_image").find('.block').height(); //Find the height of the "block"

		$(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 150 , function() { //Pull the block down (negative bottom margin of its own height)
				$(".main_image .block").html(imgDesc).animate({ opacity: 0.85,  marginBottom: "0" }, 400 ); //swap the html of the block, then pull the block container back up and set opacity
				$(".main_image img").attr({ src: imgTitle , alt: imgAlt}).animate({ opacity: 1}, 400 ); //Switch the main image (URL + alt tag)
		});
		
		//Show active list-item
		$(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all list-items
		$next.addClass('active');  //Add class of 'active' on the selected list
	}
