$(document).ready(function(){

$(".main_image .desc").show(); //Show Banner
$(".main_image .block").animate({ opacity: 0.85 }, 1 ); //Set Opacity

$(".image_thumb ul li:first").addClass('active'); //Add the active class (highlights the very first list item by default)
$(".image_thumb ul li:last").addClass('last'); // Adds a class ‘last’ to the last li to let the rotator know when to return to the first

$(".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"
	
	//NOTE BY VICTOR
	//Image alt is set to 'image{#}' for each imgAlt. This is then set to the id for the active image.
	//This id will make sure the preloaded image with that ID is used instead of loading the iamge over again.
	//See image_rollovers.js in the /_js/ folder.

    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...
		$(".main_image img").animate({ opacity: 0}, 250 );
        //Animate the Description
       $(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250 , function() { //Pull the block down (negative bottom margin of its own height)
            $(".main_image .block").html(imgDesc).animate({ opacity: 0.85,  marginBottom: "0" }, 250 ); //swap the html of the block, then pull the block container back up and set opacity
           $(".main_image img").attr({ src: imgTitle , alt: imgAlt, name: imgAlt, id: imgAlt}).animate({ opacity: 1}, 250 ); //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
});

// * Code added by Jeff Schram
// * if we are hovering over the image area, pause the clickNext function
// * by default, our new pauseClickNext variable is false
pauseClickNext = false;
$(".main_image").hover(
function () {
pauseClickNext = true;
},
function () {
pauseClickNext = false;
}
);

// * Code added by Jeff Schram
// * Define function to click the next li
// * notice that it checks for a class of ‘last’, we added that above
var clickNext = function(){
if(!pauseClickNext) {
/// find the next li after .active
var $next_li = $("li.active").next("li");
if($("li.active").hasClass("last") ){
$(".image_thumb ul li:first").trigger("click");
} else {
$next_li.trigger("click");
}
}
};

// * Code added by Jeff Schram
// * setTimeInterval to run clickNext
setInterval(clickNext, 6000);

 });// JavaScript Document