// Constants, override in calling HTML file after including supertabs.js
var HIDE_SUPERTAB_BUTTON_TEXT = true;
var supertab_click_callback = {};
var supertab_buttons_initialised = {};
var supertab_replace_anchor = {};

$(function(){
	initialise_supertabs();
});

function initialise_supertabs(){
	var tab_button_text, tab_to_show;
	var tab_containers = $('.supertabs');

	tab_containers.each(function(i, el){
		if(supertab_buttons_initialised[el.id]){
			return;
		}

		// Build the tab button list
		$(this).prepend('<ul class="supertab-buttons" id="supertab-buttons-' + i + '"></ul>');

		var tab_content_elements = $(this).find('.tab-content');
		if(tab_content_elements.length == 0){
			return;
		}

		tab_content_elements.each(function(ii){
			// Get tab button text & hide the originals
			if(HIDE_SUPERTAB_BUTTON_TEXT){
				tab_button_text = $(this).find('h2').text();
				$(this).find('h2').remove();
			}
			$('#supertab-buttons-' + i).append('<li id="supertab-buttons-' + $(this)[0].id + '"><a href="#' + tab_text_to_id(tab_button_text) + '"><b>'+(ii+1)+'.</b><span>' + tab_button_text + '</span></a></li>');
			$('#supertab-buttons-' + $(this)[0].id).mouseover(supertab_button_click);

			// Hide the tab contents
			$(this).hide();
			// Assign a new ID to the tab contents so that it doesn't jump down
			$(this)[0].id = 'supertab-' + $(this)[0].id;

		});

		tab_to_show = null;
		// Choose which tab to show
		if(location.hash != '' ){
			var hash_id = location.hash.replace('#', '');
		}
		if(location.hash != '' && $('#supertab-' + hash_id).hasClass('tab-content')){
			// Show the tab specified in the location
			tab_to_show = 'supertab-' + hash_id;
		}else if($(this).find('.tab-content.tab-visible').length > 0){
			// Show first tab with tab-visible class
			tab_to_show = $(this).children('.tab-content.tab-visible')[0].id;
		}else{
			// Show the first tab
			tab_to_show = $(this).children('.tab-content')[0].id;
		}

		if(tab_to_show != null){
			show_supertab(tab_to_show);
		}

	});
}

function supertab_button_click(e){;
	var tab_id = $(this)[0].id.replace('supertab-buttons-', '');
	show_supertab('supertab-' + tab_id);
	var tab_content = $('#supertab-' + tab_id);
	var container = tab_content.parent('.supertabs');
	if(supertab_replace_anchor[container[0].id]){
		location.href = '#' + tab_id;
	}
	return false;
}

function show_supertab(id){
	var tab_content = $('#' + id);
	var container = tab_content.parent('.supertabs');
	container.children('.tab-content:visible').each(function(){
		$(this).hide();
	});
	tab_content.show();
	container.children('.supertab-buttons').find('li.current').each(function(){
		$(this).removeClass('current');
	});
	$('#supertab-buttons-' + id.replace('supertab-', '')).addClass('current');
	$('#supertab-buttons-' + id.replace('supertab-', '')).find('a')[0].blur();
	if(supertab_click_callback[container[0].id] != null){
		supertab_click_callback[container[0].id].call(this, id);
	}

}

function tab_text_to_id(text){
	return text.replace(/[^a-z0-9]+/gi, '-').replace(/-{2,}/gi, '-').toLowerCase();
}
