/*
   ##########################################################
   # Slideshow Script for Charter Breeze                    #
   # Copyright (C) 2010 Noah Petherbridge                   #
   ##########################################################
*/

var slides = new Array();
slides[0] = "images/los-angeles.jpg";
slides[1] = "images/hollywood.jpg";
slides[2] = "images/santa-monica.jpg";
slides[3] = "images/beverly-hills.jpg";
slides[4] = "images/orange-county.jpg";
slides[5] = "images/santa-barbara.jpg";

var ssSpeed   = 5000; // time to wait
var ssStep    = 50;   // step speed for animation
var ssOpacity = 100;  // opacity
var ssIndex   = 0;    // current slide
var ssLoaded  = {}; // slides loaded
var ssLoadedCount = 0;
var ssReachedZero = 0;

// Preload all the images first.
ssPreload();

// Preload all the images by adding them to 'slideshow-preload' and counting
// them as they load.
function ssPreload() {
	for (var i = 0; i < slides.length; i++) {
		document.getElementById("slideshow-preload").innerHTML +=
			"<img src=\"" + slides[i] + "\" onload=\"ssSlideLoaded(this)\">";
	}
}

// Callback for preloaded images.
function ssSlideLoaded(img) {
	// Which slide was this?
	var pic = img.src;
	if (ssLoaded[pic] != "yes") {
		ssLoaded[pic] = "yes";
		ssLoadedCount++;
	}

	// Start the countdown?
	if (ssLoadedCount == slides.length) {
		setTimeout("ssFade()", ssSpeed);
	}
}

// Image fader.
function ssFade() {
	// Begin fading it out.
	ssOpacity = parseInt(ssOpacity - 20);

	// Set the opacity.
	ssSetOpacity();

	// Zero?
	if (ssOpacity <= 0 && ssReachedZero == 0) {
		ssReachedZero = 1;
		ssOpacity = 0;
		// Set the next image.
		ssIndex = parseInt(ssIndex + 1);
		if (ssIndex >= slides.length) {
			ssIndex = 0;
		}
		document.getElementById("slideshow").style.backgroundImage = "url(" + slides[ssIndex] + ")";
		setTimeout("ssShow()", ssStep);
	}
	else if (ssOpacity <= 0) {
		ssOpacity = 0;
		setTimeout("ssShow()", ssStep);
	}
	else {
		setTimeout("ssFade()", ssStep);
	}
}
function ssShow() {
	// Begin fading it back out.
	ssOpacity = parseInt(ssOpacity + 20);
	ssSetOpacity();

	// 100?
	if (ssOpacity >= 100) {
		ssOpacity = 100;
		ssReachedZero = 0;
		setTimeout("ssFade()", ssSpeed);
	}
	else {
		setTimeout("ssShow()", ssStep);
	}
}

function ssSetOpacity() {
	if (typeof(document.getElementById("slideshow").style.MozOpacity) == "string") {
		// Mozilla
		document.getElementById("slideshow").style.MozOpacity = ssOpacity / 100;
		return;
	}
	else if (typeof(document.getElementById("slideshow").style.opacity) == "string") {
		// Safari & others
		document.getElementById("slideshow").style.opacity = ssOpacity / 100;
		return;
	}
	else if (document.getElementById("slideshow").filters) {
		// MSIE
		try {
		document.getElementById("slideshow").style.filter = "Alpha(opacity=" + ssOpacity + ")";
		} catch(e) {}
		return;
	}
}

