$.GingerNinja = {
	init: function(){
		// Hide all articles except the first
		$('article').each(function(){
			$(this).addClass('hide');
		})
		$('#projects ul li a').click($.GingerNinja.switchArticle);

		// If we already have a URL #, jump straight to it
		var start = window.location.hash;
		if (start.length>0){
			 $('a[href$="'+start+'"]').click();
		} else {
			$('#content .article:first-child').addClass('current');
			$('#projects ul :first-child').addClass('current');
		}
		
		// Add previous/next controls
		$('body').prepend($('<ul></ul>',{'id':'paging'})
			.append($('<li></li>',{'id':'prev'}).append($('<a></a>',{'text':'Previous'}).click(function(){
				$('#projects .current').prev().find('a').click();
			})))
			.append($('<li></li>',{'id':'next'}).append($('<a></a>',{'text':'Next'}).click(function(){
				$('#projects .current').next().find('a').click();
			})))
			).addClass('paging');
		
		
		// Final touch: if the screen is tall enough, fix the position of the menu
		if ($(window).height()>$('#projects').height()){
			$('#projects').css('position','fixed');
		}
	},
	
	switchArticle: function(){
		$('#wrapper').scrollTop();

		if (!!document.createElement('video').canPlayType){
			if ($('#content .current video')[0]){
				$('#content .current video')[0].pause();
			}
		}
		$('#content .current').removeClass('current');
		// Show article, set current
		$($(this).attr('href')).addClass('current');
		$('html, body').animate({scrollTop:0}, 'fast');
		// Unset current current
		$('#projects .current').removeClass('current');
		
		$(this).parents('li').addClass('current');
	}
};

$(document).ready(function(){
	$.GingerNinja.init();
});
