/**
 * @author gareth bond (gazbond.co.uk)
 * @copyright (c) Gizmo UK Ltd, 2009
 *
 * adds object 'slideShow' to page.
 * add images via the array slideShow.images[] or slideShow.addImage(img)
 * 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 
 */
var slideShow = {
	interval : 4000,
	images : [],
	_mainSelector  : '#slide-show',
	_imageSelector : 'img.slide-show-image',
	_thumbSelector : '.slide-show-thumb',
	_thumbPostFix : '_tn',
	_largePostFix : '_lg',
	_currentImage : 0,
	_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(this.running) {
				if(self.firstRun) {
					self.firstRun = false;
				}
				else func();
				setTimeout(function() {
					self.run();				
				}, interval);
			}
		}
		self.stop = function() {
			this.running = false;
		}
	},
	addImage : function(img) {
		var self = this;
		self.images.push(img);
	},
	play : function() {
		var self = this;
		if(self._timer == null) {
			var imageSel =  $(self._mainSelector + ' ' +  self._imageSelector);
			self._timer = new self._Thread(self.interval, function() {
				self._currentImage++;
				if(self._currentImage > self.images.length - 1) {
					self._currentImage = 0;
				}
				imageSel.attr('src', self.images[self._currentImage]);
			});
			self._timer.run();
		}
	},
	stop : function() {
		var self = this;
		if(self._timer != null) {
			self._timer.stop();
			self._timer = null;
		}
	},
	next : function() {
		var self = this;
		var imageSel =  $(self._mainSelector + ' ' +  self._imageSelector);
		var resumePlay = false;
		if(self._timer != null) {
			self.stop();
			resumePlay = true;
		}
		self._currentImage++;
		if(self._currentImage > self.images.length - 1) {
			self._currentImage = 0;
		}
		imageSel.attr('src', self.images[self._currentImage]);
		if(resumePlay) self.play();
	},
	prev : function() {
		var self = this;
		var imageSel =  $(self._mainSelector + ' ' +  self._imageSelector);
		var resumePlay = false;
		if(self._timer != null) {
			self.stop();
			resumePlay = true;
		}
		self._currentImage--;
		if(self._currentImage < 0) {
			self._currentImage = self.images.length - 1;
		}
		imageSel.attr('src', self.images[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) {
					$('img', thumbSel).attr('src', self._convertThumb(self.images[i]));
				}
				else {
					var clone = thumbSel.clone();
					$('img', clone).attr('src', self._convertThumb(self.images[i]));
					thumbPar.append(clone);
				}
			}
			var imageSel =  $(self._mainSelector + ' ' +  self._imageSelector);
			imageSel.attr('src', self.images[0]);
			imageSel.click(function() {
				window.open(self._convertLarge($(this).attr('src')));				
			});
			$('img', thumbPar).click(function(){
				var src = $(this).attr('src');
				var image = self._removeThumb(src);
				if(self._timer != null) {
					self.stop();
					imageSel.attr('src', image);
					self._currentImage = $.inArray(image, self.images);
					self.play();
				}
				else {
					imageSel.attr('src', image);
					self._currentImage = $.inArray(image, self.images);
				}
			});
		});
	}
}