// JavaScript Document
$(document).ready(function() {

	var rotateSpeed = 6000;
	var rotateTimer = setTimeout(AutoRotate,rotateSpeed);
    var delayTimer = null;

	var curItem = $('.image_thumb ul li').first();
    

	$('.image_thumb ul li').bind('mouseover', function() {
        if($(this).hasClass('over') || $(this).hasClass('active')) return;
        $('.image_thumb ul li.over').removeClass('over');
        $(this).addClass('over');

        var hover_item = $(this);
        
        clearTimeout( rotateTimer );
        clearTimeout( delayTimer );

        delayTimer = setTimeout(function(){
            NextImage(hover_item);
        }, 400);
	  	
	});

	$(".image_thumb ul li").click(function(event) {
        var page_url = $('.block h2 a', event.currentTarget).first().attr('href');
        window.location = page_url;
	  	//event.preventDefault();
		//this prevents the thumbnail images from linking to the image file
	});

	$(".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)

	function AutoRotate() {
        $('.image_thumb ul li.over').removeClass('over');
		NextImage( GetNextImgObj() );
	}

	function GetNextImgObj() {
		//this function cycles through all of the 'li' tags which contains the thumbnails
		if ( $(curItem).next().is('li') )   
			curItem = $(curItem).next();
		else 
			curItem = $('.image_thumb ul li').first();
		return curItem;
	}

	function NextImage(imgObj) {
		//Set Variables
		clearTimeout( rotateTimer ); //this in needed in case the user clicks a thumbnail, reset the timer
		var imgAlt = imgObj.find('img').attr("alt"); //Get Alt Tag of Image
		var imgTitle = imgObj.find('a').attr("rel"); //Get Main Image URL
		var imgDesc = imgObj.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
			$(".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").fadeOut('fast', function() {
					$(".main_image img").attr({ src: imgTitle , alt: imgAlt}); //Switch the main image (URL + alt tag)
				 });
				$(".main_image img").fadeIn();
			});
		}

		//Show active list-item
		$(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all list-items
		imgObj.addClass('active');  //Add class of 'active' on the selected list
		curItem = imgObj;
		rotateTimer = setTimeout(AutoRotate,rotateSpeed);	
		return false;
	} 

	$("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)
	});

});//Close Function 
