if (typeof blankimage == 'undefined') var blankimage = 'images/blank.png';
var RokVideoScroller = new Class({
	options: {
		visibleVideos: 3,
		duration: 800,
		blankimage: 'images/blank.png'
	},
	initialize: function (a, b) {
		this.setOptions(b);
		this.element = $(a) || null;
		this.videos = this.element.getElements('div.video');
		this.options.visibleVideos = (this.options.visibleVideos > this.videos.length) ? this.videos.length: this.options.visibleVideos;
		this.wrapper = new Element('div').injectTop(this.element).adopt(this.videos);
		this.pages = {
			'current': 1,
			'total': Math.ceil(this.videos.length / this.options.visibleVideos)
		};
		this.size = {
			'page': this.videos[0].getSize().size.y,
			'total': this.videos[0].getSize().size.y * this.options.visibleVideos
		};
		for (var i = 0, l = this.videos.length; i < l; i += this.options.visibleVideos) {
			this.videos[i].addClass('first');
			if (i > 0) this.videos[i - 1].addClass('last')
		};
		this.wrapper.setStyles({
			'overflow': 'hidden',
			'height': this.size.total
		});
		this.scroller = new Fx.Scroll(this.wrapper, {
			duration: this.options.duration,
			wait: false
		});
		this.bounds = {
			'prev': this.previous.bind(this),
			'next': this.next.bind(this)
		};
		this.addControls().eventsManipulate(this.arrowPrev, 'disable').setPage(this.pages.current - 1);
		return this
	},
	eventsManipulate: function (a, b) {
		
		if (!a) return this;
		var c = (a == this.arrowPrev) ? 'prev': 'next';		
		
		
		switch (b) {
		case 'disable':
			a.addClass('disabled').removeEvent('click', this.bounds[c]);
			break;
		case 'enable':
			a.removeClass('disabled').addEvent('click', this.bounds[c]);
			break;
		default:
		};
		return this
	},
	addControls: function () {
		if (this.pages.total > 1) {
			var b = new Element('div', {
				'class': 'video-controls'
			}).injectTop(this.element);
			var c = this;
			this.arrowPrev = new Element('img', {
				'class': 'control-prev',
				'title': 'Previous',
				'alt': 'prev',
				'src': this.options.blankimage
			}).inject(b);
			this.pages.all = [];
			(this.pages.total).times(function (i) {
				var a = new Element('img', {
					'class': 'control-page',
					'title': 'Page ' + (i + 1),
					'alt': (i + 1),
					'src': c.options.blankimage
				}).inject(b).addEvent('click', function () {
					c.goTo(i + 1)
				});
				c.pages.all.push(a)
			});
			this.arrowNext = new Element('img', {
				'class': 'control-next',
				'title': 'Next',
				'alt': 'next',
				'src': this.options.blankimage
			}).inject(b);
			this.arrowPrev.addEvent('click', this.bounds['prev']);
			this.arrowNext.addEvent('click', this.bounds['next'])
		}
		return this
	},
	previous: function () {
		
		if (this.pages.current == 1) this.pages.current = 1;
		else this.pages.current--;
		if (this.pages.current < this.pages.total) {
			this.eventsManipulate(this.arrowNext, 'enable')
		}
		this.scroller.scrollTo(0, (this.pages.current - 1) * this.options.visibleVideos * this.size.page);
		if (this.pages.current == 1) {
			this.eventsManipulate(this.arrowPrev, 'disable');
			return this.setPage(this.pages.current)
		}
		return this.setPage(this.pages.current)
	},
	next: function () {

		if (this.pages.current >= this.pages.total) this.pages.current = this.pages.total;
		else this.pages.current++;
		
		
		if (this.pages.current > 1) {
			this.eventsManipulate(this.arrowPrev, 'enable')
		}
				
		this.scroller.scrollTo(0, (this.pages.current - 1) * this.options.visibleVideos * this.size.page);
		if (this.pages.current == this.pages.total) {
			this.eventsManipulate(this.arrowNext, 'disable');
			return this.setPage(this.pages.current)
		}
		return this.setPage(this.pages.current)
	},
	setPage: function (b) {
		if (!this.pages.all) return this;
		b = (b > this.pages.total) ? this.pages.total: (b < 1) ? 1 : b;
		this.pages.all.each(function (a) {
			a.removeClass('active')
		});
		this.pages.all[b - 1].addClass('active');
		return this
	},
	goTo: function (a) {
		a = (a > this.pages.total) ? this.pages.total: (a < 1) ? 1 : a;
		if (a == 1) this.eventsManipulate(this.arrowPrev, 'disable');
		else this.eventsManipulate(this.arrowPrev, 'enable');
		if (a == this.pages.total) this.eventsManipulate(this.arrowNext, 'disable');
		else this.eventsManipulate(this.arrowNext, 'enable');
		var b = (a - 1) * this.options.visibleVideos * this.size.page;
		this.pages.current = a;
		this.scroller.scrollTo(0, b);
		return this.setPage(a)
	}
});
RokVideoScroller.implement(new Options, new Events);
