
var SlidingPager = new Class({
	Implements: Options,
	options: {
		numPages: 0,
		animDuration: 100
	},

	initialize: function(slidingPagerInitValues) {
		this.numPages = slidingPagerInitValues.length;
		this.linkUrls = slidingPagerInitValues;
		this.currentNum = 1;
		this.isAnimating = false;
		this.previousImg = null;
		this.currentImg = $$('#CustomerSpotlightPageCenterDiv img');
		this.currentImgSrc = String(this.currentImg.getProperty('src'));
		this.pageLeftButton = $$('#CustomerSpotlightPageLeftDiv img').addEvent('click', this.page.bind(this));
		this.pageRightButton = $$('#CustomerSpotlightPageRightDiv img').addEvent('click', this.page.bind(this));
		this.bodyElem = $('CustomerSpotlightPageCenterDiv');

		if (Cookie.read('slidingPagerIndex')) {
			var index = Number(Cookie.read('slidingPagerIndex'));
			this.currentNum = (index >= this.numPages) ? 1 : index + 1;
		} else {
			this.currentNum = Math.ceil(Math.random() * (this.numPages + 1));
		}
		Cookie.write('slidingPagerIndex', this.currentNum, { duration: 90 });
		this.currentImg.setProperty("src", '/images/Home/CustomerSpotlightSlide' + this.currentNum + '.png');
		$$('#CustomerSpotlightPageCenterDiv a').setProperty("href", this.linkUrls[this.currentNum - 1]);

		this.currentImgSrc = this.currentImg.getProperty("src")[0];
		for (var i = 1; i < this.numPages + 1; i++) {
			Asset.image(this.currentImgSrc.replace(/.\.png/, String(i) + '.png'));
		}

	},

	page: function(e) {
		if (!this.isAnimating) {
			this.isAnimating = true;
			var newItemLeftPosition = 0;
			var oldItemNewLeftPosition = 0;
			if (e.target.getProperty('src').indexOf('Left') > -1) {
				this.currentNum = (this.currentNum == 1) ? this.numPages : this.currentNum - 1;
				newItemLeftPosition = '410px';
				oldItemNewLeftPosition = '-410px';
			} else { //right
				this.currentNum = (this.currentNum == this.numPages) ? 1 : this.currentNum + 1;
				newItemLeftPosition = '-410px';
				oldItemNewLeftPosition = '410px';
			}

			var newFlankingAnchor = new Element('a', {
				'href': this.linkUrls[this.currentNum - 1]
			});
			var newFlankingImg = new Element('img', {
				'src': this.currentImgSrc.replace(/.\.png/, String(this.currentNum) + '.png'),
				'styles': { 'position': 'absolute', 'left': newItemLeftPosition }
			});
			this.newFlankingImg = newFlankingImg;
			this.newFlankingImg.inject(newFlankingAnchor);
			newFlankingAnchor.inject(this.bodyElem);

			new Fx.Elements([this.currentImg, newFlankingImg], {
				onComplete: (function() {
					this.currentImg.dispose();
					this.currentImg = this.newFlankingImg;
					this.isAnimating = false;
				}).bind(this)
			}).start({
				'0': {
					'left': [0, oldItemNewLeftPosition]
				},
				'1': {
					'left': [newItemLeftPosition, 0]
				}
			});
		}
	}
});

