// WHOA, NELLY!!
// I am by no means a jQuery expert, and therefor this jQuery code is not the most effecient there is.
// But it works. So I'm happy.
// Don't judge me.




$(window).load(function() {

//--------------------------------------------------------------------------------------------
//DEFINE FUNCTIONS
//--------------------------------------------------------------------------------------------

function isScrolledIntoView(elem) {
	var docViewTop = $(window).scrollTop() +136;
    var docViewBottom = $(window).scrollTop() + 137;

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
}


//--------------------------------------------------------------------------------------------
//REMOVE BORDERS FROM COMMENT CHILDREN
//--------------------------------------------------------------------------------------------

$('#comments li li').css('border-bottom','none');

//--------------------------------------------------------------------------------------------
//POSITION STRING ILLUSTRATIONS
//--------------------------------------------------------------------------------------------

var siteheight = $('#container').height();
$('#strings').css('margin-top', -siteheight);

//--------------------------------------------------------------------------------------------
//CALCULATE SIZE OF GAP BETWEEN FOOTER AND BOTTOM OF PAGE TO LIMIT SCROLLING
//--------------------------------------------------------------------------------------------

var stringheight = $('#content').height() + $(window).height() -1;
$('#strings .left, #strings .right').css('height', stringheight);


//--------------------------------------------------------------------------------------------
//POSITION DATE, NAME AND GRAPHIC FLOATING ELEMENTS
//--------------------------------------------------------------------------------------------

var siteleft = $('body').width() - $('#content').width();
siteleft = siteleft/2;

$('.name').css('left', siteleft - 142);
$('.graphic').css('right', siteleft - 140);
$('.comment-container > .graphic').css('right', siteleft - 113);


//--------------------------------------------------------------------------------------------
//RE-POSITION STRINGS AND FLOATING ELEMENTS ON WINDOW RESIZE
//--------------------------------------------------------------------------------------------

$(window).resize(function() {
	
	siteheight = $('#container').height();
    $('#strings').css('margin-top', -siteheight);
    
    var stringheight = $('#content').height() + $(window).height() -1;
	$('#strings .left, #strings .right').css('height', stringheight);
	
	siteleft = $('body').width() - $('#content').width();
	siteleft = siteleft/2;
	
	$('.name').css('left', siteleft - 142);
	$('.graphic').css('right', siteleft - 140);
	$('.comment-container > .graphic').css('right', siteleft - 113);
    
});



//--------------------------------------------------------------------------------------------
// SHOW / HIDE DATE & GRAPHICS AS YOU SCROLL THROUGH THE DIFFERENT SECTIONS
//--------------------------------------------------------------------------------------------

$('#post-1').addClass('nextpost');

$(window).scroll(function() {
   		
   		if(isScrolledIntoView('#navigation') || isScrolledIntoView('#header')) { 
        		
        		$('#post-1').addClass('nextpost');
				
        }
   
   		$('.section').each(function(){
   			
   			if(isScrolledIntoView(this)) { 
        		
        		$(this).children('.name, .graphic').fadeIn(100);
        		$('#post-1').removeClass('nextpost');
				$(this).prev('.post').addClass('prevpost');
				$(this).next('.post').addClass('nextpost');
				
        	} else {
        		
        		$(this).children('.name, .graphic').fadeOut(100);
        		$(this).prev('.post').removeClass('prevpost');
				$(this).next('.post').removeClass('nextpost');
        	}
   			
   		});
   		
   		if(isScrolledIntoView('#search') || isScrolledIntoView('#links') || isScrolledIntoView('#footer')) { 
        		
        		$('#post-5').addClass('prevpost');
				
        }
   		    
});



//--------------------------------------------------------------------------------------------
// DETERMINE SECTION POSITIONS TO SCROLLÊTO
//--------------------------------------------------------------------------------------------

var post1_pos = $('#post-1').position('top');
	post1_pos = post1_pos.top;
	post1_pos = post1_pos -55;

var search_pos = $('#search').position();
	search_pos = search_pos.top;
	search_pos = search_pos - 88;

var links_pos = $('#links').position('top');
	links_pos = links_pos.top;
	links_pos = links_pos - 102;

var footer_pos = $('#footer').position('top');
	footer_pos = footer_pos.top;
	footer_pos = footer_pos - 113;



//--------------------------------------------------------------------------------------------
// CONTROL SCROLLING OF LINKS IN QUICKLINKS BAR
//--------------------------------------------------------------------------------------------

$('#quicklinks li:contains(Next)').click(function() {

		var post_pos = $('.nextpost').position('top');
		post_pos = post_pos.top;
		post_pos = post_pos -55;

		$.scrollTo(post_pos, 800);

});

$('#quicklinks li:contains(Prev)').click(function() {

	var post_pos = $('.prevpost').position('top');
		post_pos = post_pos.top;
		post_pos = post_pos -55;

		$.scrollTo(post_pos, 800);
		
});


$('#quicklinks li:contains(Top)').click(function() {

	$.scrollTo(0, 800);

});

$('#quicklinks li:contains(Bottom)').click(function() {

	$.scrollTo(footer_pos, 800);

});

$('#quicklinks li:contains(Post)').click(function() {

	$.scrollTo(post1_pos, 800);

});

$('#quicklinks li:contains(Comments)').click(function() {

	var comments_pos = $('#comments').position();
	comments_pos = comments_pos.top;
	comments_pos = comments_pos - 6;

	$.scrollTo(comments_pos, 800);

});

$('#quicklinks li:contains(Search)').click(function() {

	$.scrollTo(search_pos, 800);

});

$('#quicklinks li:contains("Links")').click(function() {

	$.scrollTo(links_pos, 800);

});



//--------------------------------------------------------------------------------------------
//CLEAR FORM FIELDS ON FOCUS
//--------------------------------------------------------------------------------------------

$('#searchform .field').focus(function() {
	
	var val = $(this).val();
	
	if(val == 'Enter search keywords...'){
		$(this).val('');
	}
	
	});
		
$('#searchform .field').blur(function() {
	
	var val = $(this).val();
	
	if(val == ''){
		$(this).val('Enter search keywords...');
	}
		
	});

$('#commentform #author').focus(function() {
	
	var val = $(this).val();
	
	if(val == 'Name'){
		$(this).val('');
	}
	
	});
		
$('#commentform #author').blur(function() {
	
	var val = $(this).val();
	
	if(val == ''){
		$(this).val('Name');
	}
		
	});
	
$('#commentform #email').focus(function() {
	
	var val = $(this).val();
	
	if(val == 'Email'){
		$(this).val('');
	}
	
	});
		
$('#commentform #email').blur(function() {
	
	var val = $(this).val();
	
	if(val == ''){
		$(this).val('Email');
	}
		
	});
	
$('#commentform #url').focus(function() {
	
	var val = $(this).val();
	
	if(val == 'Website'){
		$(this).val('');
	}
	
	});
		
$('#commentform #url').blur(function() {
	
	var val = $(this).val();
	
	if(val == ''){
		$(this).val('Website');
	}
		
	});

});