Event.observe(window, 'load', function() {
	initCarousels();
});
function initCarousels() {
	// scroll galleries with blue dots
	$$('div.g1').each(function(obj,ind){
		new pImageGallery(obj, {
			btnPrev:'a.link-prev',
			btnNext:'a.link-next',
			slidesHolder:'div.slide-hold',
			slider:'ul',
			slides:'li',
			slideSpeed:0.5,
			switchTime:5000,
			autoSlide:true,
			step:1
		})
	});
	$$('div.g2').each(function(obj,ind){
		new pSlideShow(obj, {
			slides:'div.slide-hold ul.gal li',
			pagerLinks:'div.heading-top ul.switcher li',
			fadeSpeed:0.6,
			switchTime:4000,
			autoSlide:true
		})
	});
}
// Fade Slideshow class
var pSlideShow = Class.create({
	initialize: function(elem, opt){
		// gallery options
		opt = opt || {};
		this.options = {
			btnPrev:'a.link-prev',
			btnNext:'a.link-next',
			pagerLinks:'ul.switcher li',
			slides:'ul.slidelist li',
			activeClass:'active',
			fadeSpeed:1,
			switchTime:5000,
			autoSlide:false
		};
		Object.extend(this.options, opt);

		// gallery init
		this.timer = false;
		this.gallery = elem;
		this.activeClass = this.options.activeClass;
		this.autoSlide = this.options.autoSlide;
		this.switchTime = this.options.switchTime;
		this.fadeSpeed = this.options.fadeSpeed;
		this.slides = elem.select(this.options.slides);
		this.btnPrev = elem.select(this.options.btnPrev)[0];
		this.btnNext = elem.select(this.options.btnNext)[0];
		this.pagerLinks = elem.select(this.options.pagerLinks);

		// gallery variables
		this.slideCount = null;
		this.oldStep = 0;
		this.currentStep = 0;

		// gallery control
		this.slideCount = this.slides.length;
		if(!this.slideCount) return;
		if(this.btnPrev) this.btnPrev.observe('click', this.prevSlide.bind(this));
		if(this.btnNext) this.btnNext.observe('click', this.nextSlide.bind(this));

		this.slides.each(function(slide, ind){
			if(ind == this.currentStep) slide.setStyle({'display':'block'});
			else slide.setStyle({'display':'none'});
		}.bind(this))

		this.pagerLinks.each(function(link, ind){
			link.observe('click',function(ev){
				if(ev) ev.preventDefault();
				this.numSlide(ind);
			}.bind(this));
		}.bind(this))
		this.autoSwitch();
	},
	prevSlide: function(ev){
		if(ev && typeof ev.preventDefault === 'function') ev.preventDefault();
		this.oldStep = this.currentStep;
		if(this.currentStep > 0) this.currentStep--;
		else this.currentStep = this.slideCount-1;
		this.switchSlide();
	},
	nextSlide: function(ev){
		if(ev && typeof ev.preventDefault === 'function') ev.preventDefault();
		this.oldStep = this.currentStep;
		if(this.currentStep < this.slideCount-1) this.currentStep++;
		else this.currentStep = 0;
		this.switchSlide();
	},
	numSlide: function(num) {
		if(this.currentStep != num) {
			this.oldStep = this.currentStep;
			this.currentStep = num;
			this.switchSlide();
		}
	},
	refreshStatus: function(){
		this.pagerLinks.each(function(link, ind){
			if(ind == this.currentStep) link.addClassName(this.activeClass);
			else link.removeClassName(this.activeClass);
		}.bind(this))
	},
	switchSlide: function(){
		var _animSpeed = this.fadeSpeed;
		var _prevSlide = this.slides[this.oldStep];
		var _nextSlide = this.slides[this.currentStep];

		// fadeOut slide
		_prevSlide.setStyle({'display':'block','opacity':'1'});
		_prevSlide.removeClassName(this.activeClass);
		new Effect.Morph(_prevSlide, {
			style:{'opacity':'0'},
			afterFinish: function() {
				_prevSlide.setStyle({'display':'none'});
			},
			duration: _animSpeed
		});

		// fadeIn slide
		_nextSlide.setStyle({'display':'block','opacity':'0'});
		_nextSlide.addClassName(this.activeClass);
		new Effect.Morph(_nextSlide, {
			style:{'opacity': '1'},
			duration: _animSpeed
		});

		this.refreshStatus();
		this.autoSwitch();
	},
	autoSwitch: function(){
		if(this.timer) clearTimeout(this.timer);
		if(this.autoSlide) this.timer = setTimeout(this.nextSlide.bind(this),this.switchTime);
	}
});
// Scrolling Gallery
var pImageGallery = Class.create({
	initialize: function(elem, opt){
		// gallery options
		opt = opt || {};
		this.options = {
			btnPrev:'a.link-prev',
			btnNext:'a.link-next',
			pagerLinks: false,
			generatePagination: false,
			slidesHolder:'div.slides-holder',
			slider:'ul',
			slides:'li',
			curNum:false,
			allNum:false,
			step:false,
			slideSpeed:1.5,
			switchTime:5000,
			activeClass:'active',
			vertical:false,
			autoSlide:false
		};
		Object.extend(this.options, opt);

		// gallery init
		this.timer = false;
		this.gallery = elem;
		this.step = this.options.step;
		this.vertical = this.options.vertical;
		this.activeClass = this.options.activeClass;
		this.autoSlide = this.options.autoSlide;
		this.switchTime = this.options.switchTime;
		this.slideSpeed = this.options.slideSpeed;
		this.slidesHolder = elem.select(this.options.slidesHolder)[0];
		this.slider = this.slidesHolder.select(this.options.slider)[0];
		this.slides = this.slider.childElements();//select(this.options.slides);
		this.btnPrev = elem.select(this.options.btnPrev)[0];
		this.btnNext = elem.select(this.options.btnNext)[0];
		this.curNum = elem.select(this.options.curNum)[0];
		this.allNum = elem.select(this.options.allNum)[0];
		this.pagerLinks = null;

		// gallery variables
		this.visibleCount = null;
		this.slideCount = null;
		this.holderWidth = null;
		this.holderHeight = null;
		this.stepWidth = null;
		this.stepHeight = null;
		this.sumWidth = null;
		this.sumHeight = null;
		this.currentStep = 0;

		// gallery init
		if(this.recalcDimensions()) return;

		// pagination links
		if(this.options.generatePagination) {
			this.pagerHolder = elem.select(this.options.generatePagination)[0];
			if(this.pagerHolder) {
				var ul = new Element('ul', {'class': 'scroll-pager'});
				var links = '';
				for(var i=1; i<= this.stepCount; i++)
					links+='<li><a href="#"><span>'+i+'</span></a></li>';
				ul.update(links);
				this.pagerHolder.update('');
				this.pagerHolder.insert(ul);
				this.pagerLinks = ul.select('li');
			}
		} else {
			if(this.options.pagerLinks) {
				this.pagerLinks = elem.select(this.options.pagerLinks);
			}
		}

		// gallery event handlers
		if(this.pagerLinks) {
			this.pagerLinks.each(function(link, ind){
				link.observe('click',function(e){
					if(e) e.preventDefault();
					this.numSlide(ind);
				}.bind(this));
			}.bind(this));
		}
		if(this.btnPrev) this.btnPrev.observe('click', this.prevSlide.bind(this));
		if(this.btnNext) this.btnNext.observe('click', this.nextSlide.bind(this));
		this.autoSwitch();
		this.refreshStatus();
	},
	recalcDimensions: function() {
		if(!this.slides.length) return true;
		this.slideCount = this.slides.length;
		this.holderWidth = this.slidesHolder.getDimensions().width;
		this.holderHeight = this.slidesHolder.getDimensions().height;

		// calc step
		if(this.vertical) {
			if(this.step) this.stepHeight = this.slides[0].getDimensions().height * this.step;
			else this.stepHeight = this.holderHeight;
		} else {
			if(this.step) this.stepWidth = this.slides[0].getDimensions().width * this.step;
			else this.stepWidth = this.holderWidth;
		}

		// sum width
		var _sumWidth = 0;
		var _sumHeight = 0;
		this.slides.each(function(slide){
			_sumWidth += slide.getDimensions().width;
			_sumHeight += slide.getDimensions().height;
		});
		this.sumWidth = _sumWidth;
		this.sumHeight = _sumHeight;

		this.maxOffset = (this.vertical ? this.holderHeight - this.sumHeight : this.holderWidth - this.sumWidth);
		this.stepCount = Math.ceil(Math.abs(this.maxOffset / (this.vertical ? this.stepHeight : this.stepWidth)))+1;
	},
	refreshStatus: function() {
		if(this.curNum) this.curNum.innerHTML = this.currentStep+1;
		if(this.allNum) this.allNum.innerHTML = this.stepCount;

		if(this.pagerLinks) this.pagerLinks.each(function(item,ind){
			if(this.currentStep == ind) item.addClassName(this.activeClass);
			else item.removeClassName(this.activeClass);
		}.bind(this));
	},
	numSlide: function(num) {
		if(this.currentStep != num) {
			this.currentStep = num;
			this.switchSlide();
		}
	},
	prevSlide: function(event){
		if(event) event.preventDefault();
		if(this.currentStep > 0) this.currentStep--;
		else this.currentStep = this.stepCount-1;
		this.switchSlide();
	},
	nextSlide: function(event){
		if(event) event.preventDefault;
		if(this.currentStep < this.stepCount-1) this.currentStep++;
		else this.currentStep = 0;
		this.switchSlide();
	},
	switchSlide: function(){
		if(this.vertical) {
			var _offset = -this.stepHeight*this.currentStep;
			if(_offset < this.maxOffset) _offset = this.maxOffset;
			new Effect.Morph(this.slider, {
				style: {marginTop : _offset+'px'},
				duration: this.slideSpeed
			});
		} else {
			var _offset = -this.stepWidth*this.currentStep;
			if(_offset < this.maxOffset) _offset = this.maxOffset;
			new Effect.Morph(this.slider, {
				style: {marginLeft : _offset+'px'},
				duration: this.slideSpeed
			});
		}
		this.autoSwitch();
		this.refreshStatus();
	},
	autoSwitch: function(){
		if(this.timer) clearTimeout(this.timer);
		if(this.autoSlide) this.timer = setTimeout(this.nextSlide.bind(this),this.switchTime);
	}
});
