
$(document).ready(function () {
	
		// ---- Last class ----------------------------------------------------------------------------------------------------------
		
	    $('.footer-box:first-child').addClass('first');
	    $('.footer-box:last-child,#footer-nav li:last').addClass('last');
		
		
		// ---- Top menu hover -------------------------------------------------------------------------------------------------
		
		$('#top-menu li').hover(function(){
			$(this).addClass('hover');							 
		}, function(){
			$(this).removeClass('hover');	
		});
	
	
		// ---- Tabs ----------------------------------------------------------------------------------------------------------------
	
		$(".tab-content").hide(); // Hide all content
		$("ul.tabs li:first").addClass("active").show(); // Activate first tab
		$(".tab-content:first").show(); // Show first tab content
		$("ul.tabs li").click(function() { // onClick
			$("ul.tabs li").removeClass("active"); // Remove any "active" class
			$(this).addClass("active"); // Add "active" class to selected tab
			$(".tab-content").hide(); // Hide all tab content
			var activeTab = $(this).find("a").attr("href"); // Find the href attribute value to identify the active tab + content
			$(activeTab).fadeIn();
			return false; // Prevent the default link behaviour
		});
		
		
		// ---- Accessible Looped Slider  ----------------------------------------------------------------------------------------------------------
	
		$('.container').css({'height' : '386px','overflow' : 'hidden','cursor' : 'pointer'}); // These styles are set via JS to ensure accessiblity
		$('.slides').css({'position' : 'absolute','top' : '0','left' : '0'});
		$('.slides div').css({'position' : 'absolute','top' : '0'});
		
		$('</a><a href="#" class="next"><img src="images/slider-arrow.jpg" width="36" height="36" alt="Next" /></a> \
		   <ul class="pagination"></ul>')
		.insertAfter('.container'); // Add navigation
		
		$('.slides').each(function() { // Cycle through each .slide and generate the pagination
			var n = 1;
			$(this).find('.slide').each(function() {
				$('<li><a href="#">' + n + '</a></li>').appendTo($(this).parents('.container').siblings('.pagination'));
				n=n+1;
			});		
		});
		
		$('#loopedSliderContent').loopedSlider({ //Run the slider plugin on #loopedSliderContent
			autoStart: 6000, // Set to positive number for auto start and interval time
			slidespeed: 1000
		});


		// ---- Columnizer ----------------------------------------------------------------------------------------------------------
        columnize();
	   
		// clean up disqus comment display
		$('.dsq-widget-list').each(function() {
			$(this).removeClass('dsq-widget-list').addClass('links');
			$(this).children('li').each(function() {
				var links = $(this).children('p').html();
				$(this).children('p').remove();
				$(this).append(links);
				// remove the contributor name link
				var name = $(this).children('a').first().text();
				$(this).children('a').first().remove();
				$(this).prepend('<p>' + name + ':</p>');
				// remove the page link
				$(this).children('a').first().remove();
				// grab the time link and wrap it around the whole comment
				var url = $(this).children('a').first().attr('href');
				$(this).children('span').wrap('<a href="' + url + '" />');
			});
		});
		$('.dsq-widget-item').removeClass('dsq-widget-item');
		
		// sliding sidebar
		$(window).resize(function() {moveSidebar()});
		$(window).scroll(function() {moveSidebar()});
		sbTopInit = $('#sidebar').position().top;

		// flash the info or error messages if they're shown
		$('.InfoMessage').animate( { color: 'orange' }, 500)
			.animate( { color: 'white' }, 500)
			.animate( { color: 'orange' }, 500)
			.animate( { color: 'white' }, 500);
		$('.ErrorMessage').animate( { color: 'orange' }, 500)
			.animate( { color: 'white' }, 500)
			.animate( { color: 'orange' }, 500)
			.animate( { color: 'white' }, 500);
		
});

var sidebarMoving = false;
var sbTopInit = 0;

function moveSidebar() {
	if (sidebarMoving) return;
	// get the position of the top & bottom of the sidebar
	var sbTop = $('#sidebar').offset().top;
	var sbHeight = $('#sidebar').height();
	var sbBottom = sbTop + sbHeight;
	// get the current viewport dimensions
	var winHeight = $(window).height();
	var winTop = $(window).scrollTop();
	var winBottom = winTop + winHeight;
	var docHeight = $(document).height();
	// if the sidebar has disappeared off the top of the screen, move it
	if (sbBottom < winTop)
	{
		sidebarMoving = true;
		$('#sidebar').animate(
			{ top: (Math.min(winTop - sbTopInit, docHeight - sbHeight - 750)) + 'px' }, 
			500, 
			function() { sidebarMoving = false; }
		);
	}
	else if (sbTop > winBottom)
	{
		sidebarMoving = true;
		$('#sidebar').animate(
			{ top: (Math.max(winBottom - sbHeight, 0)) + 'px' }, 
			500, 
			function() { sidebarMoving = false; }
		);
	}
	else if (winTop == 0)
	{
		sidebarMoving = true;
		$('#sidebar').animate(
			{ top: '0px' }, 
			500, 
			function() { sidebarMoving = false; }
		);
	}
}
			

function columnize()
{
    $(".columnizeList").each(function() {
        $(this).addClass("col1").clone().removeClass("col1").addClass("col2").insertAfter(this);
        $(".col1 li:odd").remove();
        $(".col2 li:even").remove();
    });
}

