if(typeof mcd === 'undefined') {
	var mcd = {};	
}

if(typeof mcd.news === 'undefined') {
	mcd.news = {};
}

/**
 * Simply provides ajax functioanlity to load in more posts onto the
 * index page of the MCD news blog
 * @author dgalarza
 */
mcd.news.moreNews = (function () {
	var $ = jQuery;
	var config = {		
		'page'		: 1,
		'perPage' 	: 8,
		'uri'		: '/',
		'page_id'	: '39',
		'total_posts' : 0
	};
	
	/**
	 * gotMore : checks if there is more post. if so display link
	 */
		
	var getMore = function(){
		var onPage = $('.post').size();
		if(config.total_posts == onPage){
			$('#more-trigger').addClass('hide');
			
		}else {
			$('#more-trigger').removeClass('hide');
		}
	};
	
	/**
	 * Init
	 */
	var init = function (uConfig) {			
		$.extend(config, uConfig);				
		
		// The more posts click handler
		$('#more-trigger').click(function (e) {
			// Move on to the next page
			config.page++;
				
			e.preventDefault();			
			
			// Request
			$.get(config.uri, {
				'page_id' : config.page_id,
				'pageOffset' : config.page,
				'perPage' : config.perPage								
			}, parseResponse);			
		});
		getMore();
	};
	
	/**
	 * Parses the ajax response and pushes it to the page
	 */
	var parseResponse = function (response) {	
		$('#more-posts').append(response);
		getMore();
	};
	
	return {
		'init' : init
	};
	
})();