// Awesome functions

(function ($) {
	$.fn.vAlign = function() {
		return this.each(function(i){
			var h = $(this).height();
			var oh = $(this).outerHeight();
			var mt = (h + (oh - h)) / 2;
			$(this).css("margin-top", "-" + mt + "px");
			$(this).css("top", "50%");
			$(this).css("position", "absolute");
		});
	};
})(jQuery);

(function ($) {
	$.fn.hAlign = function() {
		return this.each(function(i){
			var w = $(this).width();
			var ow = $(this).outerWidth();
			var ml = (w + (ow - w)) / 2;
			$(this).css("margin-left", "-" + ml + "px");
			$(this).css("left", "50%");
			$(this).css("position", "absolute");
		});
	};
})(jQuery);

// Lazyload 
$(function() {
	$(".story img").lazyload({
		placeholder : "img/grey.gif",
		threshold : 100
	});
}); 


// On Pageload
$(document).ready(function(){
		

	// modded fancybox for gallery
	$("a.fancybox").fancybox({
		'centerOnScroll': true
	});
	
	$("ul.tags li:first").addClass('first');


	// fancybox for stories
	$(".story a.footnote-link").fancybox({
		'centerOnScroll': true,
		'frameWidth': 520,
		'frameHeight': 75,
		'callbackOnShow': onShow
	});
	function onShow() {
		var source = $("#fancy_div").html();
		$("#fancy_div").empty();
		$("#fancy_div").prepend('<p>Source: ' + source + '</p>');
	} 

	// default fancybox
	$(".story .fancybox,.thenNowContent .fancybox").fancybox({
		'centerOnScroll': true
	});	

	// hide chapters
	$(".chapter:not(#chapter1)").hide();

	// story nav?
	var chaps = $(".story .chapter").size();
	var chap = 1;
	var nextChap = chap + 1;

	buildPreview = function(){
		var	preview = '';
			preview += '<div id="preview">';
			preview += '	<div class="nextChapterPreview">';
			preview += '		<h2></h2>';
			preview += '		<p class="pp"></p>';
			preview += '	</div>';
			preview += '	<a id="continue">Continue Reading</a>';
			preview += '</div>';
		$('#references').before(preview);
	}

	fillPreview = function(){
		var previewHeading = $('#chapter' + nextChap + ' h2').eq(0).html(); // get the first H2 in the next chapter
		var previewParagraph = $('#chapter' + nextChap + ' p').eq(0).html(); // get the first P in the next chapter
		$('#preview #continue').empty().append('Continue Reading (' + nextChap + ' of ' + chaps + ')');
		$('.nextChapterPreview h2').empty().append(previewHeading);
		$('.nextChapterPreview p.pp').empty().append(previewParagraph);
	}

	if(chaps > 1){
		buildPreview();
		fillPreview();
	}

	$("#continue").click(function(){
		if(nextChap == chaps){
			$('#chapter' + nextChap + '').slideDown(function(){
				$('#preview').hide();
			});
		} else {
			$('#chapter' + nextChap + '').slideDown();
			chap = chap + 1;
			nextChap = chap + 1;
			fillPreview();					
		}
	});

	// references links
	$("#footnotes,#image-credits").hide();
	$("a[href='#footnotes']").toggle( function(){ $("#footnotes").slideDown(); return false; },function(){ $("#footnotes").slideUp(); return false; });
	$("a[href='#image-credits']").toggle( function(){ $("#image-credits").slideDown(); return false; },function(){ $("#image-credits").slideUp(); return false; });

	
	$('.then .swapper').click(function(){
		swapLink = $(this);
		swapLink = swapLink[0].href;
		$('.then #swappee').attr('src',swapLink);
		return false;
	});
	$('.now .swapper').click(function(){
		swapLink = $(this);
		swapLink = swapLink[0].href;
		$('.now #swappee').attr('src',swapLink);
		return false;
	});
	
});
