function Slideshow (wrapperDiv, options) {

	if (!wrapperDiv) {
		return;
	}
	this.options = {
		showNav: true, 
		height: '261px', 
		width: '350px', 
		speed_delay: 4000, 
		slide_speed: 1000
	}
	Object.extend(this.options, options || {});

	this.wrapper = document.getElementById( wrapperDiv );
	this.wrapper.slideshow = this;
	wrapper = this.wrapper;
	
	//public properties
	this.wrapperDiv = wrapperDiv;
	this.showNav = this.options.showNav;
	this.height = this.options.height;
	this.width = this.options.width;
	this.speed_delay = this.options.speed_delay;
	this.slide_speed = this.options.slide_speed;

	this.autoslide = true;
	this.slideOuter = "slide-wrapper";
	this.slideLoading = "slide-loading";
	this.slideClass = "slide";
	this.naviClass = "navi";
	this.activeSuffix = "-active";
	
	var self = this; // corrects privatization bug in JS 1.5

	// Effect handle
	this.isShowing = 0;
	this.slides;
	this.navis;
	this.fx1 = new Array();
	this.firstDelay = false;
	this.pauseFlag = false;
	this.timer;
	this.playButton;

	wrapper.getElementsByClassName = function ( className ) {
		var children = wrapper.getElementsByTagName('*');
		var elements = [];
		// $c is provided in prototype.lite.js
		$c(children).each(function(child){
			if (Element.hasClassName(child, className)) {
				elements.push(child);
			}
		});  
		return elements;
	}

	wrapper.style.width = this.options.width;

	var displayloading = wrapper.getElementsByClassName('slide-loading')[0];

	if (displayloading) {
		displayloading.style.display = "none";
	}
	var displayslide = wrapper.getElementsByClassName('slide-wrapper')[0];

	if (displayslide) {
		displayslide.style.display = "block";
	}
	if (readCookie("com_jw_fpss") == "true") {
		autoslide = true;
	} else if (readCookie("com_jw_fpss") == "false") {
		autoslide = false;
	}

	if (this.options.showNav) {
		var prev = wrapper.getElementsByClassName('prevlink');
		if (prev.length > 0) {
			this.prevButton = prev[0];
			this.prevButton.slideshow = this;
			this.prevButton.onclick = this.previous;
		}
	
		var next = wrapper.getElementsByClassName('nextlink');
		if (next.length > 0) {
			this.nextButton = next[0];
			this.nextButton.slideshow = this;
			this.nextButton.onclick = this.next;
		}
	
		var play = wrapper.getElementsByClassName('playButton');
		if (play.length > 0) {
			this.playButton = play[0];
			this.playButton.slideshow = this;
			this.playButton.onclick = this.playpause;
		}
	}

	this.slides = wrapper.getElementsByClassName(this.slideClass);
	this.navis = wrapper.getElementsByClassName(this.naviClass);
	if (this.slides.length == 0) { // || this.navis.length == 0) {
		return;
	}
	
	for (i = 0; i < this.slides.length; ++i) {
		this.fx1[i] = new fx.Combo(this.slides[i], { opacity: true, width: false, duration: this.slide_speed, height: false, toggle: false });
		if (this.navis.length > i) {
			this.navis[i].slideshow = this;
			this.navis[i].onclick = function(evt) {
				var	target = (typeof evt != 'undefined') ? evt.currentTarget : this,
					theshow = target.slideshow,
					current = null;
				for (j = 0; j < theshow.navis.length; ++j) {
					if (this == theshow.navis[j]) {
						current = j;
					}
				}
				if (current != theshow.isShowing) {
					theshow.fx1[theshow.isShowing].clearTimer();
					if (theshow.fx1[theshow.isShowing].el.offsetHeight) {
						theshow.fx1[theshow.isShowing].hide();
					}
					theshow.fx1[current].toggle();
					theshow.navis[theshow.isShowing].className = theshow.naviClass;
					theshow.navis[current].className = theshow.naviClass + theshow.activeSuffix;
					theshow.isShowing = current;
					theshow.clearSlide();
				}
				return false;
			}
		}
		if (i != 0) {
			this.fx1[i].hide();
		} else if (this.navis.length > i) {
			this.navis[i].className = this.naviClass + this.activeSuffix;
		}
	}
	this.autoSlide();
}

