(function() {
	// Redirect to a hashed URL
	var splitPath = window.location.pathname.split('/');
	if (splitPath[1] == 'life-by-ed' && splitPath.length >= 3) {
		window.location.href = '/#!/life-by-ed/'+splitPath[2];
		return;
	}
}());

$(window).bind('hashchange', function() {
	var splitHash = window.location.hash.substring(3).split('/');
	
	var lifeByEdEl = $('#life-by-ed');
	var lifeByEdInner = lifeByEdEl.find('.inner');

	if (splitHash[0] == 'life-by-ed') {
		if (lifeByEdEl.hasClass('opened')) {
			var container = $('<div></div>');

			container.load('/'+splitHash[0]+'/'+splitHash[1]+' #life-by-ed .entry', function() {
				var contentEl = lifeByEdEl.find('.content');
				lifeByEdEl.find('.entry').remove();
				
				var newEntryEl = container.find('.entry');
				newEntryEl.css({
					opacity: 0
				});
				contentEl.append(newEntryEl);
				contentEl.find('h3').edify();
				newEntryEl.edMediaLoaded(function() {
					newEntryEl.css({
						opacity: 1
					});
				});
				
				FB.XFBML.parse();
			});
		} else {
			lifeByEdEl.edMarkLoading();

			var container = $('<div></div>');

			container.load('/'+splitHash[0]+'/'+splitHash[1]+' #life-by-ed .content', function() {
				var contentEl = container.find('.content');

				lifeByEdInner.append(container.find('.content'));
				
				var navContainer = $('<div></div>');

				function handlePreviousNextClick(event) {
					event.preventDefault();
					
					navContainer.load($(this).attr('href')+' #content .life-by-ed .nav', handleNavLoad);
					
					return false;
				}
				
				function handleNavLoad() {
					var splitHash = window.location.hash.substring(3).split('/');
					contentEl.find('.nav').replaceWith(navContainer.find('.nav'));
					contentEl.find('.nav a.entry-item').removeClass('current');
					contentEl.find('.nav a.entry-item').each(function() {
						if (splitHref[splitHref.length-1] == splitHash[1]) {
							$(this).addClass('current');
						}
					});
					contentEl.find('.nav a.previous, .nav a.next').click(handlePreviousNextClick);	
					contentEl.find('.nav a.entry-item').click(navItemClicked);
				}
				
				function navItemClicked(event) {
					event.preventDefault();
					var splitHref = $(this).attr('href').split('/');
					location.hash = '!/life-by-ed/'+splitHref[splitHref.length-1];

					$(this).siblings().removeClass('current');
					$(this).addClass('current');
					
					return false;
				}
				
				contentEl.find('.nav a.previous, .nav a.next').click(handlePreviousNextClick);	
				contentEl.find('.nav a.entry-item').click(navItemClicked);
				
				contentEl.find('h3').edify();
				
				FB.XFBML.parse();


				// A somewhat-ugly hack to make sure that the Life By ED box is always large enough to
				// contain all Facebook comments. The solution checks periodically if the height of the
				// comments-iframe has changed.
				var oldBoxHeight = 0;
				var gridCellHeight = 173;
				var gridVerticalGutter = 10;
				var paddingBottom = 50; // Always preserve some extra space to the bottom of the container

				var commentHeightCheckID = setInterval(function() {
					if (contentEl.parent().length == 0) {
						clearInterval(commentHeightCheckID);
					} else {
						var iframe = contentEl.find('.fb-comments');
						var commentsHeight = iframe.height();
						
						var iframeBottom = iframe.offset().top-lifeByEdEl.offset().top + iframe.height();
						var boxHeight = Math.ceil((iframeBottom+paddingBottom)/(gridCellHeight+gridVerticalGutter))*(gridCellHeight+gridVerticalGutter)-gridVerticalGutter;

						if (boxHeight != oldBoxHeight) {
							lifeByEdEl.edResize({
								height: boxHeight
							});
							
							oldBoxHeight = boxHeight;
						}
					}
				}, 200);

				lifeByEdEl.edMarkLoadingCompleted();
			});

			lifeByEdEl.edOpen();
		}
	}
});
