Slideshow = {

	/**
	 * Image prototype
	 */
	ImagePrototype: {

		// Brighten this image
		imageBrighten: function() {
			if(this.imagePreload()) {
				Slideshow.images.each(function(el) {
					el.imageDim();
				});
				new Effect.Appear(this, { duration: 0.8 });
				//this.show();
			}
		},
		
		// Dim this image
		imageDim: function() {
			this.hide();
		},
		
		// Preload this image
		imagePreload: function() {
			var self = this;
			var img = this.down('img');
			var src = Slideshow.thumbs[this.imageNum].down('img').src.replace(/\/thumbs\//, '/images/');
			if(img.src != src) {
				var preloader = new Image();
				preloader.onload = function() {
					img.src = src;
					if(Slideshow.current == self.imageNum) self.imageBrighten();
					preloader.onload = function(){};
				}
				preloader.src = src;
			}
			else return true;
			
			return false;
		},
		
		// Number corresponding to position in slideshow
		imageNum: null
	},

	init: function() {
		if($('banner-thumb')==null) return false;
	
		// Properties
		Slideshow.current = 0;
		Slideshow.images = $$('#banner-img li');
		Slideshow.thumbs = $$('#banner-thumb li');

		// Initializers
		Slideshow.initThumbs();
		Slideshow.initImages();
		Slideshow.initCycle();
	},

	/**
	 * Start cycling through images
	 */
	initCycle: function() {
		Slideshow.slideshow = setInterval(function() {
			var num = Slideshow.current + 1;
			if(num == Slideshow.thumbs.length) num = 0;

			Slideshow.thumbs[num].thumbActivate();
			if(num+1 != Slideshow.thumbs.length) Slideshow.images[(num+1)].imagePreload();
		}, 4000);
	},

	initImages: function() {
		var i = 0;
		Slideshow.images.each(function(el) {
			var num = i;

			Object.extend(el, Slideshow.ImagePrototype);
			el.imageNum = num;
			if(num!=0) el.imageDim();

			i++;
			el.down('img').show();
		});
	},

	/**
	 * Activate all thumbs
	 */
	initThumbs: function() {
		var i = 0;
		Slideshow.thumbs.each(function(el) {
			var num = i;

			// Estabish thumb as ThumbPrototype
			Object.extend(el, Slideshow.ThumbPrototype);
			el.thumbNum = num;
			el.thumbDim();
		
			// Link handlers
			var link = el.down('a');
			link.observe('click', function(e) {
				clearInterval(Slideshow.slideshow);
				el.thumbActivate();
				Event.stop(e);
			});
			link.observe('mouseout', function(e) {
				el.thumbDim();
			});
			link.observe('mouseover', function(e) {
				el.thumbBright();
			});

			i++;
		});
	},

	/**
	 * Thumbnail prototype
	 */
	ThumbPrototype: {
		// Activate thumbnail
		thumbActivate: function() {
			Slideshow.thumbs[Slideshow.current].thumbDim(true);
			this.thumbBright();
			Slideshow.current = this.thumbNum;
			Slideshow.images[this.thumbNum].imageBrighten();
		},
		
		// Brighten thumbnail
		thumbBright: function() {
			this.thumbCancel();
			this.thumbEffect = new Effect.Opacity(this.down('a'), {duration:0.3, to:1});
		},

		// Cancel pending thumbnail effect
		thumbCancel: function() {
			if('undefined' != typeof this.thumbEffect) this.thumbEffect.cancel();
		},

		// Dim thumbnail
		thumbDim: function() {
			if(this.thumbNum == Slideshow.current && !arguments[0]) return false;
			this.thumbCancel();
			this.thumbEffect = new Effect.Opacity(this.down('a'), {duration:0.3, to:0.6});
		},
		
		// Number corresponding to position in slideshow
		thumbNum: null
	}

}; google.setOnLoadCallback(Slideshow.init);
