/*
* autoTabs (jQuery plugin) v1.0
* 2010 Copyright A navalla suíza http://anavallasuiza.com
* scrollLoader is released under the GNU Affero GPL version 3 - more information at http://www.fsf.org/licensing/licenses/agpl-3.0.html
*/

(function($) {
	var tabs;
	var current_container;
	var current_tab = '';
	var containers = '';
	var options = {
		'container_suffix': '_container',
		'selected_class': 'select'
	};
	
	function hideShow () {
		$(containers).hide();
		$('li', tabs).removeClass(options.selected_class);
		
		$(current_tab).addClass(options.selected_class);
		$(current_container).show();
	}
	
	//Public function
	jQuery.fn.autoTabs = function (user_options) {
		var anchor = unescape(self.document.location.hash);
		
		tabs = this;
		
		$.extend(options, user_options);
		
		$('li', tabs).each(function (i) {
			containers += ', #' + $(this).attr('id') + options.container_suffix;
		});

		containers = $(containers.substr(2));
		
		if (anchor) {
			current_container = $(containers).filter(anchor + options.container_suffix);
		}

		if (!current_container || !current_container.length) {
			current_container = $(containers).first();
		}
		
		current_tab = $(current_container).attr('id');
		current_tab = $('#' + current_tab.replace(options.container_suffix, ''));
		
		hideShow();
		
		$('li', tabs).click(function () {
			current_tab = this;
			current_container = $('#' + $(this).attr('id') + options.container_suffix);
			hideShow();
		});
	}
})(jQuery);
