/* JS DOCUMENT */
/*   
Document name: Core Javascript
Web Site Name: twentysevennames
URL: http://twentysevennames.co.nz
Copyright: twentysevennames
Designer: Michael Crosbie, Crosbie Design
Developer: Michael Crosbie, Crosbie Design */


/* ON READY FUNCTION CALLS */
	$(document).ready(function(){
		if($('body').hasClass("home")){
			home_page();
			jQuery.event.add(window,"load",home_page);jQuery.event.add(window,"resize",home_page);	
		}
		if($('body').hasClass("collections")){
			fixedScroller();
		}
	});
/* ON LOAD FUNCTION CALLS */
	$(window).load(function(){
	});
/* FUNCTIONS */


/* home_page() */

function home_page(){
	$("body").css("overflow", "hidden");
	var h=($(window).height()-60);
	$("#wrapper").css("height",h);
}

/* collections */
function goToByScroll(id){
     	$('html,body').animate({scrollTop: ($("#"+id).offset().top-104)},'slow');
}
function fixedScroller(){
	var top = ($('#fixed').offset().top-104) - parseFloat($('#fixed').css('marginTop').replace(/auto/, 0));
	  $(window).scroll(function (event) {
	    // what the y position of the scroll is
	    var y = $(this).scrollTop();

	    // whether that's below the form
	    if (y >= top) {
	      // if so, ad the fixed class
	      $('#fixed').addClass('fixed');
	    } else {
	      // otherwise remove it
	      $('#fixed').removeClass('fixed');
	    }
	  });
}
/* ---------- @ Blog functions -----------*/
function blog(){
			//settings on top
		//function that creates posts
		var postHandler = function(postsJSON) {
			$.each(postsJSON,function(i,post) {
				//post url
				var id = 'post-' + post.id;
				//create the HTML
				$('.blog_posts').append(post.parsed.master);
			});
		};
		//place the initial posts in the page
		//first, take care of the "load more"
		//when someone clicks on the "load more" DIV
		var start = 1;
		var desiredPosts = 10;
		var loadMore = $('#load-more');
		//load event / ajax
		loadMore.click(function(){
			//add the activate class and change the message
			loadMore.addClass('activate').text('Loading...');
			//begin the ajax attempt
			$.ajax({
				url: 'dimensions/dynamic_blog/load_more/', //ajax script -- same page
				data: {
					'start': start,
					'desiredPosts': desiredPosts
				},
				type: 'post',
				dataType: 'json',
				cache: false,
				success: function(responseJSON) {
					//reset the message
					loadMore.text('Load More');
					//increment the current status
					if(responseJSON.error){
						loadMore.text(responseJSON.error);
					}else{
						start += 1;
						//add in the new posts
						postHandler(responseJSON.article);
					}
				},
				//failure class
				error: function() {
					//reset the message
					loadMore.text('Oops! Try Again.');
				},
				//complete event
				complete: function() {
					//remove the spinner
					loadMore.removeClass('activate');
				}
			});
		});
}
