// JavaScript Document
// JavaScript slideshow

function SlideShow(options) {
	// Current slide
	this.currentSlideNo = 0;	// First Slide
	this.nextSlideNo = 1;	// index of slide to preload
	this.slideShowTimer = false;
	this.fadeEvery = options.fadeEvery || 4;	// Fade images every n seconds
	this.nextImg = false;		// will hold next image preloaded
	this.preloadComplete = false;
	this.slideUp = 0; this.slideDown = 1 - this.slideUp;
	this.containerId = options.containerId || 'slideshow';
	this.slides = options.slides || [];
	this.pause = false;
	if ($('#slideimg1').length == 0) {
		// create a second image container
		$('#slideimg0').clone().attr({id: 'slideimg1', display: 'none'}).css('opacity',0).appendTo('#'+this.containerId);
		$('#'+this.containerId).click({slide: this },function (e) {
			// if there's a link on click follow it
			var s = e.data.slide;
			if (s.slides[s.currentSlideNo].link.length != 0 && s.slides[s.currentSlideNo].link != '#') {
				window.location = s.slides[s.currentSlideNo].link;
			}
		});
		this.nextImg = new Image($('#slideimg0').width(),$('#slideimg0').height()); // create preload image holder
		if (this.currentSlideNo+1 < this.slides.length) {
			// if more than one slide prefetch the next one
			this.nextSlideNo = this.currentSlideNo+1;
			this.nextImg.src = this.slides[this.nextSlideNo++].src;	// preload next image
		}
	}
	for(var i=0;i<this.slides.length;i++) {
		$('#slideshow_controls_wrap').append('<span title="'+i+'"></span>');
	}
	$('#slideshow_controls_wrap').mouseleave({ slideshow: this }, function (e) {
		e.data.slideshow.start();
	});
	$('#slideshow_controls_wrap span').click( { slideshow: this }, function (e) {
		// when they click a link, stop slide show and show image
		e.data.slideshow.showSlide($(this).attr("title"));		
	});
	$('#slideshow_controls_wrap span').first().addClass('on');
	$('#'+this.containerId).mouseenter({ slideshow: this },function (e) {
		// stop slide show while mouse is over it
		e.data.slideshow.pause = true;		
	});
	$('#'+this.containerId).mouseleave({ slideshow: this }, function (e) {
		// continue slide show now mouse isnt over it
		e.data.slideshow.pause = false;
	});
}
SlideShow.prototype.start = function () {
	if (!this.slideShowTimer && this.slides && this.slides.length > 1) {
		var self = this;
		this.slideShowTimer = setInterval(function () { self.transition(); },this.fadeEvery*1000);
	} 
}
SlideShow.prototype.stop = function () {
	if (this.slideShowTimer != false) {
		clearInterval(this.slideShowTimer);
		this.slideShowTimer = false;
	}
}
SlideShow.prototype.addSlide = function (slide) {
	this.slide.push(slide);
}
SlideShow.prototype.showSlide = function (i) {
	this.stop();
	this.currentSlideNo = i; // set current slide
	this.nextSlideNo = (i == this.slides.length) ? 0 : i+1; // set next slide
	this.slideUp = 0; this.slideDown = 1 - this.slideUp; // reset images up/down
	$('#slideimg'+this.slideDown).css({opacity: 0});
	$('#slideimg'+this.slideUp).css({opacity: 1});
	$('#slideimg'+this.slideUp).attr('src',this.slides[this.currentSlideNo].src);
	if (this.slides[this.currentSlideNo].link.length != 0 && this.slides[this.currentSlideNo].link != '#') {
		$('#'+this.containerId).addClass('clickable');
	} else {
		$('#'+this.containerId).removeClass('clickable');
	}
	$('#slideshow_description').html(this.slides[this.currentSlideNo].caption);
	$('#slideshow_controls_wrap span').removeClass('on');
	$('#slideshow_controls_wrap span').eq(this.currentSlideNo).addClass('on');	
}
SlideShow.prototype.transition = function () {
	if (!this.slides.length || this.slides.length < 2 || this.pause) return false; // only run if 2 or more images, and slide show is not paused
	this.currentSlideNo = (this.currentSlideNo+1 >= this.slides.length) ? 0 : this.currentSlideNo+1;
	this.nextSlideNo = (this.currentSlideNo+1 >= this.slides.length) ? 0 : this.currentSlideNo+1;
	this.nextImg.src = this.slides[this.nextSlideNo].src;	// preload next image
	
	// Change slide
	this.slideUp = 1-this.slideUp;
	this.slideDown = 1-this.slideUp;
	$('#slideimg'+this.slideDown).animate({opacity: 0},1000);
	$('#slideimg'+this.slideUp).attr('src',this.slides[this.currentSlideNo].src).animate({opacity: 1},1000);		
	if (this.slides[this.currentSlideNo].link.length != 0 && this.slides[this.currentSlideNo].link != '#') {
		$('#'+this.containerId).addClass('clickable');
	} else {
		$('#'+this.containerId).removeClass('clickable');
	}
	$('#slideshow_description').html(this.slides[this.currentSlideNo].caption);
	$('#slideshow_controls_wrap span').removeClass('on');
	$('#slideshow_controls_wrap span').eq(this.currentSlideNo).addClass('on');
}

