// Init the originalTheme variable if it not exists
// Whe use the origianlTheme variable to store all 
// parameters and functions which is needed globaly
if (typeof originalTheme === 'undefined') {
	var originalTheme = {};
}


(function() {
	
	// init the news object
	originalTheme.news = {};
	
	jQuery(document).ready(function() {
		
		// Find all news macros to init
		jQuery('#main .news_wrapper').each(function() {
			initNews(jQuery(this));
		});
		
	
	});
	
	//
	// Private methods
	//
	
	/**
	 * Init the news macro with the first news items
	 */
	function initNews(newsMacro)
	{
		
		jQuery.ajax({
			type: "POST",
			url: contextPath + "/ajaxactions/load-more-news.action",
			data: {
				max : 0,
				stepSize : newsMacro.find('#stepSizeVarNews').html(),
				profilePic : newsMacro.find('#profilePicVarNews').html(),
				spaces : newsMacro.find('#spacesVarNews').html(),
				type : newsMacro.find('#typeVarNews').html(),
				labels : newsMacro.find('#labelsVarNews').html(),
				content : newsMacro.find('#contentVarNews').html()
			},
			success: function(html){
			
				// Validate the ajax request
				html = originalTheme.validateAjaxResponse(html);
				
				newsMacro.find('#news_container').append(html);
				
				newsMacro.find('#news_loader').remove();
							
				// Show the container
				newsMacro.show();
		
			}
		});
		
	}
	
	//
	// Public methods
	//
	
	/**
	* Load more news items in to the news_container
	*/ 
	originalTheme.news.loadMoreNews = function (newsMacro){
		newsMacro.find('#load_more_news_link .load_more_text').hide();
		newsMacro.find('#load_more_news_link .load_more_image').show();
		newsMacro.find('#load_more_news_link').addClass("load_more_link_loading");
		
		jQuery.ajax({
			type: "POST",
			url: contextPath + "/ajaxactions/load-more-news.action",
			data: {
				max : newsMacro.find('#maxVarNews').html(),
				profilePic : newsMacro.find('#profilePicVarNews').html(),
				spaces : newsMacro.find('#spacesVarNews').html(),
				type : newsMacro.find('#typeVarNews').html(),
				labels : newsMacro.find('#labelsVarNews').html(),
				stepSize : newsMacro.find('#stepSizeVarNews').html(),
				content : newsMacro.find('#contentVarNews').html()
			},
			success: function(html){
			
				// Validate the ajax request
				html = originalTheme.validateAjaxResponse(html);
		
				newsMacro.find('#news_vars').remove();
				newsMacro.find('#load_more_news_link').remove();
				newsMacro.find('#news_container').append(html);
				
			}
		});
	}
		
	/**
	 * Show a news item
	 */
	originalTheme.news.showNewsItem = function (nbr)
	{		
		jQuery('#news_' + nbr).show();			
		jQuery('#news_show_' + nbr).hide();
		jQuery('#news_hide_' + nbr).show();
	}			
	
	/**
	 * Hide a news item
	 */
	originalTheme.news.hideNewsItem = function (nbr)
	{
		jQuery('#news_' + nbr).hide();	
		jQuery('#news_show_' + nbr).show();
		jQuery('#news_hide_' + nbr).hide();
	}
	
})();

