var FeatureController = new Class({
	
	Implements: [Initialize],
					  
	attachments	: [],
	selectors	: [],		
	name		: 'FeatureController',

	initialize: function() {
		
		if (!$('feature-container')) {
			return;	
		}
		
		this.features			= [];
		this.navElements		= [];
		this.container			= $('feature-container');
		this.controlContainer	= this.container.getElement('.feature-nav-container');
		this.leftButton			= this.controlContainer.getElement('.feature-left-button');
		this.rightButton		= this.controlContainer.getElement('.feature-right-button');		
		this.navBar				= this.controlContainer.getElement('.feature-nav');
		this.featuresContainer	= this.container.getElement('.features');
		this.centreFrame		= this.featuresContainer.getElement('.centred');
		this.slider				= this.centreFrame.getElement('.feature-slider');
		this.active				= 0;
		this.timer				= null;
		this.featureWidth		= 960;
		
		$$('.feature').each(function(feature) {
			this.features.push(new Feature(feature, this));							 
		}.bind(this));
		
		this.slider.set('tween', {
			'duration' 		: 1000
		});
	
		this.setNav();
		this.startSlideshow();
	},
	
	setNav: function() {
		this.leftButton.addEvents({
			'click'			: function(e) {
				e.preventDefault();
				this.previous();
			}.bind(this),
			'mouseenter' 	: function(e) {
				e.stop();
				this.stopSlideshow();
			}.bind(this),
			'mouseleave'	: function(e) {
				e.stop();
				this.startSlideshow();
			}.bind(this)
		});
		
		this.rightButton.addEvents({
			'click'			: function(e) {
				e.preventDefault();
				this.next();
			}.bind(this),
			'mouseenter' 	: function(e) {
				e.stop();
				this.stopSlideshow();
			}.bind(this),
			'mouseleave'	: function(e) {
				e.stop();
				this.startSlideshow();
			}.bind(this)
		});
		
		this.navBar.addEvents({
			'mouseenter' 	: function(e) {
				e.stop();
				this.stopSlideshow();
			}.bind(this),
			'mouseleave'	: function(e) {
				e.stop();
				this.startSlideshow();
			}.bind(this)							  
		});
		
		this.navElements = this.navBar.getElements('div');
		
		this.navElements.each(function(element, index) {
			element.addEvents({
				'click'	: function(e) {
					e.preventDefault();
					this.activate(index);
				}.bind(this)
			});
		}.bind(this));
	},
	
	resetTimer: function() {
		$clear(this.timer);		

		this.timer = (function() {
			this.next();
		}.bind(this)).periodical(6000);
	},
	
	startSlideshow: function() {
		this.timer = (function() {
			this.next();
		}.bind(this)).periodical(6000);
	},
	
	stopSlideshow: function() {
		$clear(this.timer);
	},
	
	activate: function(index) {
		var position 	= this.featureWidth * index * -1;
		this.active		= index;
		
		this.navElements.each(function(element) {
			element.removeClass('active');							   
		});
		
		if (this.navElements[this.active]) {
			this.navElements[this.active].addClass('active');
		}

		this.slider.tween('left', position);
	},
	
	next: function() {
		this.active++;
		
		if (this.active >= this.features.length) {
			this.active = 0;	
		}
		
		this.activate(this.active);
	},
	
	previous: function() {
		this.active--;
		
		if (this.active < 0) {
			this.active = this.features.length - 1;	
		}
		
		this.activate(this.active);		
	}
	
});

var Feature = new Class({
	
	Implements: [Attachments, Initialize],
					  
	attachments	: [],
	selectors	: ['.feature'],		
	name		: 'Feature',

	initialize: function(element, controller) {
		
		// The prepare method of the Initialize class sets this.element, stores the current
		// class instance against the element, and attaches any functions to the element.
		this.prepare(element);
		
		this.controller			= controller;
		this.index				= this.controller.features.length;
		
		this.element.addEvents({
			'mouseenter' 	: function(e) {
				e.stop();
				this.controller.stopSlideshow();
			}.bind(this),
			'mouseleave'	: function(e) {
				e.stop();
				this.controller.startSlideshow();
			}.bind(this)
		});
	},
	
	activate: function() {
		this.controller.activate(this.index);
	}

});

window.addEvent('domready', function() {
	var featureController = new FeatureController();
});
