// news feederer
$(document).ready(function(){
	$('.newsfeed').each(function(i){
		buildFeed(i);	
	});
});
function buildFeed(feedNumber){
	// add a div for the news after the link and replace link with an h4
	var url=$('.newsfeed').eq(0).attr('href');
	var feedName=$('.newsfeed').eq(0).text();
	$('.newsfeed').eq(0).after('<div class="news'+feedNumber+'"></div>').replaceWith("<h3>"+feedName+"<span class='smallerer'> &ndash; provided by </span> <a href='/pctoday/'><img style='vertical-align: bottom' src='http://www.pct.edu/cgi-bin/images/pctoday.jpg'/></a></h3>");
	// grab news div and load the category index page's summary divs from PCT o'day
	
	$('.news'+feedNumber+'').load(url+' .summaryContainer',function(){
		i=0;
		$(".summaryText").hide();
		$('.summaryContainer').each(function(i){
			 //alert(i+' of '+$('.summaryContainer').length)
			 if(i>5){return false}else{
			//remove inline styles and html 'align' attr
			// nested spans occur when more than one image is used. 
			$('.summaryText span,.summaryText span span,.summaryText span span span').removeAttr('style').removeAttr('align');
			// if there is one image attached...
			var title=$(this).children('h4').text();
			var boxTag="<a id='newsStory"+i+"'>" + title + "</a>";
			// if there is an article page for this summary add the read more link
			if($(this).children('h4').children('a').length == '1'){
				var articleLink=$(this).children('h4').children('a').attr('href');
				$(this).append('<a class="articleLink" href="'+articleLink+'">Read more...</a>');
			}
			$(".articleLink").hide();
			$('.summaryText img').addClass('rfloat')
			$('.summaryText a img').addClass('rfloat').parent('a').attr({rel:'box'});
			var height=$(this).height();
			$(this).children('h4').replaceWith("<h5 class='showNews' h="+height+">"+boxTag+"</h5>");
			$(this).children('.summaryDate').replaceWith("<p class='smaller summaryDate'>Published on " + $(this).children('.summaryDate').text() + "</p>");
			 }
			 $('.news'+feedNumber+' .summaryContainer:nth-child(5)').nextAll().remove();
			 
		});
		$('.news'+feedNumber).css({'overflow':'auto'}).append('<p><a class="moreNews" href="'+url+'">More '+feedName+'...</a></p>');
		// style elements
		$('.summaryContainer').hover(
			function(){
				$(this).css({'backgroundColor':'#efe'});
			},
			function(){
				$(this).css({'backgroundColor':'#eee'});
				$('.summaryContainer:even').css({
					'background':'#fff'
				});
		});
		$('.showNews').css({
			'font-weight':'bold',
			'color':'#666',
			'cursor':'pointer',
			'padding':'5px 25px 0 35px'
			});
		$('.summaryText img').not('.rfloat').css({
			'margin':'0 5px 5px 0',
			'float':'right'
			});
		$('.summaryText img:last').css({
			'paddingLeft':'10px'
			});
		$('.articleLink,.moreNews').css({
			'margin':'0 45px 20px 45px',
			'padding':'10px',
			'backgroundColor':'#fff',
			'border':'1px solid #ddd',
			'clear':'both',
			'float':'left'
			});
		
		$('.summaryDate').css({
			 'padding':'0 0 5px 45px',
			 'margin':'0','color':'#aaa'
		});
		$('.summaryContainer').css({
			'overflow':'auto'
			});
		$('.moreNews').css({
			'padding':'5px 10px',
			'margin':'10px 0 5px -15px',
			'float':'right',
			'background':'#eee'
			});
		$('.summaryContainer:odd').css({
			'background':'#eee'
		});
		$('h3 .smallerer').css({
			'fontSize':'11px'
		});
		$('h4 span').css({
			'font-size':'11px',
			'font-weight':'normal'
		})
		i=i+1;
		$(".summaryText a[rel^='box'], .boxgallery a").each( function (){initbox(this);});
		$('.news'+feedNumber+' .showNews').click(function(){
			$(this).siblings('p.summaryText, .articleLink').slideToggle();
														 
		});
	});
}

