$(function(){

	$('body').addClass('jscript').removeClass('noscript');

	
	
	$('.article .node img').hover(function(){
		
		$(this).closest('.article').find('h1 a, h2 a').addClass('hover');
		
		}, function(){
			
			$(this).closest('.article').find('h1 a, h2 a').removeClass('hover');
		
			})//#index .article .node img HOVER
	
	.each(function(){
		
		var $img = $(this);
		
		if ( $.browser.webkit ) {
			
			
			
			$img.load(function(){//webkit browsers need to load images first before they can get width and height

				imgWide($img);
				
				});
				
		} else {
				
				imgWide($img);
		
		}
		
		});//#index .article .node img EACH
	
	
	function imgWide($img) {
		
		var $node = $img.closest('.node'),
		    imgH  = $img.height(),
		    nodeH = $node.height();
		
		if ( nodeH > imgH ) { $img.addClass('wide') }
		
		}
	
	
	function setCookie(name, value, expires) {	

document.cookie = name + "=" + escape(value) + "; path=/" + ( (expires == null) ? "" : "; expires=" + expires.toGMTString() ); 

}//FN setCookie 

var exp = new Date();//set new date object	

exp.setTime( exp.getTime() + ( 1000 * 60 * 60 * 24 * 30 ) );     //set it 30 days ahead 
	
	

function getCookie (name) {	

    var dc = document.cookie;	
    var cname = name + "=";	

    if (dc.length > 0) {	
      
	  begin = dc.indexOf(cname); 
      
	  if (begin != -1) { 
       
	    begin += cname.length; 
        end = dc.indexOf(";", begin);
        if (end == -1) end = dc.length;
        return unescape(dc.substring(begin, end));
       
	    } 

      }	

    return null;	
}
	
	
	
	if ( getCookie('setView') == 'grid') { $('body').removeClass('rows').addClass('grid') }
	
	
		
		$('.toolGrid, .toolList').click(function(){
		
		if ( $('.article').is(':animated') ) { return false; }
		
		$('.row .article').css('opacity',0);
		
		if ( $('body').hasClass('grid') )
		
		 { $('body').removeClass('grid').addClass('rows'); setCookie('setView','rows',exp); }
		 
		 else { $('body').removeClass('rows').addClass('grid');  setCookie('setView','grid',exp); }
		
		setArticles();
		
		return false;
		
		});//.toolGrid CLICK
	
	function setArticles() {
		
		var timer = 0,
		    delay = 100;
		
		$('.article').each(function(){
			
			$(this).delay(timer).animate({'opacity':1},'fast');
		    
			timer += delay;
		    
			});//.article EACH	
		
		}
	
	setArticles();
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	//change font size
	
	var fontDefault = parseInt( $('body').css('font-size') ),
	       fontSize = parseInt( getCookie('setFont') );
	
	if ( isNaN(fontSize) ) { fontSize = fontDefault; } else { $('#content').css('font-size',fontSize)  }
	
	
	$('.sizeDefault').click(function(){
		
		$('#content').css('font-size',fontDefault);
		
		fontSize = fontDefault;
		
		setCookie('setFont', fontSize, exp);
		
		return false;
		
		});
	
	$('.sizeMinus').click(function(){
		
		fontSize -= 2;
		
		if ( fontSize < 10 ) { fontSize = 10 }
		
		$('#content').css('font-size',fontSize);
		
		setCookie('setFont', fontSize, exp);
		
		return false;
		
		});
	
	$('.sizePlus').click(function(){
		
		fontSize += 2;
		
		if ( fontSize > 22 ) { fontSize = 22 }
		
		$('#content').css('font-size',fontSize);
		
		setCookie('setFont', fontSize, exp);
		
		return false;
		
		});
	
	$('.toolPrint').click(function(){
		
		window.print();
		
		});
	
	//change font size
	
	
	
	
	
	//strip duplicate article
	
	var mainTitle = $('#main h1').text();
	
	$('#list-articles h1').each(function(){
		
		var subTitle = $(this).text();
		
		if ( subTitle == mainTitle ) { $(this).closest('.article').before('<span id="placeHolder"/>').remove(); $('#placeHolder').closest('#list-articles').find('.row:last .article:last-child').insertAfter('#placeHolder').prev().remove();  }
				
		});//#list-articles h1 EACH
	
	//strip duplicate article

	
	
	
	
	
	
	//fixes carried over from previous html
	
	if ( $('h2:contains("Latest Articles")') ) { 
	
	var $ul = $('.more-articles:first');
	
	$('h2:contains("Latest Articles")').next().insertBefore($ul).wrapAll('<div class="more-articles rad panel"><section></section></div>').before('<h3 class="radTL radTR">Latest Articles</h3>');
	
	$('h2:contains("Latest Articles")').remove();
	
	 }//IF h2:contains...
	
	
	
	
	if ( $('.snippets').length > 0 && $('.box-4 ul li').length > 0 ) { 
	
	$('.box-4').find('ul').appendTo('#sidebar').wrapAll('<div class="more-articles rad panel"><section></section></div>').before('<h3 class="radTL radTR">Other stories</h3>');
	
	$('h2:contains("Latest Articles")').remove();
	
	 }//IF .snippets LENGTH > 0
	
	
	
	
	$('.more-articles li, .nav li').each(function(){
		
		var $ul = $(this).closest('ul');
		
		if ( $(this).find('a').text().length === 0 ) { $(this).closest('li').remove(); }
		
		if ( $ul.children().length === 0 ) { $ul.closest('.more-articles').remove(); }
		
		});//.more-articles li EACH
		
		
	
	if ( $('.snippets').length > 0 ) {
		
		$('.snippets .article').each(function(){
			
			var snippetText = $(this).find('h1').text();
			
			$('#list-articles .article').each(function(){
				
				var listText = $(this).find('h2').text();
				
				if ( snippetText == listText ) { $(this).remove(); }
				
				});//#list-articles .article EACH
			
			});//.snippets .article EACH
		
		}//IF .snippets LENGTH > 0
		
	

	
	$('p:not(".node")').each(function(){
		
		var text = $(this).html().replace(/�/gi,'');
		
		if ( text == '' ) { $(this).remove(); } else { $(this).html(text); }

		});//p EACH
	
	//fixes carried over from previous html
	

	$('html').removeAttr('class');
	
	
	//sticky toolbar
	
	
	
	var      $toolbar = $('#toolbar ul'),
	         toolbarY = $toolbar.offset().top;
	
	$(window).scroll(function(){
		
		var windowY = $(window).scrollTop();
		
		if ( windowY > toolbarY ) { 
		
		if ( navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod' )
{
     $toolbar.css('position', 'static');
} else {
		
		$toolbar.addClass('fixed') } } else { $toolbar.removeClass('fixed') }
		
		});//sticky toolbar
	
	});//DOCUMENT.READY
