window.addEvent('domready',function(){
	initCycle();
});

// cycle gallery init
function initCycle() {
	$$('div.gallery').each(function(obj,i){
		var _gallery = new cycleCarousel(obj);
	})
}

// cycle gallery code
var cycleCarousel = new Class({
	options: {
		activeClass: 'active',
		btPrev: 'a.link-prev',
		btNext: 'a.link-next',
		pagerLinks: 'ul.tabset li',
		slidesHolder: 'div.mask',
		slider: 'ul',
		slides: 'li',
		switchTime: 5000,
		duration : 450,
		stopAfterClick:false,
		autoRotation:false
	},

	// create class
	initialize: function(element, options){
		this.setOptions(options);
		var _this = this;

		if (this.options.btNext) this.next = element.getElement(this.options.btNext);
		else this.next = false;

		if (this.options.btPrev) this.prev = element.getElement(this.options.btPrev);
		else this.prev = false;

		if (this.options.pagerLinks) this.pagerLinks = element.getElements(this.options.pagerLinks);
		else this.pagerLinks = false;

		this.animated = false;
		this.holder = element.getElement(this.options.slidesHolder);
		this.slider = this.holder.getElement(this.options.slider);
		this.slides = this.holder.getElements(this.options.slides);
		this.stepWidth = this.slides[0].getSize().x;
		this.switchTime = this.options.switchTime;
		this.activeClass = this.options.activeClass;
		this.autoRotation = this.options.autoRotation;
		this.stopAfterClick = this.options.stopAfterClick;

		// gallery init
		this.duration = this.options.duration;
		this.slideCount = this.slides.length;
		this.oldIndex = 0;
		this.currentIndex = 0;
		this.timer = false;
		this.direction = false;

		// slides init
		this.slider.setStyles({position:'relative',height:this.slider.getSize().y, width:this.stepWidth});
		this.slides.each(function(slide){
			slide.setStyles({position:'absolute',top:0,left:_this.stepWidth});
		})
		this.slides[this.currentIndex].setStyles({left:0});

		// mouseover
		_this.addEvent('mouseover', function(){
			if (_this.timer) clearInterval(_this.timer);
		}).addEvent('mouseout', function(){
			_this.autoSlide();
		});

		// gallery control
		if (this.next) {
			this.next.addEvent('click', function(){
				if (!_this.animated) {
					if(_this.stopAfterClick) {
						_this.autoRotation = false;
						clearTimeout(_this.timer);
					}
					_this.nextSlide();
				}
				return false;
			});
		}
		if (this.prev) {
			this.prev.addEvent('click', function(){
				if (!_this.animated) {
					if(_this.stopAfterClick) {
						_this.autoRotation = false;
						clearTimeout(_this.timer);
					}
					_this.prevSlide();
				}
				return false;
			});
		}
		if (this.pagerLinks) {
			this.pagerLinks.each(function(link,ind){
				link.addEvent('click', function(){
					if (!_this.animated && _this.currentIndex!=ind) {

						if(_this.stopAfterClick) {
							_this.autoRotation = false;
							clearTimeout(_this.timer);
						}
						_this.direction = (_this.currentIndex < ind);
						_this.oldIndex = _this.currentIndex;
						_this.currentIndex = ind;
						_this.switchSlide();
					}
					return false;
				});
			})
		}

		// autoslide
		if (this.options.autoRotation) {
			this.autoSlide();
		}
	},
	nextSlide: function(){
		this.oldIndex = this.currentIndex
		if(this.currentIndex < this.slideCount-1) this.currentIndex++;
		else this.currentIndex = 0;
		this.direction = true;
		this.switchSlide();
	},
	prevSlide: function(){
		this.oldIndex = this.currentIndex
		if(this.currentIndex < this.slideCount-1) this.currentIndex++;
		else this.currentIndex = 0;
		this.direction = false;
		this.switchSlide();
	},
	refreshPager: function() {
		// refresh pagination
		if (this.pagerLinks) {
			this.pagerLinks.each(function(link,ind){
				if(ind != this.currentIndex) {
					link.removeClass(this.activeClass);
				} else {
					link.addClass(this.activeClass);
				}
			}.bind(this));
		}
	},
	switchSlide: function(){
		var _d = (this.direction ? -1 : 1);
		var oldSlide = this.slides[this.oldIndex];
		var newSlide = this.slides[this.currentIndex];
		var fxOldSlide = new Fx.Morph(oldSlide, {duration: this.duration});
		var fxNewSlide = new Fx.Morph(newSlide, {duration: this.duration});

		newSlide.setStyles({left:-this.stepWidth*_d})
		fxOldSlide.start({'left':this.stepWidth*_d});
		fxNewSlide.start({'left':0});
		this.autoSlide();
		this.refreshPager();
	},
	autoSlide : function(){
		if(this.autoRotation) {
			var _this = this;
			if(this.timer) clearTimeout(this.timer);
			this.timer = setInterval(function(){_this.nextSlide()}, this.options.switchTime);
		}
	},

	// add options and events
	Implements : [Options, Events]
});
