// Put all your code in your document ready area
jQuery(document).ready(function($){
	
	// Handles correct Google Analytics tracking for
	// tabs. By default, Google ignores fragments, so all visits
	// point to the same page. We take care of that here
	// Also, the GATC is loaded at the start of the page for this page (in main-inner.tpl).
	function fragmentTrack() {
		tab = jQuery('.currentTab > a');
		if (tab && window.urchinTracker) {
			urchinTracker('/mail-server/features/' + tab.attr('href').substr(1) + '/');
		}		
	}
	
	// Hide all tabs
	jQuery('.jsTab').hide();
	
	var defaultDiv = jQuery('.defaultTab');
	var lhash = window.location.hash || undefined;
	
	// Test for special case of hash in location pointing to the defaultTab
	// Skip processing in this case
	if (!lhash || lhash.length <= 1 || defaultDiv.attr('id') != (lhash.substr(1) + '-panel')) {
		// Check the location 
		var tab = jQuery('#jsTabs a[href="' + lhash + '"]');
		
		if (tab.length) {
			defaultDiv.removeClass('defaultTab');
			defaultDiv = jQuery(tab.attr('href') + '-panel');
			var crtTab = jQuery('#jsTabs .currentTab');
			crtTab.removeClass('currentTab');
			tab.parent().addClass('currentTab');
		}
	}
	
	// Google Analytics page tracking for tabs
	fragmentTrack();
	
	// show only the default one
	defaultDiv.show();

	jQuery("#jsTabs a").click( function() {
		var j = jQuery(this);
		// No action for current tab
		if (j.parent().hasClass('currentTab')) {
			j.blur();
			
			// Google Analytics page tracking for tabs
			fragmentTrack();
			
			return;
		}
		
		// Current tab A tag - jquery extended
		var crtTab = jQuery('#jsTabs .currentTab');
		// Current div panel associated with current tab
		var crtDiv = jQuery(crtTab.children().attr('href') + '-panel');
		
		// Hide current active div
		crtDiv.hide();
		
		// Remove the currentTab class
		crtTab.removeClass('currentTab');
		// Add it to the new tab
		j.parent().addClass('currentTab');
		j.blur();
		
		// Display the corresponding panel
		jQuery(j.attr('href') + '-panel').fadeIn();
		
		// Modify the location
		window.location.hash = j.attr('href');
		
		// Google Analytics page tracking for tabs
		fragmentTrack();
		
		return false;
	});
});
