$(document).ready(function() {
	
	
	// Toggle navigation block for each section
	$('#pages li.normal').waypoint(function(event, direction) {
		if (direction === 'down')
			$(this).children('.sheba').fadeIn();
		else
			$(this).children('.sheba').fadeOut();
	}, { offset: 240 });
	
	
	// Replace href with data-block attribute on load
	$('.sheba a').each(function() {
		var targetBlock = $(this).attr('data-block');
		
		$(this).attr('href', targetBlock);
	});
	
	
	// Smooth scroll anchor links
	$('.sheba a').smoothScroll({
		offset: -80,
		afterScroll: function() {
			var link = $(this).attr('href');
			
			$('#pages li h2').filter(function(a) {
				return ($(this).attr('data-slug') == link.substr(1)) ? 1 : 0;
			}).find('a').trigger('click');
		}
	});
	
	
	// Prevent default behaviour on project pagination
	$('#prev, #next').click(function() {
		$('#projects').cycle('pause');
		return false;
	});
	
	
	// Close button to hide currently displayed content
	$('a.close-trigger').live('click', function() {
		$(this).parent().slideUp().addClass('closed');
		return false;
	});
	

	// Configuration for home page sections
	if ($('body.home #main').length > 0) {
		
		// Allow all content section to expand
		$('#main .section').append('<div class="c closed"></div>');
		
		// Prepare the content-blocks for clicks/animations
		$('#pages a.sq-img, #pages h2 a, #pages a.read-more').click(function() {
			var id = $(this).parents('li').attr('id').substr(5);
			var url = $(this).attr('href');
			var parts = url.split('/');
			var start = url.match(/http:\/\//) ? 3 : 1;
			var newURL = "";
			for (var i=start; i<parts.length; i++) newURL += "/"+parts[i];
			
			document.location.hash = "#!/"+newURL.substr(1);
			prepareDiv(id, newURL);
			
			return false;
		});

		// Expand content section if hashbang (#!) is set
		var hash = document.location.hash.substr(1);
		
		if (hash.substr(0,1) == "!") {
			var parts = hash.substr(1).split('/');
			
			$.smoothScroll({
				scrollTarget: '#'+parts[1],
				offset: -80
			});
			
			var id = $("#"+parts[1]).attr('data-id');
			prepareDiv(id, parts[1]);
		}
	} else {
		// Redirect to home & pass along proper identifier
		var url = document.location.pathname.substr(1);
		var id = $('#post_id').attr('data-value');
		if ((id > 0) && (!String(id).match(/^(241|172)$/))) {
			document.location.replace("/#!/"+url);
		}
	}

}); // End of document ready


function prepareDiv(id, newURL) {
	if ((id == 124) || (id == 187)) {

		$("#post-"+id+" .c").html('<iframe src="'+newURL+'" width="100%" height="400" frameBorder="0" allowtransparency="true" topmargin="0" leftmargin="0" marginwidth="0" marginheight="0"></iframe>');
		$("#post-"+id+" .c").slideDown();
		$("#post-"+id+" .c").removeClass('closed').addClass('open');
		$("#post-"+id+" .c").append('<a class="close-trigger" href="#">Close</a>');
	} else {
		$("#post-"+id+" .c").load(newURL+" #main", function(r, s, xmlObj) {
			if (id == 50) prepareSlideshow();
			$(this).slideDown();
			$(this).removeClass('closed').addClass('open');
			$(this).append('<a class="close-trigger" href="#">Close</a>');
		});
	}
}


// Load the slideshow for projects
function prepareSlideshow() {
	 $('#projects').cycle({ 
		fx: 'scrollHorz', 
		timeout: 10000,
		speed: 500,
		autostop: 0,
		nowrap: 0,
		pause: true,
		prev: '#prev',
		next: '#next'
	});
}
