$(document).ready(function(){
	
	$.address.init(function(e){
		
		// use the navigation
		$('#header ul li a.link').address();
		
		// use the contact titles
		$('#content #contact h2 a').address(function(){
			return 'contact/' + $(this).attr('href').substr(9);
		});
		
		// use the project thumbnails
		$('#content #work ul li a, #content #case-studies h2 a, #content #clients a').address(function(){
			return 'case-studies/' + $(this).attr('href').substr(6);
		});
		
		// use the thoughts/things titles
		$('#content #thoughts-things h2 a').address(function(){
			return 'thoughts-things/' + $(this).attr('href').substr(10);
		});
		
		// use the propganda titles
		$('#content #propaganda h2 a').address(function(){
			return 'propaganda/' + $(this).attr('href').substr(12);
		});
		
		// use the people titles
		$('#content #people h3 a').address(function(){
			return 'people/' + $(this).attr('href').substr(8);
		});
		
	}).change(function(e){
		
		// get the values from the URL
		var segment_1 = e.pathNames[0];
		var segment_2 = e.pathNames[1];
		
		// first set the target id as the default id value
		var targetid = '#' + segment_1;
		
		// next set the default offsetYModifier, this is because the padding-top
		// for case studies and blog articles is only 34px and it needs to be
		// 172px in order to align properly, so where applicable we should add
		// 138px to the offsetYModifier.
		var offsetYModifier = 172;
		
		// then check against specific sections
		if (segment_1 == 'case-studies') {
			targetid = '#work-' + segment_2;
			
		} else if (segment_1 == 'contact' && segment_2) {
			targetid = '#contact-' + segment_2;
			
		} else if (segment_1 == 'thoughts-things' && segment_2) {
			targetid = '#thoughts-' + segment_2;
			
		} else if (segment_1 == 'propaganda' && segment_2) {
			targetid = '#propaganda-' + segment_2;
			
		} else if (segment_1 == 'people' && segment_2) {
			targetid = '#people-' + segment_2;
		}
		
		
		// scroll to the appropriate content
		var offsetY = $(targetid).offset().top - offsetYModifier;
		var offsetX = $(targetid).offset().left - $('#content-wrapper').offset().left;
		$('html, body').stop().animate({scrollTop:offsetY, scrollLeft:offsetX}, {duration: 1500, easing:'easeInOutCubic'});
	});
	
	// hide the scrollbars at the beginning
	$('body').css('overflow','hidden');	
	
	// hide the content-wrapper and header
	$('#header').hide();
	
	//$('#content-wrapper').fadeOut();
	
	// fade away the logo
	$('#welcome .logo').delay(5000).fadeOut(1000, function(){
		$('#header').delay(4000).fadeIn(2000);
		$('#welcome .statement').delay(5000).fadeOut('slow', function(){
			$('body').css('overflow','auto');
			$('#welcome').fadeOut(2000, function(){
				$('#welcome').remove();
			});
		});
	});
	
	// listen for the click event to run the welcome image animation
	$('#welcome').click(function(){
		$('#content-wrapper').stop().fadeIn('fast');
		$('#header').stop().fadeIn('fast');
		$('body').stop().css('overflow','auto');
		$('#welcome .logo').stop().remove();
		$('#welcome .statement').stop().remove();
		$('#welcome').remove();
	});
});
