var $minitab = jQuery.noConflict();
$minitab(document).ready(function(){
	// We can use this object to reference the panels container
	var panelContainer = $minitab('div#minipanels');
	// Create a DIV for the tabs and insert it before the panel container
	$minitab('<div id="minitabs"></div>').insertBefore(panelContainer);
	
	// Find panel names and create nav
	// -- Loop through each panel
	panelContainer.find('div.minipanel').each(function(n){
		// For each panel, create a tab
		$minitab('div#minitabs').append('<a class="minitab" href="#' + (n+1) + '">' + $minitab(this).attr('title') + '</a>');
	});
	
	// Determine which tab should show first based on the URL hash
	var panelLocation = location.hash.slice(1);
	if(panelLocation){
		var panelNum = panelLocation;
	}else{
		var panelNum = '1';
	}
	// Hide all panels
	panelContainer.find('div.minipanel').hide();
	// Display the initial panel
	panelContainer.find('div.minipanel:nth-child(' + panelNum + ')').fadeIn('slow');
	// Change the class of the current tab
	$minitab('div#minitabs').find('a.minitab:nth-child(' + panelNum + ')').removeClass().addClass('minitab-active');
	
	// What happens when a tab is clicked
	// -- Loop through each tab
	$minitab('div#minitabs').find('a').each(function(n){
		// For each tab, add a 'click' action
		$minitab(this).click(function(){
			// Hide all panels
			panelContainer.find('div.minipanel').hide();
			// Find the required panel and display it
			panelContainer.find('div.minipanel:nth-child(' + (n+1) + ')').fadeIn('slow');
			// Give all tabs the 'tab' class
			$minitab(this).parent().find('a').removeClass().addClass('minitab');
			// Give the clicked tab the 'tab-active' class
			$minitab(this).removeClass().addClass('minitab-active');
		});
	});
});
