var stick = function(el){
	var w = $(window);
	var sticker = $(el);
	var topOff = 56;
	w.bind('scroll', function(){
		if((w.scrollTop() + topOff) > sticker.offset().top && sticker.css('position') != 'fixed'){
			sticker.css({'position': 'fixed', 'top' : topOff});
		}else if((w.scrollTop() + topOff) < sticker.offset().top && sticker.css('position') == 'fixed'){
			sticker.css({'position': 'relative', 'top' : 0});
		}
	});
};

var TOCLinks = function(){
	var anchors = $('#content h4 a');
	var ul = $('<ul class="anchorLinks"/>');
	
	anchors.each(function(){
		var _t = $(this);
		var li = $('<li><a href="#'+_t.attr('name')+'">'+ _t.text() +'</a></li>').appendTo(ul);
	});
	
	$('#anchorNav').append(ul);
	// stick('#anchorNav')
}

var NavIndicator = function(){
	var t = this;
	this.ul = $('#header .menu ul').eq(0);
	this.elems = this.ul.children('li');
	this.ind = $('<div class="indicator"/>');
	this.sel = this.ul.find('.current_page_item, .current_page_ancestor').eq(0);
	this.tl = this.sel.children('a');
	
	this.ind.width(this.tl.width()).css({left:this.tl.position().left, top:this.ul.offset().top}).insertAfter(this.ul);
	
	this.elems.bind('mouseenter', function(){
		var _t = $(this);
		t.ind.stop().animate({left:_t.position().left + 5, width:_t.children('a').width()}, {duration:400});
	});
	
	this.ul.bind('mouseleave', function(){
		var _t = $(this);
		t.ind.stop().animate({left:t.sel.position().left + 5, width:t.sel.children('a').width()}, {duration:400});
	});
}

$(document).ready(function() {
	var toc = new TOCLinks();
	var ni = new NavIndicator();
	
	if($('#content').hasClass('home')){
		$('#wrapper').css('background', 'none');
	}
	
	if($('#content').hasClass('about')){
		$('#header .menu ul li.page-item-2').addClass('.current_page_item');
	}
});

