﻿var IntervalID = 0;
var Delay = 5000;

function SlideShow() {
	this.Start = startShow;
	this.Pause = pauseShow;
	this.Resume = resumeShow;
	this.MoveNext = moveNext;
	this.SetSlide = setSlide;
	this.SkipToSlide = skipToSlide;
	this.Clean = cleanWhitespace;

	this.Delay = 5000;
	this.CurrentSlideIndex = 0;
	this.IntervalID = null;
	this.Instance = null;
	this.Banner = null;
	this.Buttons = null;
	this.Slides = 0;
}

function startShow() {
	this.CurrentSlideIndex = 0;
	
	this.Clean(this.Banner);
	this.Clean(this.Buttons);
	
	this.Slides = this.Buttons.childNodes.length;
	this.SetSlide(0);
	this.Resume();
}

function pauseShow() {
    window.clearInterval(IntervalID);
}

function resumeShow() {
	IntervalID = window.setInterval("ss.MoveNext()", Delay)
}

function setSlide(index) {

	this.Banner.childNodes[index].style.display = "block";
	this.Buttons.childNodes[index].className = "Selected";

	if (index == 0) this.Buttons.childNodes[this.Slides - 1].className = "";
	if (index > 0) this.Buttons.childNodes[index - 1].className = "";
}

function skipToSlide(newIndex) {
	this.Banner.childNodes[this.CurrentSlideIndex].style.display = "none";
	this.Buttons.childNodes[this.CurrentSlideIndex].className = "";
	this.SetSlide(newIndex);
	this.CurrentSlideIndex = newIndex;
	window.clearInterval(IntervalID);
	IntervalID = window.setInterval("ss.MoveNext()", Delay)
}

function moveNext() {
	var o = this.Banner.childNodes[this.CurrentSlideIndex];
	if (o!=null) o.style.display = "none";
	if (this.CurrentSlideIndex < this.Slides - 1) this.CurrentSlideIndex++;
	else this.CurrentSlideIndex = 0;
	this.SetSlide(this.CurrentSlideIndex);
}

var notWhitespace = /\S/;
function cleanWhitespace(node) {
	for (var x = 0; x < node.childNodes.length; x++) {
		var childNode = node.childNodes[x]
		if ((childNode.nodeType == 3) && (!notWhitespace.test(childNode.nodeValue))) {
			node.removeChild(node.childNodes[x])
			x--
		}
		if (childNode.nodeType == 1) {
			cleanWhitespace(childNode)
		}
	}
}
