(function($){
	$.fn.switcher = function(settings){
		var switcher = this;

		settings = $.extend({
			interval:    3000,
			activeClass: "active",
			selector:    "tr"
		}, settings);

		return this.each(function(){
			var e = $(this);
			var s = settings;

			e.hover(function(){
				e.addClass("switcher-lock");
				e.find(s.selector + "." + s.activeClass).removeClass(s.activeClass);
				window.clearInterval(e.intervalRef);
			}, function(){
				e.removeClass("switcher-lock");

				if(typeof e.hoverRef != "undefined"){
					e.hoverRef.addClass(s.activeClass);
				}

				if(typeof this.run != "undefined"){
					e.intervalRef = window.setInterval(this.run, s.interval);
				}
			});

			e.find(s.selector).hover(function(){
				e.hoverRef = $(this);
			}, function(){});

			this.run = function(){
				if(e.hasClass("switcher-lock")){
					return;
				}

				var ca = e.find(s.selector + "." + s.activeClass);

				if(ca.length > 0){
					ca.removeClass(s.activeClass);
				}

				if(ca.next().length > 0){
					var next = ca.next();
				}else{
					var next = e.find(s.selector + ":first-child");
				}

				next.addClass(s.activeClass);
			}

			this.run();
			e.intervalRef = window.setInterval(this.run, s.interval);
		});
	};
})(jQuery);
