//AJAX NEWS FUNCTIONS
jQuery(document).ready(function(){
		
	//Hide old news on click and show articles based on selected year
	jQuery('#childnav li a.year').click(function(){
		
		//Check if year is already selected
		if((jQuery(this).parent('li').attr('class')) == "here"){
		
			//Do Nothing
			
		}
		else {
		
			//Hide back link
			jQuery('a.viewall').slideUp('fast');
			
			jQuery('#childnav li').removeClass('here');
			jQuery('#childnav li').removeClass('expanded');
			
			//Hightlight selected year in the side list
			jQuery(this).parent('li').addClass('here');
			jQuery(this).parent('li').addClass('expanded');
			
			//Get Archive Year
			var archiveYearStr = jQuery(this).attr('id');
			var archiveYear = archiveYearStr.substr(5);
						
			//Add loader
			jQuery('#newsWrapper').block();
			
			//Hide previous news
			jQuery('#news').fadeOut('slow', function(){
			
				//Load articles based on year
				jQuery('#news').load('/releases/archive/', {year: archiveYearStr, language: language }, function(){
					
					//Remove border on last news item
					jQuery('#news div:last-child').addClass('last');
					
					//Enable ajax news articles again
					ajaxNewsEntries();
					
					//Fade in news
					jQuery('#news').slideDown('slow', function(){
						
						//Remove loader
						jQuery('#newsWrapper').unblock();
					
					});
				
				});
			
			});					
		
		}
	
	});

	//Hide old news on click and show articles based on selected year & month
	jQuery('#childnav li a.month').click(function(){
		
		//Check if year is already selected
		if((jQuery(this).parent('li').attr('class')) == "here"){
		
			//Do Nothing
		
		}
		else {
		
			//Hide back link
			jQuery('a.viewall').slideUp('fast');
			
			jQuery('#childnav li').removeClass('here');
			jQuery('#childnav li').removeClass('expanded');
			
			//Hightlight selected year in the side list
			jQuery(this).parent('li').parent('ul').parent('li').addClass('here');
			jQuery(this).parent('li').parent('ul').parent('li').addClass('expanded');
			
			//Highlight selected month in the side list
			jQuery(this).parent('li').addClass('here');
			
			//Get Archive Year
			var archiveYearStr = jQuery(this).parent('li').parent('ul').siblings('a').attr('id');
			var archiveYear = archiveYearStr.substr(5);
			
			//Get Archive Month
			var archiveMonthStr = jQuery(this).attr('id');
			var archiveMonth = archiveMonthStr.substr(16);
									
			//Add loader
			jQuery('#newsWrapper').block();
			
			//Hide previous news
			jQuery('#news').fadeOut('slow', function(){
			
				//Load articles based on year & month
				jQuery('#news').load('/releases/archive/', {year: archiveYearStr, month: archiveMonth, language: language }, function(){
					
					//Remove border on last news item
					jQuery('#news div:last-child').addClass('last');
					
					//Enable ajax news articles again
					ajaxNewsEntries();
					
					//Fade in news
					jQuery('#news').slideDown('slow', function(){
						
						//Remove loader
						jQuery('#newsWrapper').unblock();
					
					});
				
				});
			
			});					
		
		}
	
	});

	
	//Show all news for current year
	jQuery('a.viewall').click(function(){
	
		//Enable view all link
		ajaxAllEntries();
	
	});

});


//Load all entries based on year chosen
function ajaxAllEntries(){

	//Get Archive Year
	var archiveYearStr = jQuery('#childnav li.here a').attr('id');
	var archiveYear = archiveYearStr.substr(5);

	//Hide viewall link
	jQuery('a.viewall').fadeOut('slow');
	
	//Add loader
	jQuery('#newsWrapper').block();
	
	//Hide previous news
	jQuery('#news').fadeOut('slow', function(){
	
		//Load articles based on year
		jQuery('#news').load('/releases/archive/', {year: archiveYearStr, language: language }, function(){
			
			//Enable ajax news articles again
			ajaxNewsEntries();
			
			//Remove border on last news item
			jQuery('#news div:last-child').addClass('last');
			
			//Fade in news
			jQuery('#news').slideDown('slow', function(){
				
				//Remove loader
				jQuery('#newsWrapper').unblock();
			
			});
		
		});
	
	});

}


//Enable full posts to be shown on read more
function ajaxNewsEntries(){
	
	//Get Archive Year
	var archiveYearStr = jQuery('#childnav li.here a').attr('id');
	var archiveYear = archiveYearStr.substr(5);
	
	//Show full story on click and hide other posts
	jQuery('#news div.newsEntry a.readmore').click(function(){
		
		//Get Article ID
		var entryId = jQuery(this).parent('p').parent('div.content').parent('div.newsEntry').attr('id');
				
		//Check if there is only one article for the year
		if ((jQuery('#news div.newsEntry').size()) == "1"){
			
			//Load full story
			ajaxLoadArticle(entryId, archiveYear);
		
		}
		else {
			
			//Fade out all other news items to make room for full article
			jQuery("#news div.newsEntry:not(div#"+entryId+")").slideUp('slow', function(){
				
				//Load full story
				ajaxLoadArticle(entryId, archiveYear);
			
			});
		
		}
	
	});				

}			


//Load full article based on entryId
function ajaxLoadArticle(entryId, archiveYear) {

	//Remove border on the bottom
	jQuery('#'+entryId).addClass('fullstory');
	
	//Add loader
	jQuery('#newsWrapper').block();
			
	//Hide summary content
	jQuery('#'+entryId+' div.content').fadeOut('slow',function(){
			
		jQuery('a.viewall').attr('title',titleText);
		jQuery('a.viewall span').html(linkHtml);
									
		//Show back link
		jQuery('a.viewall').slideDown('fast');
		
		//Load full article
		jQuery('#'+entryId+' div.fullstory_content').load('/releases/fullstory/', {entry: entryId, language: language }, function(){
																	
			//Slide it into view
			jQuery('#'+entryId+' div.fullstory_content').slideDown('slow', function(){
				
				//Remove loader
				jQuery('#newsWrapper').unblock();
			
			});
		
		});				
	
	});

}