/*
	Tabs - jQuery plugin
	
	Copyright (c) Justin Sepulveda
	
	Dual licensed under the MIT and GPL licenses:
	http://www.opensource.org/licenses/mit-license.php
	http://www.gnu.org/licenses/gpl.html

	Date: 2007.05.24
	Version: 1.0
 */

(function($) {
	
	$.fn.tabs = function(initial, settings) {
			
		if (typeof initial == 'object') settings = initial;
		
		settings = $.extend({
			
			initial: (initial && typeof initial == 'number' && initial > 0) ? --initial : 0,
			selectedClass: '.selected',
			tabClass: '.tab',
			panelClass: '.panel',
			remoteClass: '.remote',
			remote: null
			
		 }, settings || {});

		return this.each(function() {

			var container = this;
			
			var tab = settings.tabClass;
			var tabSet = $(tab, container);
			
			var panel = settings.panelClass;
			var panelSet = $(panel, container);
			
			var selected = settings.selectedClass.replace('.', '');
			
			tabSet.eq(settings.initial).addClass(selected);
			
			if (settings.remote) {
			
				$(settings.remoteClass).load(tabSet.eq(settings.initial).find('>a').attr('href'));
			
			} else {	
				
				panelSet.not(':eq(' + settings.initial + ')').hide();
					
			}
											
			tabSet.find('>a').click(function() {  
				
				var showThis = $(this.hash);
				var hideThis = panelSet.filter(':visible');
				
				if (settings.remote) { 

					$(settings.remoteClass).load(this.href);
					
				} else {
									
					if(showThis.is(':visible')) { return; }
				
					hideThis.hide();
					showThis.show();
	
				}
					
				$(this.parentNode).addClass(selected).siblings().removeClass(selected);
				
				return false;
		
			});
			
		});	
	};
	
})(jQuery);