var Feature = function($container, $features, settings) {
	this.$container = $container;
	this.$features = $features;
	this.$feature = $features.eq(0);
	
	this.Timeout = "";
	this.Index = 0;
	this.Stopped = false;
	this.Started = false;
	this.IE6 = $.browser.msie & $.browser.version.indexOf("6.") > -1;

	this.settings = {
		fadeInSpeed : 550, //ms
		fadeOutSpeed : 750, //ms
		intervals : [3000],
		effect : "default", // default, fade, slide
		onRotateBefore : null,
		onRotateAfter : null,
		onStart : null
	};
	
	$.extend(this.settings, settings);
	
	var self = this;
	
	this.getIndex = function()
	{
		return this.Index;
	};
	
	this.setIndex = function(index)
	{
		this.Index = index;		
	};
	
	this.getTabs = function()
	{	
		return self.$container.find("ul[id]");
	};
	
	this.getActiveTab = function()
	{	
		return self.getTabs().eq(self.getIndex());
	};
	
	this.getPanes = function()
	{
		return self.$features;
	};
	
	this.getActivePane = function()
	{
		return self.getPanes().eq(self.getIndex());
	};
	
	this.getRotation = function(rotation)
	{
		var default_settings = {
			idx : self.getIndex(),
			stop_rotation : false
		}
		
		if(rotation === undefined)
			var rotation = {};
	
		if(rotation.idx === undefined)
		{
			rotation.idx = self.getIndex();
		}

		$.extend(default_settings, rotation);
		rotation = default_settings;
		
		if(rotation.idx > this.getPanes().length - 1)
		{
			rotation.idx = 0;
		}
		else if(rotation.idx < 0) {
			rotation.idx = this.getPanes().length - 1;
		}
		
		this.setIndex(rotation.idx);
		this.Stopped = rotation.stop_rotation;
		
		return rotation;
	}
					
	this.rotate = function(rotation) {		
		var $feature = [],
			$container = self.$container,
			$features = this.getPanes();

		rotation = self.getRotation(rotation);

		$feature = $features.eq(self.getIndex());
		self.$feature = $feature;
		
		if(self.settings.intervals[self.getIndex()] == 0)
			self.Stopped = true;
			
		if(typeof settings.onRotateBefore === "function")
			settings.onRotateBefore(rotation);

		window.clearTimeout(self.Timeout);
		
		if(self.Started)
		{ 
			self.initEffect();
		}
		else
		{
			if(typeof settings.onStart === "function")
				settings.onStart();
		}
		
		if(typeof settings.onRotateBefore === "function")
			settings.onRotateBefore(rotation);
		
		self.Started = true;

		if(!self.Stopped)
		{
			self.Timeout = window.setTimeout(function() { self.setIndex(self.getIndex() + 1); self.rotate(); }, self.settings.intervals[self.getIndex()]);
		}
	};
	
	this.addEffect = function(effect)
	{
		
	};
	
	this.initEffect = function()
	{
		var $features = self.getPanes(),
			$feature = self.getActivePane(),
			$features_x = $features.not($feature);
		
		if(typeof self.settings.effect != "function")
		{
			switch(self.settings.effect)
			{
				case "fade":
					$features_x.stop().show().animate({opacity : 0}, self.settings.fadeOutSpeed, function() { $features_x.hide(); });		
					$feature.stop().show().animate({opacity : 1}, self.settings.fadeInSpeed, function() { $feature.show(); if(self.IE6) this.style.removeAttribute("filter"); });		
					break;
				case "slide":
					
				default:
					$features_x.css({opacity : 1}).not($feature).hide();		
					$feature.show(); 
			}
		}
	};
	
	this.play = function(rotation)
	{ 
		self.rotate(rotation);
	};

	this.stop = function()
	{
		window.clearTimeout(self.Timeout);
		self.setIndex(0);
		self.Stopped = true;
		self.rotate({idx : 0, stop_rotation : true});
		self.Started = false;
	};
	
	this.pause = function()
	{
		window.clearTimeout(self.Timeout);
	};
	
	this.show = function(idx, stop_rotation)
	{
		self.pause();
		self.rotate({idx : idx, stop_rotation : stop_rotation});	
	};
	
	this.previous = function(stop_rotation)
	{
		self.pause();
		self.setIndex(self.getIndex() - 1);
		self.rotate({stop_rotation : stop_rotation});
	};	
	
	this.next = function(stop_rotation)
	{
		self.pause();
		self.setIndex(self.getIndex() + 1);
		self.rotate({stop_rotation : stop_rotation});
	};	
	
	this.initNavigation = function()
	{
		var	$feature_navigation = self.getTabs().show(),
			$feature_previous = $feature_navigation.parent().find(".previous"),
			$feature_next = $feature_navigation.parent().find(".next");
			
		this.getPanes().slice(1).css({opacity : 0});

		$feature_previous
			.click(function() { return false; })
			.mouseup(function() {	
				self.previous(true);
				return false;
			});
	
		$feature_next
			.click(function() { return false; })
			.mouseup(function() {	
				self.next(true);
				return false;
			});
		
		$feature_navigation.find("a").each(function(index) {
			$(this)
				.click(function() { return false; })
				.mouseup(function() {
					self.show(index, true);
				});
		});
	};
	
	(this.init = function() {
		if(self.getPanes().length > 1)
		{	
			self.initNavigation(); 
			self.play();
		}
	})();
};
