/*
---

name: Gallery

description: A Gallery

requires: [MooTools, Class, DomReady, Element, Element.Event, Element.Dimensions, Fx, Fx.Tween, Fx.Transitions, CoreTemplates, Abacus.Element]

provides: [Abacus.Gallery]

...
*/

(function ($, window, document){

	Abacus.Gallery = {};
	if (!Abacus.SPIN2) Abacus.SPIN2 = {};

	Abacus.Gallery.Rotator = new Class({

		Implements: [Options, Events],
		options: {
			items: '.thumbs LI',
			delay: 5000
		},

		initialize: function(element, options){
			this.element = $(element);
			this.setOptions(options);
			this.current = 0;
			this.start();
			this.items = this.element.getElements(this.options.items);

			this.element.store('abacus:rotator', this);
		},

		start: function(){
			if (!$(this).hasClass('paused')){
				this.periodical = (function(){
					if (this.current < (this.items.length - 1)) this.current++;
					else this.current = 0;
					this.show(this.current);
				}).periodical(this.options.delay, this);
			}
		},

		stop: function(){
			clearInterval(this.periodical);
		},

		restart: function(){
			this.stop();
			this.start();
		},

		show: function(index){
			this.items.removeClass('selected');
			this.items[index].addClass('selected');
			this.fireEvent('itemSelect', [this.items[index]], this);
		},

		next: function(){
			this.stop();
			if (this.current < (this.items.length - 1)) this.current++;
			else this.current = 0;
			this.show(this.current);
			return this;
		},

		previous: function(){
			this.stop();
			if (this.current > 0) this.current--;
			else this.current = this.items.length - 1;
			this.show(this.current);
			return this;
		},

		toElement: function(){
			return this.element;
		}

	});

	AbacusHomeCarousel = new Class({
		Extends: Abacus.Gallery.Rotator,
		Implements: [Options, Events],
			options: {
				items: '.thumbnails li',
				delay: 8000,
				display: '.slideItem',
				caption: '.caption'
			},

		initialize: function(element, options){
			this.parent(element, options);
			var self = this,
				slides = self.element.getElement(self.options.display).getParent('ul.slides');

			self.element.getElements(self.options.items + ':not(.first)').each(function(thumbnails, i){
				var	details = thumbnails.parseClass(),
					newSlideItem = new Element('li').addClass('slideItem').inject(slides),
					currentCaption = thumbnails.getElement(self.options.caption);
				if (details.fullsize) var image = new Element('img').set('src', details.fullsize).inject(newSlideItem);
				if (details.url) newSlideItem.addClass('link');
				if (currentCaption) currentCaption.clone().inject(newSlideItem);
			});

			$(this).addEvents({
				'mouseleave': function(){
					if (!$(self).hasClass('paused')){
						self.restart.bind(self);
					}
				},
				'mouseenter': function(){
					self.stop.bind(self);
				}
			});

			this.addEvent('itemSelect', function(thumbnail){
				var details = thumbnail.parseClass(),
					current = self.current+1,
					previous = self.previousIndex+1,
					slideItems = slides.getChildren(),
					height = this.element.getSize().y,
					moveMeasure = slides.getStyle('margin-top').toInt() - ((current-previous) * height);

				var tween = new Fx.Tween(slides, {
						duration: 300,
						transition: 'linear',
						onComplete: function(){
							slides.setStyle('margin-top', moveMeasure + 'px');
							if (previous === slideItems.length && current !== slideItems.length){
								slideItems[slideItems.length-1].getAllPrevious().reverse().inject(slides);
								slides.setStyle('margin-top', (moveMeasure - height) + 'px');
							}
							if (current === slideItems.length && previous !== slideItems.length){
								slideItems[0].inject(slides);
								slides.setStyle('margin-top', (moveMeasure + height) + 'px');
							}
						}
					});

				if ((current === 1 && previous === slideItems.length)){
					tween.start('margin-top', slides.getStyle('margin-top').toInt() - height);
				} else {
					tween.start('margin-top', moveMeasure);
				}

				slideItems[self.current].addEvent('click', function(){
					if (details.url) window.location = details.url;
				});

				self.items.removeClass('selected');
				thumbnail.addClass('selected');
			});

			this.bindThumbEvents();

			$(this).getElement(this.options.display).addEvent('click', function(){
				window.location = this.getElement('a').get('href');
			});

			$(this).getElement('.controls li').addEvent('click', function(){
				var newmessage = 'play slideshow';
				if (this.hasClass('play')) newmessage = 'pause slideshow';
				this.toggleClass('pause').toggleClass('play').set('text', newmessage);

				$(self).toggleClass('paused');
				self.restart();
			});
		},

		bindThumbEvents: function(){
			var self = this;
			this.items.each(function(item, index){
				item.addEvents({
					'click': function(evt){
						evt.stop();
						self.current = index;
						self.show(index);
					},
					'mouseenter': function(){
						self.stop();
					},
					'mouseleave': function(){
						if (!$(self).hasClass('paused')){
							self.restart();
						}
					}
				});
			});
		},

		show: function(index){
			this.previousIndex = this.items.hasClass('selected').indexOf(true);
			this.items.removeClass('selected');
			this.items[index].addClass('selected');
			this.fireEvent('itemSelect', [this.items[index]], this);
		}

	});

})(document.id, window, document);
