var currentTab, newHash, safariCurrentTab;
var fadeDuration = 500;
var seen = false;

// updateState function
// This function is called when:
// 1. after calling $.historyInit();
// 2. after calling $.historyLoad();
// 3. after pushing "Go Back" button of a browser
function updateState(hash) {
	if(!hash && !seen) {
		// force first category, first time only*
		$.history.load($("#tabHeader li:first").attr("class"));
		seen = true;
	}
	
	setCurrentTab(hash, fadeDuration);
	trackView(hash);
}

function trackView(hash) {
	if (hash) {
		hash = hash.replace("#", "");
		locationPath = document.location.pathname;
		if (locationPath.lastIndexOf("/") != (locationPath.length - 1)) {
			locationPath = locationPath + "/";
		}
		pageTracker._trackPageview(locationPath + hash);
	}
}


$().ready(function() {
	
	// hide all tabs	
	$("div.tab").hide();
	
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.history.init(updateState);

	// click functionality	
	$("ul#tabHeader li a").click(function() {
		tmpClass = $(this).parent().attr("class");
		if (window.location.hash.replace("#", "") != tmpClass) {
			$.history.load(tmpClass);
		}
	});
	
	// appearance fixer for FF
	$("ul#tabHeader li a").click(function() { 
		$(this).blur();
	});
	
	// add hover border
	$(".gift").hover(function() {
		if ($(this).position().left == 444) {
			$(this).addClass("hover-right");
		} else {
			$(this).addClass("hover");
		}
	}, function() {
		if (!$(this).hasClass("hover-right")) {
			$(this).removeClass("hover");
		} else {
			$(this).removeClass("hover-right");
		}
	});
	
	$(".gift").click(function() {
		if ( !jQuery(this).hasClass('bike') )
		{
			if (window.location.hash.replace("#", "") != "retailers") {
				$.history.load("retailers");
			}
		}
		else
		{
			var url = jQuery(this).find('a.bikeLink').eq(0).attr('href');
			if (url)
			{
				window.location = url;
			}
		}
	});
	
	$("#stateList a").click(function() {
		var selectedState;
		selectedState = $(this).attr("href").replace("#", "");
		$("#retailerList tr").hide();
		$("#retailerList tr#" + selectedState).show();
		$("#retailerList tr." + selectedState).show();
		return false;
	});
	
	$().ready(function() {
		// add zebra striping
	  	$("#retailerList tr:even").addClass("even");
		$("#retailerList tr:odd").addClass("odd");
	});
	
});

function setCurrentTab(t, fd) {
	// fd is the fade duration
	t = t.replace('#', '');
	currentTab = t;
	
	// show requested tab
	if (t.length) {
		$("div.tab").hide();
		$("#" + t + "Tab").fadeIn(fd);
		// get seleted marker
		var currentSelected = $('#tabHeader li a.selected').eq(0);
		// clear selected marker
		$("#tabHeader li a").removeClass("selected");
		tabButtonOutState( $(currentSelected) );
		// activate marker
		var selected = $("li." + t + " a");
		$(selected).addClass("selected");
	}
}

// Switches button image to over state
function tabButtonOverState( tabBtn )
{
	var img = jQuery(tabBtn).children('img').eq(0);
	var src = jQuery(img).attr('src');
	if ( src && src.indexOf('_over') == -1 )
	{
		src = src.substr( 0, src.lastIndexOf('.gif') ) + '_over.gif';
		jQuery(img).attr('src', src);
	}
}

// switches button image to out state
function tabButtonOutState( tabBtn )
{
	if (!jQuery(tabBtn).hasClass('selected'))
	{
		var img = jQuery(tabBtn).children('img').eq(0);
		var src = jQuery(img).attr('src');
		if (src)
		{
			src = src.replace( '_over', '' );
			jQuery(img).attr('src', src);
		}
	}
}
