//New Image Carousel for Horizon Home Page

//globals
var gallery_timer = 0;
var timer = 9000;
var cur_image = '';
var play = true;

$(document).ready(function(){
	
	//set the initial interval for scrolling through each slide in the gallery
	gallery_timer = setInterval("scrollImages()", timer);	
	
	//make the first image and associated menu item active
	var first_image = $("#image-container div[index='0']");
	first_image.addClass('active-image');
	var first_item = $("div.nav-box[index='0']");
	first_item.addClass('selected-nav-box');
	var first_fact = $("#happy-fact div[index='0']");
	first_fact.css({display: 'block'});
	
	//make the first image the current one
	cur_image = first_image;
	
	//listener for menu item clicks
	$(".nav-box").click(function(){
				
		if(!$(this).hasClass("selected-nav-box"))
		{
      //reset the timer
      pauseGalleryTimer();
      
			//move the old selected nav item to the new one
			$(".selected-nav-box").removeClass("selected-nav-box");
			$(this).addClass("selected-nav-box");
			
			//get the associated image and hide the current image then show the new one
			var this_index = $(this).attr('index');
			var img_swap = $("#image-container div[index='" + this_index + "']");
			var this_fact = $("#happy-fact div[index='" + this_index + "']");
			cur_image.fadeOut(function(){
				cur_image.removeClass('active-image');
				img_swap.addClass('active-image').fadeIn();
				$(".happy-fact-text").fadeOut('slow', function(){
					this_fact.fadeIn('slow');
				});
				cur_image = img_swap;
        
        if(play == true)
        {
          setGalleryTimer();
        }
			});
			
		}
		
	});
	
	//handle gallery image link clicks
	$("#photo-link").click(function(){
		var this_link = cur_image.find('a').attr('href');
		document.location = this_link;
	});
	
	$("#control-button").click(function(){
		if(play == true)
		{
			pauseGalleryTimer()
			$("#play").show();
			$("#pause").hide();
			play = false;
		}
		else
		{
			gallery_timer = setInterval("scrollImages()", timer);
			$("#pause").show();
			$("#play").hide();
			play = true;
		}
	});
	
});//end of document ready

function scrollImages() {
	
	var image_list = $("#image-container div.image-nav");
	var menu_list = $("#photo-nav div.nav-box");
	var fact_list = $("#happy-fact div.happy-fact-text");
	var active_image = $("#image-container div.active-image");
	var active_menu = $("#photo-nav div.selected-nav-box");
	var index = parseInt(active_image.attr('index'));
	var next_index = (index+1) % image_list.length;
	
  pauseGalleryTimer();
	//get the current image and swap it out with new image
	active_image.fadeOut('slow',function(){
		active_image.removeClass('active-image');
		cur_image = image_list.eq(next_index);
		image_list.eq(next_index).addClass('active-image').fadeIn('slow');
		active_menu.removeClass('selected-nav-box');
		menu_list.eq(next_index).addClass('selected-nav-box');
		
		$("#happy-fact div[index='" + index + "']").fadeOut('fast', function(){
			fact_list.eq(next_index).fadeIn();
		});
  
    setGalleryTimer();
	});
}

function setGalleryTimer() { 
  clearInterval(gallery_timer);
  gallery_timer = null;
  gallery_timer = setInterval("scrollImages()", timer);
}

function pauseGalleryTimer() {
	clearInterval(gallery_timer);
	gallery_timer = null;
}

