$(function(){
	
	$(document).ready(function() {
		
		// Add custom link tracking
		var anchors = $("a").filter(function() { 
			return !(
				$(this).parent('ul.tabNav li').length || 
				$(this).parents("div#text-header").length ||
				$(this).parents("div#header").length ||
				$(this).parents("div.post").length ||
				$(this).parents("div#recent-posts").length
			);
		});
		anchors.click(trackLink);
		
		// Opens a.external in new window
		$('a.external').click(function(){
			window.open($(this).attr('href')); 
			return false;
		});
		// Opens a[rel="external"] in new window
		$('a[rel=external]').click(function(){
			window.open($(this).attr('href')); 
			return false;
		});

		// Sidebar tabs
		$('div.tabs').each(function() {
			var tabContainers = $('> div', this);
			var tabs = $('> ul.tabNav li a', this);

			$('ul.tabNav a', this).click(function() {
				tabContainers.hide().filter(this.hash).show();
				tabs.removeClass('selected');
				$(this).addClass('selected');
				return false;
			}).filter(':first').click();
		});
		
		// Next Steps
		$('#next-steps .header').click(
			function() {
				$(this).next().slideToggle(500, function() {
					var id = 'kpthrive_' + $(this).parent().attr('id'); 
					if ($(this).is(":visible")) { 
						$.cookie(id, 'show', { expires: 60, path: '/' 
					});
					$(this).parent().addClass("selected");
				} else { 
					$.cookie(id, 'hide', { expires: 60, path: '/' });
					$(this).parent().removeClass("selected"); 
				};
			});
			return false;
		}).next().hide();
		
		$('#next-steps .header').hover(
			function() { $(this).addClass('hover'); },
			function() { $(this).removeClass('hover'); 
		});
		
		// Test for cookie and if it's value is set to 'show' display content
		if ($.cookie('kpthrive_story') == 'show') { 
			$("#story").addClass("selected");
			$('#story div.content').css('display','block'); };
		if ($.cookie('kpthrive_share') == 'show') { 
			$("#share").addClass("selected");
			$('#share div.content').css('display','block'); };
		if ($.cookie('kpthrive_member') == 'show') { 
			$("#member").addClass("selected");
			$('#member div.content').css('display','block'); };
	});
});