// Home
// slug: ''

// 3x3x3 Tab
// slug: #333n
// n = {'speedcubing', 'oll', 'meph', 'beginners'}

// 2x2x2 Tab
// slug: #2x2x2n
// n = {'', 'cll'}

// Big Cubes
// slug: n
// n ={'#4x4x4','#5x5x5'}

// MegaMinx
// slug: #megaminx

// gqTimer
// slug: #gqtimer

// Bonus
// slug: #bonus

// Buy
// slug: #buyersguide

// Clicking on a Tab gets you to that tab's content, does not go directly to an anchor for that content, i.e. clicking on the Megaminx tab shows that tab's content, does not go to #megaminx.

$(function(){
	
	$('.tab').hide(); // initially hides all .tab divs
	$('#tab-home').show(); // shows the Home tab on load
	$('#tabbar ul li:first').addClass('lefttab'); // Special case for left corner tab
	$('#tabbar ul li:last').addClass('righttab'); // Special case for right corner tab
	//On page load, finds proper tab
	var theURL = window.location.hash.substring(1); // Gets the HASH without the hash mark
 	determineTab(theURL); // Run determineTab
	showThisTab('tab-'+selectedTab); // Shows the determined tab

	// When Back or Forward button is pressed (requires jQuery ba bbq):
	$(window).bind( 'hashchange', function(e) { // Detects when the Hash changes
		var hash = location.hash.substring(1);
		determineTab(hash);
		showThisTab('tab-'+selectedTab);
	});

	// When a link is clicked
	$('a').click(function(){
		if($(this).attr('href').substring(0,1) == '#')
		{
			theLink = $(this).attr('href').substring(1);  // Finds the hash, assuming links are properly created to include just the "#______"
			determineTab(theLink);
			showThisTab('tab-'+selectedTab);
		}
	});
});
function determineTab(l)
{ // input a string that is the URL after the #.
// backup of original JS by Steve
//	if (l.substring(0,3) == '333'){
//		selectedTab = '333';
//	} else if (l.substring(0,5) == '2x2x2' || l == '222'){
//		selectedTab = '222';
	if (l.substring(0,3) == '333'){
		selectedTab = '333';
	} else if (l.substring(0,5) == '2x2x2' || l == '222eg1' || l == '222'){
		selectedTab = '222';
	} else if (l == 'promeph' || l == 'procolphil' || l == 'proohwest' || l == 'pro') {
		selectedTab = 'pro';
	} else if (l == '4x4x4' || l == 'k4' || l == '5x5x5' || l == 'bigcubes') {
		selectedTab = 'bigcubes';
	} else if (l == 'megaminx' || l == 'mastermorphix' || l == 'square1' || l == 'mega'){
		selectedTab = 'mega';
	} else if (l == 'gqtimer' || l == 'gqtimerinfo'){
		selectedTab = 'gqtimerinfo';
	} else if (l == 'bonus' || l == 'bonusstuff'){
		selectedTab = 'bonusstuff';
	} else if (l == 'buy' || l == 'buyersguide'){
		selectedTab = 'buyersguide';
	} else {
		selectedTab = 'home';
		$('#li-home').addClass('active');
	}
	$('#li-'+selectedTab).addClass('active').siblings().removeClass('active'); // updates the 'active' tab link in the navigation bar.
}
function showThisTab(x)
{
	$('.tab:visible[id!='+x+']').hide(); // hides visible tabs n.e. theTab
	$('.tab:hidden[id='+x+']').show(); // shows theTab if it is hidden
}
