window.addEvent('domready',function(){
	initGallery();
});

function initGallery(){
	$$('div.featured-items').each(function(obj){
		new slideshow(obj);
	});
}

var slideshow = new Class({
	Implements : [Options, Events],
	options:{
		slides:'ul.slide > li',
		nextBtn:'a.btn-next',
		prevBtn:'a.btn-prev',
		pagingHolder:'div.slide-prev-holder',
		pagingTag:'li',
		createPaging:false,
		autoPlay:false,
		dynamicLoad:false,
		imgAttr:'alt',
		effect:'slideX',//fade, slideX, slideY,
		startSlide:false,
		switchTime:2000,
		animSpeed:700
	},
	initialize:function(element, options) {
		this.mainHolder = $(element);
		this.setOptions(options);
		this.slides = this.mainHolder.getElements(this.options.slides);		
		if (this.options.nextBtn) this.nextBtn = this.mainHolder.getElement(this.options.nextBtn);
		if (this.options.prevBtn) this.prevBtn = this.mainHolder.getElement(this.options.prevBtn);
		this.previous = -1;
		this.loadingFrame = 1;
		this.busy = false;
		this.direction = 1;
		this.timer;
		this.pagingArray = new Array;
		this.loadArray = new Array;
		this.preloader = new Array;
		this.slidesParent = this.slides[0].getParent();
		this.autoPlay = this.options.autoPlay;
		this.block = $$('div.below-gallery')[0];
		
		;(function(){
			if (this.options.startSlide) this.current = this.options.startSlide
			else {
				var active = -1;
				for(var i = 0; i< this.slides.length-1; i++) {
					if (this.slides[i].hasClass('active')) {
						active = i;
						break;						
					}
				}
				if (active != -1) this.current = active;
				else this.current = 0;
			}
		}).apply(this);
		
		this.initPaging();
		this.setStyles();
		this.bindEvents();
		this.showSlide();
	},
	
	initPaging:function(){
		this.pagingHolder = this.mainHolder.getElements(this.options.pagingHolder);
		
		if (this.options.createPaging) {
			this.pagingHolder.each(function(paging,i){
				paging.empty();
				var list = new Element('ul');
				var html = '';
				for (var i = 0; i < this.slides.length; i++) {
					html += '<li><a href="#">' + (i + 1) + '</a></li>';
				}
				list.innerHTML = html;
				list.inject(paging);
			}.bind(this));
		}
		
		this.pagingHolder.each(function(paging){
			this.pagingArray.push(paging.getElements(this.options.pagingTag))
		}.bind(this));
	},
	
	setStyles:function(){
		//loader
		if (this.options.dynamicLoad) {
			this.loader = new Element('div').addClass('loader');
			this.loaderDiv = new Element('div').inject(this.loader);
			this.loader.inject(this.slidesParent);
		}
		
		//slides
		this.slides.each(function(slide,i){
			if (this.options.effect == 'fade') {
				if (i != this.current) slide.setStyles({display:'none'});
				else slide.setStyles({display:'block'});
			} else if (this.options.effect == 'slideX'){
				if (i != this.current) slide.setStyles({display: 'none'});
				else slide.setStyles({display:'block',left:0});
			} else if (this.options.effect == 'slideY'){
				if (i != this.current) slide.setStyles({display:'none'});
				else slide.setStyles({display:'block',top:0});
			}
		}.bind(this));
		
		this.slideH = this.slides[this.current].getSize().y
		this.slidesParent.setStyles({height:this.slideH}); 
		this.block.setStyles({marginTop:-this.slideH-37});
	},
	
	bindEvents:function(){
		if (this.nextBtn) this.nextBtn.addEvent('click',function(){
			if (!this.busy) this.nextSlide();
			return false;
		}.bind(this));
		
		if (this.prevBtn) this.prevBtn.addEvent('click',function(){
			if (!this.busy) this.prevSlide();
			return false;
		}.bind(this));
		this.pagingArray.each(function(paging){
			paging.each(function(btn,i){
				btn.addEvent('click',function(){
					if (i != this.current && !this.busy) {
						this.previous = this.current;
						this.current = i;
						if (this.previous > i) this.direction = -1
						else this.direction = 1;
						this.showSlide();
					}
					return false;
				}.bind(this));
			}.bind(this));
		}.bind(this));
		
		if (this.options.dynamicLoad) this.loader.addEvent('click',this.abortLoading.bind(this));
	},
	
	nextSlide:function(){
		this.previous = this.current;
		if (this.current < this.slides.length-1) this.current++
		else this.current = 0;
		this.direction = 1;
		this.showSlide();
	},
	
	prevSlide:function(){
		this.previous = this.current;
		if (this.current > 0) this.current--
		else this.current = this.slides.length-1;
		this.direction = -1;
		this.showSlide();
	},
	
	showSlide:function(){
		if (this.previous == this.current) return; 
		var _current = this.current;
		this.busy = true;
		clearTimeout(this.timer);
		if (typeof this.loadArray[_current] != 'undefined' || !this.options.dynamicLoad) {
			//slide already loaded
			this.switchSlide();
		
		} else {
			//slide not loaded
			this.showLoading();
			var images = this.slides[this.current].getElements(this.options.dynamicLoad);
			if (images.length) {
				var counter = 0;
				images.each(function(img){
					var preloader = new Image;
					this.preloader.push(preloader);
					preloader.onload = function(){
						counter++;
						checkImages.apply(this);
					}.bind(this);
					preloader.onerror = function(){
						//ignore errors
						counter++;
						checkImages.apply(this);
					}.bind(this);
					preloader.src = img.getProperty(this.options.imgAttr);
				}.bind(this));
				
				function checkImages(){
					if (counter == images.length) {
						images.each(function(img){
							img.setProperty('src',img.getProperty(this.options.imgAttr));
						}.bind(this));
						successLoad.apply(this);
					}
				}
			} else successLoad.apply(this);
		}
		
		function successLoad(){
			this.loadArray[_current] = 1;
			this.hideLoading();
			this.switchSlide();
		}
	},
	
	switchSlide:function(){
		var obj = this;
		
		if (this.previous != -1) {
			var nextSlide = this.slides[this.current];
			var prevSlide = this.slides[this.previous];
			
			if (this.options.effect == 'fade') {
				nextSlide.setStyles({display:'block',opacity:0});
				var nextFx = new Fx.Morph(nextSlide,{duration:this.options.animSpeed,onComplete:function(){
					nextSlide.setStyles({opacity:'auto'});
				}});
				nextFx.start({opacity:1});
				
				prevSlide.setStyles({opacity:1});
				var prevFx = new Fx.Morph(prevSlide,{duration:this.options.animSpeed,onComplete:callback.bind(this)});
				prevFx.start({opacity:0});
			} else if (this.options.effect == 'slideX'){
				nextSlide.setStyles({display:'block'});
				this.slideW = nextSlide.getSize().x;
				this.slideH = nextSlide.getSize().y;
				nextSlide.setStyles({left:this.slideW*this.direction});
				
				var nextFx = new Fx.Morph(nextSlide,{duration:this.options.animSpeed});
				nextFx.start({left:0});
				
				var prevFx = new Fx.Morph(prevSlide,{duration:this.options.animSpeed,onComplete:callback.bind(this)});
				prevFx.start({left:-this.slideW*this.direction});
			} else if (this.options.effect == 'slideY'){
				nextSlide.setStyles({display:'block',top:this.slideH*this.direction});
				var nextFx = new Fx.Morph(nextSlide,{duration:this.options.animSpeed});
				nextFx.start({top:0});
				
				var prevFx = new Fx.Morph(prevSlide,{duration:this.options.animSpeed,onComplete:callback.bind(this)});
				prevFx.start({top:-this.slideH*this.direction});
			}
			var parentFx = new Fx.Morph(this.slidesParent,{duration: this.options.animSpeed});
			parentFx.start({height:this.slideH});
			
			var blockFx = new Fx.Morph(this.block,{duration: this.options.animSpeed});
			blockFx.start({marginTop:-this.slideH-37});
		
		} else {
			if (this.autoPlay) this.startAutoPlay();
			this.busy = false;
		}
		
		this.refreshStatus();
		
		function callback(){
			prevSlide.setStyles({display:'none'});
			if (this.autoPlay) this.startAutoPlay();
			this.busy = false;
		}
	},
	
	refreshStatus:function(){
		/*
		for (var i = 0; i < this.pagingArray.length;i++) {
			this.pagingArray[i].eq(this.previous).removeClass('active');
			this.pagingArray[i].eq(this.current).addClass('active');
		}
		this.slides.eq(this.previous).removeClass('active');
		this.slides.eq(this.current).addClass('active');
		*/
	},
	
	showLoading:function(){
		this.loader.setStyles({display:'block'});
		clearInterval(this.loadingTimer);
		this.loadingTimer = setInterval(animateLoading.bind(this), 66);
		
		function animateLoading(){
			this.loaderDiv.setStyles({'top': this.loadingFrame * -40});
			this.loadingFrame = (this.loadingFrame + 1) % 12;
		}
	},
	
	hideLoading:function(){
		this.loader.setStyles({display:'none'});
		clearInterval(this.loadingTimer);
	},
	
	abortLoading:function(){
		this.busy = false;
		this.hideLoading();
		this.current = this.previous;
		for (var i = 0; i < this.preloader.length; i++) {
			this.preloader[i].onload = null;
			this.preloader[i].onerror = null;
		}
		if (this.autoPlay) this.startAutoPlay();
	},
	
	startAutoPlay:function(){
		clearTimeout(this.timer);
		this.timer = setTimeout(this.nextSlide.bind(this),this.options.switchTime);
	}
});
