/**
 * @author gareth bond (gazbond.co.uk)
 * @copyright (c) Gizmo UK Ltd, 2009
 *
 * adds object 'slideShow' to page.
 * add images via  slideShow.addImage(img, capt)
 * you must call slideShow.init() to initialize slideShow
 * you can then call slideShow.play(), slideShow.stop(), slideShow.prev() and slideShow.next() to control the slide show
 * slide show speed (ms) is controlled via slideShow.interval 
 * state changes can update the dom via slideShow.stateChangeFunc(state) where state is either stop or play
 */
var slideShow = {
	interval : 4000,
	stateChangeFunc : null,
	_images : [],
	_captions : [],
	_mainSelector  : '#slide-show',
	_imageSelector : 'img.slide-show-image',
	_thumbSelector : '.slide-show-thumb',
	_captionSelector : '.slide-show-caption',
	_thumbPostFix : '_tn',
	_largePostFix : '_lg',
	_currentImage : 0,
	_imageSel : null,
	_captionSel : null,
	_getImageSel : function() {
		var self = this;
		if(self._imageSel == null) {
			self._imageSel = $(self._mainSelector + ' ' +  self._imageSelector);
		}
		return self._imageSel;
	},
	_getCaptionSel : function() {
		var self = this;
		if(self._captionSel == null) {
			self._captionSel = $(self._mainSelector + ' ' +  self._captionSelector);
		}
		return self._captionSel;
	},
	_updateImage : function(sel, src, capt) {
		sel.attr('src', src);
		sel.attr('alt', capt);
		sel.attr('title', capt);
	},
	_convertThumb : function(path) {
		var self = this;
		var split = path.split('.');
		return split[0] + self._thumbPostFix + '.' + split[1];
	},
	_removeThumb : function(path) {
		var self = this;
		var split = path.split(self._thumbPostFix);
		return split[0] + split[1];
	},
	_convertLarge : function(path) {
		var self = this;
		var split = path.split('.');
		return split[0] + self._largePostFix + '.' + split[1];
	},
	_timer : null,
	_Thread : function(interval, func) {
		var self = this;
		self.running = true;
		self.firstRun = true;
		self.run = function() {
			if(self.running) {
				if(self.firstRun) {
					self.firstRun = false;
				}
				else func();
				setTimeout(function() {
					self.run();				
				}, interval);
			}
		}
		self.stop = function() {
			self.running = false;
		}
	},
	addImage : function(img, capt) {
		var self = this;
		self._images.push(img);
		self._captions.push(capt);
	},
	play : function() {
		var self = this;
		if(self.stateChangeFunc != null) self.stateChangeFunc('play');
		if(self._timer == null) {
			var imageSel =  self._getImageSel();
			var captionSel =  self._getCaptionSel();
			self._timer = new self._Thread(self.interval, function() {
				self._currentImage++;
				if(self._currentImage > self._images.length - 1) {
					self._currentImage = 0;
				}
				self._updateImage(imageSel, self._images[self._currentImage], self._captions[self._currentImage]);
				captionSel.html(self._captions[self._currentImage]);
			});
			self._timer.run();
		}
	},
	stop : function() {
		var self = this;
		if(self.stateChangeFunc != null) self.stateChangeFunc('stop');
		if(self._timer != null) {
			self._timer.stop();
			self._timer = null;
		}
	},
	next : function() {
		var self = this;
		var imageSel =  self._getImageSel();
		var captionSel =  self._getCaptionSel();
		var resumePlay = false;
		if(self._timer != null) {
			self.stop();
			resumePlay = true;
		}
		self._currentImage++;
		if(self._currentImage > self._images.length - 1) {
			self._currentImage = 0;
		}
		self._updateImage(imageSel, self._images[self._currentImage], self._captions[self._currentImage]);
		captionSel.html(self._captions[self._currentImage]);
		if(resumePlay) self.play();
	},
	prev : function() {
		var self = this;
		var imageSel =  self._getImageSel();
		var captionSel =  self._getCaptionSel();
		var resumePlay = false;
		if(self._timer != null) {
			self.stop();
			resumePlay = true;
		}
		self._currentImage--;
		if(self._currentImage < 0) {
			self._currentImage = self._images.length - 1;
		}
		self._updateImage(imageSel, self._images[self._currentImage], self._captions[self._currentImage]);
		captionSel.html(self._captions[self._currentImage]);
		if(resumePlay) self.play();
	},
	init : function() {
		var self = this;
		if(self._images.length == 0) throw new Error('no images defined for slide show!');
		$(document).ready(function(){
			var thumbSel    = $(self._mainSelector + ' ' +  self._thumbSelector);
			var thumbPar    = thumbSel.parent();
			for(var i = 0; i < self._images.length; i++) {
				if(i == 0) {
					var curSel = $('img', thumbSel);
					self._updateImage(curSel, self._convertThumb(self._images[i]), self._captions[i]);
				}
				else {
					var clone = thumbSel.clone();
					var curSel = $('img', clone);
					self._updateImage(curSel, self._convertThumb(self._images[i]), self._captions[i]);
					thumbPar.append(clone);
				}
			}
			var imageSel =  self._getImageSel();
			var captionSel =  self._getCaptionSel();
			self._updateImage(imageSel, self._images[0], self._captions[0]);
			imageSel.click(function() {
				window.open(self._convertLarge($(this).attr('src')));				
			});
			captionSel.html(self._captions[0]);
			$('img', thumbPar).click(function(){
				var src = $(this).attr('src');
				var image = self._removeThumb(src);
				var index = $.inArray(image, self._images);
				if(self._timer != null) {
					self.stop();
					self._updateImage(imageSel, image, self._captions[index]);
					captionSel.html(self._captions[index]);
					self._currentImage = index;
					self.play();
				}
				else {
					self._updateImage(imageSel, image, self._captions[index]);
					captionSel.html(self._captions[index]);
					self._currentImage = index;
				}
			});
		});
	}
}