
var index = 0;				// The index variable will keep track of which image is currently showing
var fadeSpeed = 1000;		// How quickly will we fade?
var switchDelay = 7000; 	// How quickly will we switch to the next image?
var slideshow = null; 		// Slideshow timer varia

// Our slides and headlines
var slides = [
  { img: template_url+"/images/home/splash-home-1.jpg", 
  	headline: "The Crossing at Ghost River inspires you to connect with nature, your peers, yourself, and your potential." },
  { img: template_url+"/images/home/splash-home-2.jpg", 
  	headline: "Enveloped by one of the most breathtaking ecosystems in the world, the everyday pace seems a world apart." },
  { img: template_url+"/images/home/splash-home-3.jpg", 
  	headline: "Endless possibility stretches before you, in a landscape that welcomes and nurtures, inspires and encourages." },
  { img: template_url+"/images/home/splash-home-4.jpg", 
  	headline: "We season breakfast with early morning light, the fresh scent of the valley, the sweet taste of morning dew, and the melody of the river. It’s delicious." },
  { img: template_url+"/images/home/splash-home-5.jpg", 
  	headline: "Connect with your colleagues in a comfortable atmosphere, discover hidden talents, reach a deeper level of focus and unleash your company’s competitive edge - your team." },
  { img: template_url+"/images/home/splash-home-6.jpg", 
  	headline: "Limitless possibilities await you, varying from the thrilling to the tranquil, from the adventuresome to homebound, and everything in between." },
  { img: template_url+"/images/home/splash-home-7.jpg", 
  	headline: "Open windows give way to curtains blowing in the breeze, filling each room with the crisp scent of the mountains and the river, just beyond your windowsill." },
  { img: template_url+"/images/home/splash-home-8.jpg", 
  	headline: "Refined comfort radiates from our two beautiful colonial style buildings. With delightful rooms and comfortable common areas, our house becomes your home." }
];

function initFrontPage () {
	
	// Call backstretch for the first time,
	switchImage();
	
	// Set an interval that increments the index and sets the new image
	startInterval();
	
	// Rotate the slide based on the order of the button clicked
	$("#rotateButtons a").click(function(e) {
		// Prevent the page from trying to visit the href locations
		e.preventDefault();
				
		$("#rotateButtons img").attr('src',template_url+'/images/button-off.png');
        $(this).attr('src',template_url+'/images/button-on.png');
        
		var clickedIndex = $("#rotateButtons a").index(this);        
		switchImage(clickedIndex);
		
		// Reset the slideshow interval
		clearInterval(slideshow);
		startInterval();
	});
    	
	// hover on buttons
	$("#rotateButtons img").hover(
		function () {
			$(this).attr('src',template_url+'/images/button-on.png');
		}, 
		function () {
			$(this).attr('src',template_url+'/images/button-off.png');
			$('#photo'+(index+1)).attr('src',template_url+'/images/button-on.png');
		}
	);   
	
	// Preload the other images
	$(slides).each(function(){
		$('<img/>')[0].src = this.img;
	});
}

function startInterval(){
	slideshow = setInterval(function() {
		index = (index >= slides.length - 1) ? 0 : index + 1;
		switchImage();
	}, switchDelay);	
}

function switchImage(slideNum) {
   
	if (slideNum != undefined) index = slideNum;
	
	$('#fading-elements').fadeOut((fadeSpeed/2), function() {
		
		$.backstretch(slides[index].img, {speed: fadeSpeed, effect: "fade"}, function() {
			$("#rotateButtons img").attr('src',template_url+'/images/button-off.png');
			$("#photo"+(index+1)).attr('src',template_url+'/images/button-on.png');
		});	
		
		$("#headline-quote").html(slides[index].headline);
		$('#fading-elements').fadeIn((fadeSpeed/2));
	});
}
