/**
 * RokVideoScroller - A video scroller with prev/next for prev/next page.
 * 
 * @version		1.0
 * 
 * @license		MIT-style license
 * @author		Djamil Legato <djamil [at] djamil.it>
 * @client		Andy Miller @ Rockettheme
 * @copyright	Author
 */
 
var RokVideoScroller = new Class({
	
	options: {
		visibleVideos: 3,
		duration: 800,
		blankimage: 'images/blank.png'
	},
	
	initialize: function(element, options) {
		this.setOptions(options);
		this.element = $(element) || 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(el, type) {
		if (!el) return this; 
		var dir = (el == this.arrowPrev) ? 'prev' : 'next';

		switch(type) {
			case 'disable':
				el.addClass('disabled').removeEvent('click', this.bounds[dir]);
				break;
			case 'enable':
				el.removeClass('disabled').addEvent('click', this.bounds[dir]);
				break;
			default:
		};
		
		return this;
	},
	
	addControls: function() {
		if (this.pages.total > 1) {
			var controls = new Element('div', {'class': 'video-controls'}).injectTop(this.element);
			var self = this;
			
			this.arrowPrev = new Element('img', {
				'class': 'control-prev', 
				'title': 'Previous', 
				'alt': 'prev', 
				'src': this.options.blankimage
			}).inject(controls);
			
			this.pages.all = [];
			(this.pages.total).times(function(i) {
				var page = new Element('img', {
					'class': 'control-page',
					'title': 'Page ' + (i + 1),
					'alt': (i + 1),
					'src': self.options.blankimage
				}).inject(controls).addEvent('click', function() {
					self.goTo(i + 1);
				});
				
				self.pages.all.push(page);
			});

			this.arrowNext = new Element('img', {
				'class': 'control-next', 
				'title': 'Next', 
				'alt': 'next', 
				'src': this.options.blankimage
			}).inject(controls);
			
			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(page) {
		if (!this.pages.all) return this;
		page = (page > this.pages.total) ? this.pages.total : (page < 1) ? 1 : page;

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

RokVideoScroller.implement(new Options, new Events);