$(function() {
	$.fn.simple_aria_tabs = function (options) {
		// creating the HTML - only touching the DOM once = performance win
		function create_tabs($headers, current_text, init_shown) {
			// also adding in ARIA attrs in here
			var tabs_html = "<ul role='tablist' class='simple_tab_nav'>";
			$headers.each(function(key, value) {
				var that = $(this);
				that.addClass('hidden');
				if (key !== init_shown) {
					tabs_html += "<li><a href='#tab" + key + "' role='tab' id='tab_" + key + "'><span>" + that.text() + "</span></a></li>";
				} else {
					tabs_html += "<li class='selected'><a href='#tab" + key + "' role='tab' id='tab_" + key + "'><span>" + that.text() + ' ' + current_text + "</span></a></li>";	
				}
			});
			tabs_html += "</ul>";

			return tabs_html;
		}

		// plugin defaults
		$.fn.simple_aria_tabs.defaults = {
			header_level:'h2',
			init_active:0,
			active_class:'active'
		};

	    return this.each(function () {
	        // collecting variables for use through-out the script
			var opts = $.extend({}, $.fn.simple_aria_tabs.defaults , options);
	        var $wrapper = $(this),
	            $headers = $wrapper.find(opts.header_level),
				$tab_containers = $wrapper.find(opts.header_level + ' + div'),
	  			// provide an extra hint for screen-reader users
				current_text = "<em> (current tab)</em>";

	        // ok - so first off lets create the tabs
			$wrapper.find(':header:first').before(create_tabs($headers, current_text, opts.init_active));
			// add an ID attribute that the tabs will use to identify which tab to show
			$tab_containers.each(function(key, value) {
				var labeled_by = 'tab_' + key;
				var that = $(this).attr('role','tabpanel').attr('aria-labelledby',labeled_by);
				that.attr('id','tab' + key).attr('tabIndex','-1').attr('aria-hidden','true');
				if (key === opts.init_active ) {
					that.addClass(opts.active_class).attr('aria-hidden','false');
				}
			});
			// assigning handlers to the anchors
			$wrapper.find('ul a').click(function() {
				var that = $(this),
					inner_text = that.find('span'),
					// IE grabs the entire URL - this is fail
					href = '#' + that.attr('href').split('#')[1],
					$current_selected = $wrapper.find('li.selected a');
				// remove current selected stuff - from tabs nav and also the tabs themselves
				$current_selected.find('em').remove();
				$wrapper.find('.selected').removeClass('selected');
				$wrapper.find('.' + opts.active_class).removeClass(opts.active_class).attr('aria-hidden','true');
				// adding the selected states back to the tabs
				that.parent().addClass('selected');
				inner_text.append(current_text);
				// show the selected tab
				$(href).addClass(opts.active_class).attr('aria-hidden','false').focus();
				return false;
			});
	    });
	};
});

$(function() {
	/* 
		pulling in all rider table from an external page to speed up initial page load
		once complete inits the tabs
	*/
	$('.tabbed_content').simple_aria_tabs({
		header_level:'h2'
	});
});
