﻿

var m_CarouselItemsCount;				// Total number of items in carousel
var m_CarouselIndexScheduled = 1;		// The index of the item to switch to on a scheduled change
var m_CarouselIndexUnscheduled = 2;		// The index of the item to switch to on an unscheduled change
var m_CarouselIndex;					// The master index of the item to switch to
var m_CarouselIndexNext;				// The master index of the item after the item to switch to
var m_CarouselWait = 8000;				// The wait between scheduled transitions
var m_CarouselSpeed = 1000;				// The length of time taken for a schedulled transition
var m_Clicked = 0;						// =1 when a click has interrupted a scheduled animation
var m_AnimationRunning = 0;				// =1 when an animation is running
var m_MainImageUrl;						// The URL of the main image to display
var m_MainImageAlt;						// The alt text of the main image
var m_NextImageUrl;						// The URL of the next image in the slideshow
var m_NextImageAlt;						// The alt text of the next image in the slideshow
var m_Title;							// The (html) Title of the currently displayed carousel item
var m_Body;								// The (html) body text of the currently displayed carousel item
var m_Link;								// The (html) link of the currently displayed carousel item
var m_ThumbBg;							// Stores the initial URL of the thumbnail's background image before hover event
var m_Timeout;							// The master timeout variable.  Clear this to prevent future animations.

// Gets the values required to change the slideshow
// Constructor indicates if action is scheduled (1) or unscheduled (0)
function SetValues(m_Scheduled) {
	
	if (m_Scheduled == 1) {
		m_CarouselIndex = m_CarouselIndexScheduled
	}
	else {
		m_CarouselIndex = m_CarouselIndexUnscheduled
	}
		
	if (m_CarouselIndex < m_CarouselItemsCount) {
		m_CarouselIndexNext = parseInt(m_CarouselIndex) + 1;
	}
	else {
		m_CarouselIndexNext = 1;
	}
	    m_MainImageUrl = jQuery('.CarouselItems div:nth-child(' + m_CarouselIndex + ')').children('.CarouselItemMainImage').find("img").attr("src");
	    m_MainImageAlt = jQuery('.CarouselItems div:nth-child(' + m_CarouselIndex + ')').children('.CarouselItemMainImage').find("img").attr("alt");
	    m_Title = jQuery('.CarouselItems div:nth-child(' + m_CarouselIndex + ')').children('.CarouselItemTitle').html();
	    m_Body = jQuery('.CarouselItems div:nth-child(' + m_CarouselIndex + ')').children('.CarouselItemBody').html();
	    m_Link = jQuery('.CarouselItems div:nth-child(' + m_CarouselIndex + ')').children('.CarouselItemLink').html();
	    m_NextImageUrl = jQuery('.CarouselItems div:nth-child(' + m_CarouselIndexNext + ')').children('.CarouselItemMainImage').find("img").attr("src");
	    m_NextImageAlt = jQuery('.CarouselItems div:nth-child(' + m_CarouselIndexNext + ')').children('.CarouselItemMainImage').find("img").attr("alt");

    //Change text fields
	jQuery('.CarouselTitle').html(m_Title);
	jQuery('.CarouselBody').html(m_Body);
	jQuery('.CarouselFindOutMoreContainer').children().find("li").html(m_Link);

	//Reset the reel
	jQuery('.CarouselVisible').find("img").attr("src", m_MainImageUrl);
	jQuery('.CarouselVisible').find("img").attr("alt", m_MainImageAlt);
	jQuery('.CarouselNotVisible').find("img").attr("src", m_NextImageUrl);
	jQuery('.CarouselNotVisible').find("img").attr("alt", m_NextImageAlt);
	jQuery('.CarouselReel').animate({ left: 0 }, 0);
	
	//Clear existing selected thumbs
	jQuery('.CarouselGalleryThumbs').children().css("background-image", "url(/images/CarouselThumbBGOff.gif)");
	//Set new selected thumb
	jQuery('.CarouselGalleryThumbs').children('.CarouselGalleryThumbs div:nth-child(' + m_CarouselIndexScheduled + ')').css("background-image", "url(/images/CarouselThumbBGOn.gif)");
	
};
function Animate() {

	m_Clicked = 0;
	m_AnimationRunning=1;
		jQuery('.CarouselReel').animate({ left: -598 }, m_CarouselSpeed,
	      		      function() {
					  	m_AnimationRunning=0;
	      		      	if (m_CarouselIndexScheduled < m_CarouselItemsCount) {
	      		      		m_CarouselIndexScheduled++;
	      		      	}
	      		      	else {
	      		      		m_CarouselIndexScheduled = 1;
	      		      	}  		      	
	      		      	SetValues(1);
	      		      	m_Timeout = setTimeout("Animate()", m_CarouselWait);
	      		      });
};

jQuery(document).ready(function() {


	//Count the number of slideshow items
	m_CarouselItemsCount = jQuery('.CarouselItems').children().length;
	SetValues(1);
	m_Timeout = setTimeout("Animate()", m_CarouselWait);

	//Mouseover
	jQuery(".CarouselGalleryThumb").hover(
      function() {
		if (m_AnimationRunning ==0){
			m_ThumbBg = jQuery(this).css("background-image");
			//if (m_Clicked == 0) {
				m_CarouselIndexUnscheduled = (this.className).substring(((this.className).search("__")) + 3, (this.className).length);
				SetValues(0);
				jQuery(this).css("background-image", "url(/images/CarouselThumbBGHover.gif)");
				clearTimeout(m_Timeout);
			//}
			return false;
		}
      },
      //Mouseout
      function() {
  	    if (m_AnimationRunning ==0){
		    clearTimeout(m_Timeout);
		    //if (m_Clicked == 0) {
			    jQuery(this).css("background-image", m_ThumbBg);
			    SetValues(1);
		    //}
		    m_Timeout = setTimeout("Animate()", m_CarouselWait);
		    return false;
	    }
      }
    );

	jQuery(".CarouselGalleryThumb").click(
      function() {
	  			if (m_AnimationRunning ==0){
      	m_Clicked = 1;
      	clearTimeout(m_Timeout);
      	jQuery(this).css("background-image", "url(/images/CarouselThumbBGOn.gif)");
      	m_CarouselIndexScheduled = (this.className).substring(((this.className).search("__")) + 3, (this.className).length);
      	SetValues(1);
      	m_Timeout = setTimeout("Animate()", m_CarouselWait);
		}
      }
	);
});

