var FeatureSlider = function(selector) {

	var features,
		links,
		timer,
		current_feature;
	
	this.init(selector);

};

FeatureSlider.prototype.settings = {
	timer_duration : 3000
};

FeatureSlider.prototype.init = function(selector) {

	var self = this;
	
	this.features 	= $(selector + " .feature");
	this.links		= $(selector + " .features-nav a");
	
	this.start_timer();

	this.hide_all_features();
	this.set_feature(0);

	this.links.bind("click", function(e) {
		
		e.preventDefault();

		self.on_link_click($(this));

	});

}

FeatureSlider.prototype.total_features = function() {
	
	return this.features.length;

}

FeatureSlider.prototype.set_feature = function(index) {
	
	this.hide_all_features();

	this.deactivate_all_links();
	
	this.show_feature(index);

	this.activate_link(index);

	this.current_feature = index;

}

FeatureSlider.prototype.show_feature = function(index) {
	
	var feature = this.get_feature(index);

	feature.show();

}

FeatureSlider.prototype.hide_feature = function(index) {
	
	var feature = this.get_feature(index);

	feature.hide();

}

FeatureSlider.prototype.hide_all_features = function() {
	
	this.features.hide();

}

FeatureSlider.prototype.get_feature = function(index) {
	
	return $(this.features[index]);

}

FeatureSlider.prototype.next_feature = function() {

	this.hide_feature(this.current_feature);

	this.deactivate_link(this.current_feature);
	
	this.current_feature = (this.current_feature + 1) % this.total_features();

	this.show_feature(this.current_feature);

	this.activate_link(this.current_feature);

}

FeatureSlider.prototype.prev_feature = function() {
	
	this.hide_feature(this.current_feature);

	this.deactivate_link(this.current_feature);
	
	this.current_feature = (this.current_feature - 1) % this.total_features();

	this.show_feature(this.current_feature);

	this.activate_link(this.current_feature);

}

FeatureSlider.prototype.activate_link = function(index) {
	
	var link = this.get_link(index);

	link.addClass("active");

}

FeatureSlider.prototype.deactivate_link = function(index) {
	
	var link = this.get_link(index);

	link.removeClass("active");

}

FeatureSlider.prototype.deactivate_all_links = function() {
	
	this.links.removeClass("active");

}

FeatureSlider.prototype.get_link = function(index) {
	
	return $(this.links[index]);

}

FeatureSlider.prototype.start_timer = function() {
	
	this.timer = setInterval(function(that) {
		that.on_timer()
	}, this.settings.timer_duration, this);

}

FeatureSlider.prototype.stop_timer = function() {
	
	clearInterval(this.timer);
	this.timer = null;

}

FeatureSlider.prototype.reset_timer = function() {
	
	this.stop_timer();
	this.start_timer();

}

FeatureSlider.prototype.on_timer = function() {
	
	this.next_feature();

}

FeatureSlider.prototype.on_link_click = function(link) {

	var link_index = link.data("index");

	this.set_feature(link_index);

	this.reset_timer();

}

var fs = new FeatureSlider("#features");
