$(function() {
	$('body').addClass('js');
	
	//apply ajax page loading of decades
	$('.decades ul li a').click(function(){
		var thisLink=$(this);
		$('.timeline').empty().append('<div class="loading">Loading</div>');
	
		$('.timelinetitle .article_title').load($(this).attr("href") +' .timelinetitle .article_title');
		$('.timeline').load($(this).attr("href") +' .timeline', function() {			
			$('.decades li').removeClass("current");
			$(thisLink).parent().addClass("current");
			
			applyScrolling();
			applyPagination();
			$('.timeline').fadeIn();
		});
		return false;
	});
	
	//apply pagination next/prev clicks
	$('.decades .prev').click(function(){
		var currentLink=$('.decades li.current a');
		$('.decades li.current').prev().find('a').click();
		return false;
	});
	
	$('.decades .next').click(function(){
		$('.decades li.current').next().find('a').click();
		return false;
	});
	
	applyScrolling();
	applyPagination();
});

function applyPagination(){
	if($('.decades li.current:first-child').length){
		$('.decades .prev').addClass("prev_disabled");
	}else{
		$('.decades .prev').removeClass("prev_disabled").addClass("prev");
	}
	
	if($('.decades li.current:last-child').length){
		$('.decades .next').addClass("next_disabled");
	}else{
		$('.decades .next').removeClass("next_disabled").addClass("next");
	}
}

function applyScrolling(){
	eventsListingsTop = 0;
	scrollDirection = 0;
	listingHeight = 0;
	upLink = $('<a class="up" href="javascript:void(0)">Up</a>');
	downLink = $('<a class="down" href="javascript:void(0)">Down</a>');
	eventsListings = $('.timeline .events_listing ul');
	
	/* Image lightboxes */
	$('.events_listing ul li a.illustration').fancybox();
	
	/* Timeline  */
	$('.timeline .navigation').append(upLink).append(downLink);
	eventsListings
		.wrap('<div class="events_viewport"></div>')
		.after('<div class="fade_out"></div>')
		.each(function() {
			if (this.offsetHeight > listingHeight) listingHeight = this.offsetHeight;
		});
	
	upLink.addClass('up_disabled');
	
	minEventsListingsTop = $('.timeline .events_viewport').get(0).offsetHeight - listingHeight;
	if (minEventsListingsTop > 0) {
		minEventsListingsTop = 0;
		downLink.addClass('down_disabled');
	}
	upLink.mousedown(function() {
		startScroll(3);
		$('body').one('mouseup', stopScroll);
	}).click(function() {
		scrollOneStep(20);
	});
	downLink.mousedown(function() {
		startScroll(-3);
		$('body').one('mouseup', stopScroll);
	}).click(function() {
		scrollOneStep(-20);
	});
}


function startScroll(direction) {
	if (scrollDirection == 0) {
		scrollDirection = direction;
		continueScroll();
	} else { /* already scrolling */
		scrollDirection = direction;
	}
}
function scrollOneStep(scrollDirection) {
	/* return true if scrolling can continue after this */
	if (scrollDirection != 0) {
		eventsListingsTop += scrollDirection;
		if (scrollDirection > 0 && eventsListingsTop >= 0) {
			eventsListingsTop = 0;
			upLink.addClass('up_disabled');
			stopScroll();
			eventsListings.css({'top': eventsListingsTop + 'px'});
			return false;
		} else {
			upLink.removeClass('up_disabled');
			if (scrollDirection < 0 && eventsListingsTop <= minEventsListingsTop) {
				eventsListingsTop = minEventsListingsTop;
				downLink.addClass('down_disabled');
				stopScroll();
				eventsListings.css({'top': eventsListingsTop + 'px'});
				return false;
			} else {
				downLink.removeClass('down_disabled');
				eventsListings.css({'top': eventsListingsTop + 'px'});
				return true;
			}
		}
	}
}
function continueScroll() {
	if (scrollOneStep(scrollDirection)) setTimeout(continueScroll, 10);
}
function stopScroll() {
	scrollDirection = 0;
}