Slideshow.prototype.prevButtonClicked = function () {
	this.showPrev();
	this.clearSlide();
	return false;
}

Slideshow.prototype.nextButtonClicked = function () {
	this.showNext();
	this.clearSlide();
	return false;
}

Slideshow.prototype.playButtonClicked = function () {
	if (this.autoslide) {
		this.showPauseButton();
	} else {
		this.showPlayButton();
	}
}

Slideshow.prototype.showPauseButton = function () {
	createCookie("com_jw_fpss", "true");
	this.playButton.innerHTML = "Pause"; // change "Pause" label here
	this.playButton.title = "Pause"; // change "Pause" label here
	this.pauseFlag = false;
	this.autoSlide();
}

Slideshow.prototype.showPlayButton = function () {
	createCookie("com_jw_fpss", "false");
	this.playButton.innerHTML = "Play"; // change "Play" label here
	this.playButton.title = "Play"; // change "Play" label here
	this.pauseFlag = true;
	clearTimeout(this.timer);
	this.firstDelay = false;
}

Slideshow.prototype.autoSlide = function () {
	if (!this.pauseFlag) {
		this.timer = setTimeout('document.getElementById("' + this.wrapperDiv + '").slideshow.autoSlide()', this.speed_delay);
		if (!this.firstDelay) {
			this.firstDelay = true;
		} else {
			this.wrapper.slideshow.showNext();
		}
	}
}

Slideshow.prototype.clearSlide = function () {
	if (!this.pauseFlag) {
		clearTimeout(this.timer);
		this.firstDelay = false;
		this.autoSlide();
	}
}

Slideshow.prototype.playButtonClicked = function () {
	if (this.pauseFlag) {
		this.showPauseButton();
	} else {
		this.showPlayButton();
	}
}

Slideshow.prototype.showNext = function () {
	if (this.slides.length <= 1) {
		return;
	}
	this.fx1[this.isShowing].clearTimer();
	if (this.fx1[this.isShowing].el.offsetHeight) {
		this.fx1[this.isShowing].hide();
	}
	if (this.navis.length > this.isShowing) {
		this.navis[this.isShowing].className = this.naviClass;
	}
	if (this.isShowing == this.slides.length - 1) {
		this.fx1[0].toggle();
		this.isShowing = 0;
	} else {
		this.fx1[++this.isShowing].toggle();
	}
	if (this.navis.length > this.isShowing) {
		this.navis[this.isShowing].className = this.naviClass + this.activeSuffix;
	}
}

Slideshow.prototype.showPrev = function () {
	if (this.slides.length <= 1) {
		return;
	}
	this.fx1[this.isShowing].clearTimer();
	if (this.fx1[this.isShowing].el.offsetHeight) {
		this.fx1[this.isShowing].hide();
	}
	if (this.navis.length > this.isShowing) {
		this.navis[this.isShowing].className = this.naviClass;
	}
	if (this.isShowing == 0) {
		this.fx1[this.slides.length - 1].toggle();
		this.isShowing = this.slides.length - 1;
	} else {
		this.fx1[--this.isShowing].toggle();
	}
	if (this.navis.length > this.isShowing) {
		this.navis[this.isShowing].className = this.naviClass + this.activeSuffix;
	}
}

Slideshow.prototype.previous = function (evt) {
	var	target = (typeof evt != 'undefined') ? evt.currentTarget : this,
		theshow = target.slideshow;
	theshow.prevButtonClicked();
	return false;
}

Slideshow.prototype.next = function (evt) {
	var	target = (typeof evt != 'undefined') ? evt.currentTarget : this,
		theshow = target.slideshow;
	theshow.nextButtonClicked();
	return false;
}

Slideshow.prototype.playpause = function (evt) {
	var	target = (typeof evt != 'undefined') ? evt.currentTarget : this,
		theshow = target.slideshow;
	theshow.playButtonClicked();
	return false;
}

// Cookie handlers
function createCookie (name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie (name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') {
			c = c.substring(1,c.length);
		}
		if (c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length,c.length);
		}
	}
	return null;
}

