(function($) {

	$.fn.extend({
		simpleTabs: function(config) {
			var defaults = {
				wrapperClass: 'content', // Classname to apply to the div that is wrapped around the original Markup
				currentClass: 'current', // Classname to apply to the LI of the selected Tab
				tabhead: 'h4', // Tag or valid Query Selector of the Elements to Transform the Tabs-Navigation from (originals are removed)
				tabheadclass: 'structural',
				tabbody: '.tab', // Tag or valid Query Selector of the Elements to be treated as the Tab Body
				fx: 'show', // can be "fadeIn", "slideDown", "show"
				fxspeed: 'normal', // speed (String|Number): "slow", "normal", or "fast") or the number of milliseconds to run the animation
				tabsListClass: 'tabs-list', // Class to apply to the generated list of tabs above the content
				syncheights: false, // syncs the heights of the tab contents when the SyncHeight plugin is available http://blog.ginader.de/dev/jquery/syncheight/index.php
				syncHeightMethodName: 'justify',
				clickableTabbody: false
			};
			this.options = $.extend(defaults, config);
			var o = this;
			return this.each(function(t) {
				var el = $(this);
				var list = '';
				var tabCount = 0;
				ids = [];
				$(el).wrapInner('<div class="' + o.options.wrapperClass + '"></div>');
				$(el).find(o.options.tabhead).each(function(i){
					if ($(this).find('a').length) {
						list += '<li class="tab-' + i + '">' + $(this).html() + '</li>';
						$(this).addClass(o.options.tabheadclass).find('a').contents().unwrap();
					}
					tabCount++;
				});

				$(el).prepend('<ul class="cf ' + o.options.tabsListClass + ' tabamount' + tabCount+'">' + list + '</ul>');
				var $tabList = $(el).find('ul.' + o.options.tabsListClass).first();
				$(el).find(o.options.tabbody).each(function(i) {
					if(o.options.clickableTabbody) {
						$(this).bind('click', function() {
							document.location.href = $tabList.find('a:eq('+i+')').attr('href');
						}).css('cursor','pointer');
					}
					$(this).find('a').attr('tabindex', '-1');
					$(this).addClass('tab-' + i).hide();
				});
				$(el).find(o.options.tabbody + ':first').show();
				$(el).find("ul > li:first-child").addClass(o.options.currentClass);

				if (o.options.syncheights && $.fn[o.options.syncHeightMethodName]) {
					$(el).find(o.options.tabbody)[o.options.syncHeightMethodName]();
					$(window).resize(function(){ 
						$(el).find(o.options.tabbody)[o.options.syncHeightMethodName]();
					});
				}

				$tabList.find('a').each(function(i) {
					var $tab = $(this);
					$tab.bind('mouseenter focus', function() {
						if ($tabList.find('li.' + o.options.currentClass).attr('class') != $tab.parent().attr('class')) {
							$(el).find(o.options.tabbody + ':visible').stop(true, true).hide();
							$(el).find(o.options.tabbody).eq(i).stop(true, true).show();
							$tabList.find('li.' + o.options.currentClass).removeClass(o.options.currentClass);
							$tab.parent().addClass(o.options.currentClass);
						}
					});
				});
			});
		}
	});

})(jQuery);

