﻿
function slideshow(slideshowID, slideshowInstanceID, startNow) {
	this.slideshowID = slideshowID;
	this.slideshowInstanceID = slideshowInstanceID;

	this.elements = {};
	this.elements.outer = document.getElementById(slideshowID);
	this.elements.slides = document.getElementById("div_SlideshowImages_" + slideshowID);
	this.elements.nav = document.getElementById("div_SlideshowNav_" + slideshowID);
	this.collections = {};
	this.collections.slides = {};
	this.collections.navButtons = this.elements.nav.getElementsByTagName("A");

	this.slideDelay = 8000;
	this.currentSlide = 1;
	this.previousSlide = -1;
	this.totalSlides = 0;
	this.slideHeight = this.elements.outer.offsetHeight;

	this.currentTimeout = 0;

	this.init = function () {
		this.collections.slides = this.getChildNodes(this.elements.slides, "A");
		this.totalSlides = this.collections.slides.length;
		if (this.totalSlides > 1) {
			this.buildNav();
			if (startNow) this.setTimeout();
			for (var i = 1; i < this.totalSlides; i++) {
				var anim = new YAHOO.util.Anim(this.collections.slides[i], { opacity: { to: 0 }, zIndex: { to: -1 } }, 0);
				anim.animate();
			}
		}
		else {
			this.elements.nav.style.display = "none";
		}
	}
	this.buildNav = function () {
		for (var i = 0; i < this.totalSlides; i++) {
			var newNavButton = document.createElement("A");
			newNavButton.setAttribute("href", "#");
			newNavButton.setAttribute("name", this.slideshowInstanceID);
			//newNavButton.setAttribute("onclick", this.slideshowInstanceID + ".gotoSlide(this.innerHTML, true); return false;");
			newNavButton.onclick = function () { eval(this.name + ".gotoSlide(" + this.innerHTML + ",true)"); return false; };
			newNavButton.setAttribute("title", "Jump to slide " + (i + 1));
			newNavButton.innerHTML = i + 1;
			if (i == 0) newNavButton.className = "a_Current";
			this.elements.nav.appendChild(newNavButton);
		}
	}
	this.nextSlide = function () {
		this.gotoSlide(this.currentSlide == this.totalSlides ? 1 : parseInt(this.currentSlide) + 1);
	}
	this.calculateOffset = function () {
		var newOffset = ((this.currentSlide - 1) * this.slideHeight) * -1;
		return newOffset;
	}
	this.selectCurrentSlide = function () {
		if (this.previousSlide > 0) this.collections.navButtons[this.previousSlide - 1].className = "";
		this.collections.navButtons[this.currentSlide - 1].className += "a_Current";
	}
	this.gotoSlide = function (slideNum, stop) {
		this.previousSlide = this.currentSlide;
		this.currentSlide = slideNum;
		var anim = new YAHOO.util.Anim(this.collections.slides[this.previousSlide - 1], { opacity: { to: 0 }, zIndex: { to: -1} }, 1);
		var anim2 = new YAHOO.util.Anim(this.collections.slides[this.currentSlide - 1], { opacity: { to: 1 }, zIndex: { to: 1} }, 1);
		var thisSlideshow = this;
		if (!stop) {
			anim.onComplete.subscribe(function () {
				thisSlideshow.setTimeout();
			});
		}
		else {
			window.clearTimeout(this.currentTimeout);
		}
		if (this.previousSlide > 0) anim.animate();
		anim2.animate();
		this.selectCurrentSlide();
	}
	this.setTimeout = function () {
		window.clearTimeout(this.currentTimeout);
		this.currentTimeout = window.setTimeout(this.slideshowInstanceID + ".nextSlide()", this.slideDelay);
	}
	this.getChildNodes = function (parentNode, tagName) {
		if (parentNode.children) return parentNode.children;
		var childNodes = parentNode.childNodes;
		var childNodesOut = [];
		for (var i = 0; i < childNodes.length; i++) {
			if (childNodes[i].tagName) {
				if (!tagName || childNodes[i].tagName == tagName) {
					childNodesOut.push(childNodes[i]);
				}
			}
		}
		return childNodesOut;
	}

	this.init();
}
