var selectedTabID = 'public_sector_tab';
var selectedProductID = 'public_sector';
var autoSwitch = true;
var switchDelay = 5000;

function switchTab(target, tabName) {
	// Only switch if this is not the currently selected tab
	if(!$(target).parent().hasClass('selected')) {
		$(target).parent().addClass('selected');
		$('#' + selectedTabID).removeClass('selected');
		selectedTabID = $(target).parent().attr('id');

		$('#products_switcher #' + selectedProductID).hide();
		$('#products_switcher #' + tabName).show();
		$('#products_switcher #' + tabName).css('filter', '');
		selectedProductID = tabName;
	}
}

function autoSwitchTab() {
	if(autoSwitch) {
		// If this is the last tab, go to the first tab instead
		if(selectedProductID == $('#products_switcher .product:last').attr('id')) {
			var nextProductID = $('#products_switcher .product:first').attr('id');
		}

		else {
			var nextProductID = $('#products_switcher #' + selectedProductID + ' + .product').attr('id');
		}

		if(selectedTabID == $('#products_switcher .tab:last').attr('id')) {
			var nextTabID = $('#products_switcher .tab:first').attr('id');
		}

		else {
			var nextTabID = $('#' + selectedTabID + ' + .tab').attr('id');
		}

		$('#' + selectedTabID).removeClass('selected');
		$('#' + nextTabID).addClass('selected');
		selectedTabID = nextTabID;

		$('#products_switcher #' + selectedProductID).hide();
		$('#products_switcher #' + nextProductID).show();
		$('#products_switcher #' + nextProductID).css('filter', '');
		selectedProductID = nextProductID;
		
		switchTimer = setTimeout('autoSwitchTab()', switchDelay);
	}
}

function showProductInfoPopup(id) {
	$('.product_popup#' + id + '_popup').fadeIn();
}

$(document).ready(function() {
	// Stop automatically switching on mouseover
	$('#products_switcher').mouseenter(function() {
		autoSwitch = false;
		clearTimeout(switchTimer);
	});

	$('#products_switcher').mouseleave(function() {
		autoSwitch = true;
		switchTimer = setTimeout('autoSwitchTab()', switchDelay);
	});

	$('#search_query').focus(function() {
		if($(this).val() == 'Search')
			$(this).val('');
	});

	$('#search_query').blur(function() {
		if($(this).val() == '')
			$(this).val('Search');
	});

	

	// Product information popups
	$('#service_logos a').mouseenter(function() {
		productPopupTimer = setTimeout('showProductInfoPopup("' + $(this).attr('id') + '")', 500);
	});

	$('#service_logos a').mouseleave(function() {
		clearTimeout(productPopupTimer);
		$('.product_popup#' + $(this).attr('id') + '_popup').fadeOut();
	});


	// Social network logo rollovers
	$('#social_footer li a img').hide();

	$('#social_footer li').mouseenter(function() {
		$(this).children('a').children('img').fadeIn(200);
	});

	$('#social_footer li').mouseleave(function() {
		$(this).children('a').children('img').fadeOut(200);
	});


	// Newsletter popups
	$('#newsletter_link').click(function() {
		$('#newsletter_wrapper').fadeIn();
		return false;
	});

	$('#newsletter_close').click(function() {
		$('#newsletter_wrapper').fadeOut();
		return false;
	});

	switchTimer = setTimeout('autoSwitchTab()', switchDelay);
});
