/*
	jQuery tabs v0.9
	
	Wouter Beeftink
	wouter@footsteps.nl
*/
(function($) {
	$.fn.tabs = function(settings) {	
		
		var settings = $.extend({
			pages : '.tabs-pages',
			title : '.tabs-title',
			navigation : 'tabs-navigation',
			tab : 'tabs-navigation-element',
			active : 'active',
			start : 0,
			rotate : false,
			interval : 5000,
			effect : 'show',
			duration : 1000,
			mouseover : 'pause',
			reverse : false,
			insert : 'prepend'
		}, settings);
		
		return this.each(function() {
			
			var node = $(this);
			var pages = node.find(settings.pages);
			
			function Menu(node) {
				this.node = node;
				this.pages = [];
				this.timer = null;
				this.addPages = function(pages) {
					var menu = this;
					pages.each(function() {
						menu.addPage($(this));
					});
				};
				this.addPage = function(page) {
					this.pages.push(new Page(page));
				};
				this.showPage = function(n) {
					this.hidePages();
					this.pages[n].show();
					this.currentPage = n;
					if(this.navigation != null) {
						this.navigation.activateTab(n);
					};
					if(this.timer != null) {
						this.timer.reset();
					};
				};
				this.hidePage = function(n) {
					this.pages[n].hide();
				};
				this.hidePages = function() {
					for(var i in this.pages) {
						this.hidePage(i);
					};
				};
				this.previous = function() {
					if(this.currentPage == 0) {
						var n = this.pages.length - 1;
					} else {
						var n = this.currentPage - 1;
					};
					this.showPage(n);
				};
				this.next = function() {
					if(this.currentPage == (this.pages.length - 1)) {
						var n = 0;
					} else {
						var n = this.currentPage + 1;
					};
					this.showPage(n);
				};
				this.setNavigation = function(input) {
					if(input == null) {
						this.navigation = null;
					} else {
						if(typeof input == 'object') {
							this.navigation = new Navigation(input, this);
						} else if(typeof input == 'string') {
							var navigation = $('<div class="' + input + '" />');
							this.navigation = new Navigation(navigation, this);
							this.navigation.insert(settings.insert);
						};
						this.navigation.addTabs(this.pages);
					};
				};
				this.rotate = function() {
					var menu = this;
					var direction = settings.reverse ? 'previous' : 'next';
					this.timer = new Timer(function() {
						menu[direction]();
					}, settings.interval);
					this.timer.start();
					if(settings.mouseover == 'pause') {
						for(var i in this.pages) {
							this.pages[i].node.hover(function() {
								menu.timer.stop();
							}, function() {
								menu.timer.start();
							});
						};
					};
				};
			};
			function Page(node, effect) {
				this.node = node;
				this.show = function() {
					this.node.show();
				};
				this.hide = function() {
					this.node.hide();
				};
				this.fadeIn = function(speed) {
					this.node.setIndex(2);
					this.node.fadeTo(speed, 100);
				};
				this.fadeOut = function(speed) {
					this.node.setIndex(1);
					this.node.fadeTo(speed, 100);
				};
				this.fadeTo = function(opacity, speed) {
					this.node.fadeTo(speed, opacity);
				};
				this.setIndex = function(n) {
					this.node.css('z-index', n);
				};
			};
			function Navigation(node, menu) {
				this.node = node;
				this.menu = menu;
				this.tabs = [];
				this.insert = function(mode) {
					menu.node[mode](this.node);
				};
				this.addTabs = function(pages) {
					for(var i in pages) {
						var title = pages[i].node.find(settings.title);
						var tab = createTab(this, i, title);
						this.node.append(tab);
						this.tabs.push(tab);
					};
				};
				this.activateTab = function(n) {
					for(var i in this.tabs) {
						this.tabs[i].removeClass(settings.active);
					};
					this.tabs[n].addClass(settings.active);
				};
				function createTab(navigation, i, titleNode) {
					var tab = $('<div class="' + settings.tab + '" />');
					var link = createLink(navigation, i, titleNode);
					tab.append(link);
					return tab;
				};
				function createLink(navigation, i, titleNode) {
					var title = titleNode.text();
					var rel = titleNode.attr('rel');
					var text = rel.length ? rel : title;
					var href = titleNode.attr('href');
					var link = $('<a href="' + href + '" title="' + title + '">' + text + '</a>');
					link.data('n', i);
					link.click(function(event) {
						navigation.menu.showPage(parseInt($(this).data('n')));
						event.preventDefault();
					});
					return link;
				};
			};
			function Timer(fn, interval) {
				this.fn = fn;
				this.interval = interval;
				this.start = function() {
					this.timer = window.setInterval(this.fn, this.interval);
				};
				this.stop = function() {
					window.clearInterval(this.timer);
				};
				this.reset = function() {
					this.stop();
					this.start();
				};
			};
			
			var menu = new Menu(node);
			menu.addPages(pages);
			menu.setNavigation(settings.navigation);
			menu.showPage(settings.start);
			if(settings.rotate) {
				menu.rotate();
			};
			
		});
	};
})(jQuery);
