/**
 * Main JS file for GhostScroll behaviours
 */

var $post = $('.post');
var $fnav = $('.fixed-nav');
var $postafter = $('.post-after');
var $sitehead = $('#site-head');

/* Globals jQuery, document */
(function ($) {
	"use strict";
	function smoothScroll (el) {
		$('html, body').animate({
			scrollTop: el.offset().top
		}, 400);
	}
	$(document).ready(function(){
        
        // FitVids for responsive videos
        $('.post-content').fitVids();
	 
		$('.btn.first, #header-arrow').click( function () {
            var $first = $post.first();
			smoothScroll($first);
		});
		$('.btn.last').click( function () {
            var $last = $post.last();
			smoothScroll($last);
		});

        $('#totop-arrow').click(function () {
            smoothScroll($sitehead);
        });

        $('.fn-item').click(function (evt) {
            evt.preventDefault();
            var slug = $(this).attr("href");
            smoothScroll($(slug))
        });

        $(window).scroll( function () {
            var w = $(window).scrollTop();
            var g = $sitehead.offset().top;
            var h = $sitehead.offset().top + $sitehead.height()-100;
            
            // parallax stuff
            var paralex = 30 + w/13 + "%";
            $sitehead.css("background-position-y", paralex);

            // fade out fixed-nav
            if(w >= g && w<=h) {
                $('.fixed-nav').fadeOut('fast');
            } else if($(window).width()>500) {
                $('.fixed-nav').fadeIn('fast');
            }

            // highlight appropriate fixed-nav element
            $post.each(function () {
                var f = $(this).offset().top;
                var b = $(this).offset().top + $(this).height();
                var t = $(this).parent('.post-holder').index();
                // fixed-nav active post
                var i = $(".fn-item[item_index='"+t+"']");
                // previous arrow
                var a = $(this).parent('.post-holder').prev('.post-holder').find('.post-after');

                // deprecated
                $(this).attr('item_index', t);

                // fade in/out elements
                if(w >= f && w<=b) {
                    i.addClass('active');
                    a.fadeOut('slow');
                } else {
                    i.removeClass('active');
                    a.fadeIn('slow');
                }
            });
        });

		$('ul li').before('<span class="bult fa fa-asterisk icon-asterisk"></span>');
		$('blockquote p').prepend('<span class="quo icon-quote-left"></span>');
		$('blockquote p').append('<span class="quo icon-quote-right"></span>');
	});
	
    // Replace post text with font-awesome fonts
	$post.each(function () {
		var postText = $(this).html();
		var fa  = [];
		for(var i=0; i < icons.length; i++) {
			fa[i]       = {};
			fa[i].str   = "@"+ icons[i]+ "@";
			fa[i].icon  = icons[i];
			fa[i].int   = postText.search(fa[i].str);

			if(fa[i].int > -1 ) { 
				fa[i].count = postText.match(new RegExp(fa[i].str,"g")).length;
				for(var j=0; j < fa[i].count; j++) {
					$(this).html($(this).html().replace(fa[i].str, "<i class='fa "+fa[i].icon+"'></i>"))
				}
			}
		}
	});
	

}(jQuery));

@@ -964,35 +964,33 @@
     
-$(window).scroll( function 
+function isHeadInViewport
 () %7B
@@ -1006,17 +1006,30 @@
     var 
-w
+scrollPosition
  = $(win
@@ -1062,17 +1062,25 @@
     var 
-g
+topOfHead
  = $site
@@ -1118,34 +1118,32 @@
 var 
-h = $sitehead.offset().top
+bottomOfHead = topOfHead
  + $
@@ -1163,14 +1163,17 @@
 ht()
--
+ - 
 100;%0A
+%0A
     
@@ -1184,80 +1184,326 @@
     
-%0A            // parallax stuff%0A            var paralex = 30 + w/13 + %22%25%22
+var isAboveHead = topOfHead %3E scrollPosition;%0A            var isBelowHead = scrollPosition %3E bottomOfHead;%0A            return (!isAboveHead && !isBelowHead);%0A        %7D%0A%0A        function shouldDisplayNav() %7B%0A            var isTooNarrow = $(window).width() %3C 500;%0A            return (!isTooNarrow && !isHeadInViewport())
 ;%0A  
@@ -1511,62 +1511,51 @@
     
+ %7D%0A%0A
      
-$sitehead.css(%22background-position-y%22, paralex);%0A
+   $(window).scroll( function () %7B
 %0A   
@@ -1604,22 +1604,26 @@
  if(
-w %3E= g && w%3C=h
+shouldDisplayNav()
 ) %7B%0A
@@ -1658,19 +1658,18 @@
 v').fade
-Out
+In
 ('fast')
@@ -1687,39 +1687,25 @@
    %7D
- else if($(window).width()%3E500)
+%0A            else
  %7B%0A 
@@ -1731,34 +1731,35 @@
 fixed-nav').fade
-In
+Out
 ('fast');%0A      

Refactor original fade controls