if (typeof SIDesign === "undefined") {
	SIDesign = {};
}

SIDesign.SlideShow = function(config) {
	var that = this;
	
	function gotoNextSlide() {
		that.nextSlide.call(that);
	}
	
	this.path = "";
	this.prefix = "img";
	this.startIndex = 1;
	this.endIndex = 10;
	this.currentIndex = 1;
	this.extension = ".jpg";
	this.imgElementID = "slideImg";
	this.captionElementID = "slideCaption";

	this.auto = true;
	this.intervalID = 0;
	this.delay = 5;

	if (config && ("object" === typeof config)) {
		for (var field in config) {
			if (config.hasOwnProperty(field)) {
				this[field] = config[field];
			}
		}
	}

	this.setupTimer = function() {
		this.intervalID = setInterval(gotoNextSlide, this.delay * 1000);
	};
	
	if ("string" === typeof this.imgElementID) {
		this.imgElement = document.getElementById(this.imgElementID);
	}
	
	if ("string" === typeof this.captionElementID) {
		this.captionElement = document.getElementById(this.captionElementID);
	}
	
	if (this.auto) {
		this.setupTimer();
	}
};

SIDesign.SlideShow.prototype.updateSlideImg = function() {
	if (this.imgElement) {
		this.imgElement.src = this.path + this.prefix + this.currentIndex + this.extension;		
	}
};

SIDesign.SlideShow.prototype.updateCaption = function() {
	var key;
	
	if (this.captionElement && this.captionMap) {
		key = this.prefix + this.currentIndex;
		if (typeof this.captionMap[key] === "string") {
			this.captionElement.innerHTML = this.captionMap[key];
		}
		else {
			this.captionElement.innerHTML = "";
		}
	}
};

SIDesign.SlideShow.prototype.nextSlide = function() {
	if (this.imgElement) {
		this.currentIndex++;
		if (this.currentIndex > this.endIndex) {
			this.currentIndex = this.startIndex;
		}
		this.updateSlideImg();
		this.updateCaption();
	}
};

SIDesign.SlideShow.prototype.previousSlide = function() {
	if (this.imgElement) {
		this.currentIndex--;
		if (this.currentIndex < this.startIndex) {
			this.currentIndex = this.endIndex;
		}
		this.updateSlideImg();
		this.updateCaption();
	}
};

SIDesign.SlideShow.prototype.stopSlideShow = function() {
	if (this.auto) {
		clearInterval(this.intervalID);
		this.auto = false;
		this.intervalID = 0;
	}
};

SIDesign.SlideShow.prototype.startSlideShow = function() {
	if (!this.auto) {
		this.auto = true;
		this.setupTimer();
	}
};
