//additional styling for scrolling sections

$(document).ready( function() {
	//scrolling sections for news templates						
	addScrollingToArticles();					
					
	//nav scrolling & fixes
	naveffects();
	styleNav();	
});


//checks if news lists and news articles need to be contained by scrolling boxes
//build the scrolling boxes if they do
function addScrollingToArticles(){
	$('#left_column .standard_box_white').each( function(){
		if ( $(this).height() > 360 ) {
			$(this).find('.article_list').wrap('<div class="article_list_outer"></div>');
			$(this).find('.article_list_outer').wrap('<div class="article_list_container"></div>');
			$(this).find('.article_list_container').append('<a href="#" class="arrow_up" onClick="scroll_up_news(this); return false;"><span>Up</span></a><a href="#" class="arrow_down" onClick="scroll_down_news(this); return false;"><span>Down</span></a>');
		}
	});
	$('#right_column .standard_box_white').each( function(){
		if ( $(this).height() > 560 ) {
			$(this).find('.article_list').wrap('<div class="article_list_outer"></div>');
			$(this).find('.article_list_outer').wrap('<div class="article_list_container"></div>');
			$(this).find('.article_list_container').append('<a href="#" class="arrow_up" onClick="scroll_up_news(this); return false;"><span>Up</span></a><a href="#" class="arrow_down" onClick="scroll_down_news(this); return false;"><span>Down</span></a>');
		}
	});
	$('.home-leader-content').each( function(){								   
		if ( $(this).height() > 225 ) {
			$(this).find('.article_list').wrap('<div class="article_list_outer"></div>');
			$(this).find('.article_list_outer').wrap('<div class="article_list_container"></div>');
			$(this).find('.article_list_container').append('<a href="#" class="arrow_up" onClick="scroll_up_news(this); return false;"><span>Up</span></a><a href="#" class="arrow_down" onClick="scroll_down_news(this); return false;"><span>Down</span></a>');
		}
	});
	$('.home-news-content').each( function(){								   
		if ( $(this).height() > 125 ) {
			$(this).find('.article_list').wrap('<div class="article_list_outer"></div>');
			$(this).find('.article_list_outer').wrap('<div class="article_list_container"></div>');
			$(this).find('.article_list_container').append('<a href="#" class="arrow_up" onClick="scroll_up_news(this); return false;"><span>Up</span></a><a href="#" class="arrow_down" onClick="scroll_down_news(this); return false;"><span>Down</span></a>');
		}
	});
	$('.office-news-content').each( function(){								   
		if ( $(this).height() > 100 ) {
			$(this).find('.article_list').wrap('<div class="article_list_outer"></div>');
			$(this).find('.article_list_outer').wrap('<div class="article_list_container"></div>');
			$(this).find('.article_list_container').append('<a href="#" class="arrow_up" onClick="scroll_up_news(this); return false;"><span>Up</span></a><a href="#" class="arrow_down" onClick="scroll_down_news(this); return false;"><span>Down</span></a>');
		}
	});
	$('.office-copy').each( function(){			
		var h = 357 - ($(this).find('.upper-text').height());
		if ( $(this).find('.lower-text').height() > h ) {		
			$(this).find('.article_list').wrap('<div class="article_list_outer"></div>');
			$(this).find('.article_list_outer').wrap('<div class="article_list_container"></div>').css({ height: (h - 18) + 'px' });
			$(this).find('.article_list_container').css({ height: (h - 18) + 'px' }).append('<a href="#" class="arrow_up" onClick="scroll_up_news(this); return false;"><span>Up</span></a><a href="#" class="arrow_down" onClick="scroll_down_news(this); return false;"><span>Down</span></a>');
		}
	});
	
}

//scroll news list or article down 
function scroll_down_news(e) {
	var innerHeight = $(e).parent().find('.article_list').height();
	var outerHeight = $(e).parent().find('.article_list_outer').height();
	var scrollOffset = $(e).parent().find('.article_list').css('marginTop').replace('px', '').replace('-', '');
	
	var newOffset = parseInt( scrollOffset ) + 60;
	
	if ( newOffset > (innerHeight-outerHeight) ) {
		newOffset = innerHeight-outerHeight;
	}
	
	$(e).parent().find('.article_list').stop().animate({ marginTop: "-" + newOffset + "px" }, 500 );
}

//scroll news list or article up 
function scroll_up_news(e) {
	var innerHeight = $(e).parent().find('.article_list').height();
	var outerHeight = $(e).parent().find('.article_list_outer').height();
	var scrollOffset = $(e).parent().find('.article_list').css('marginTop').replace('px', '').replace('-', '');
	
	var newOffset = parseInt( scrollOffset ) - 60;
	
	if ( newOffset < 0 ) {
		newOffset = 0;
	}
	
	$(e).parent().find('.article_list').stop().animate({ marginTop: "-" + newOffset + "px" }, 500 );
}

//checks if nav items need to be contained by scrolling boxes
//build the scrolling boxes if they do
function styleNav() {
	//match the heights of each nav level to that of level 1
	var navHeight = $('#navigation').height();
	
	//add scrolling to level 2
	$('.level_2_nav').each( function() {
									 
		var level2PaddingTop = $(this).css('paddingTop').replace('px', '');
		var level2PaddingBottom = $(this).css('paddingBottom').replace('px', '');
		var newHeight = navHeight - (parseInt(level2PaddingTop) + parseInt(level2PaddingBottom));
		
		if ( $(this).height() > newHeight ) {
			$(this).addClass('overflow_hidden').addClass('scroller');
			$(this).wrapInner('<div class="nav_list_inner"></div>');
			$(this).find('.nav_list_inner').wrap('<div class="nav_list_outer"></div>');
		    $(this).append('<a href="#" class="arrow_up" onClick="scroll_up_nav(this); return false;"><span>Up</span></a><a href="#" class="arrow_down" onClick="scroll_down_nav(this); return false;"><span>Down</span></a>');
		}
		
		$(this).css({ height: newHeight + 'px' });
		$(this).find('.nav_list_outer').css({ height: newHeight + 'px' });
	});
	
	//add scrolling to level 3
	$('.level_3_nav').each( function() {
		
		//briefly show secondary level to give tertiary level height(!)
		//$(this).parent().parent().show();
		$(this).parent().parent().css({ visibility: "hidden", display: "block" });
		var forcedHeight = $(this).height();

		//then remove inline style "display"
		var newStyle = $(this).parent().parent().attr('style').replace('display: block;', '');
		$(this).parent().parent().attr({ style: newStyle });
		
		var level3PaddingTop = $(this).css('paddingTop').replace('px', '');
		var level3PaddingBottom = $(this).css('paddingBottom').replace('px', '');
		var newHeight = navHeight - (parseInt(level3PaddingTop) + parseInt(level3PaddingBottom));
		
		if ( forcedHeight > newHeight ) {
			$(this).addClass('overflow_hidden').addClass('scroller');
			$(this).wrapInner('<div class="nav_list_inner"></div>');
			$(this).find('.nav_list_inner').wrap('<div class="nav_list_outer"></div>');
		    $(this).append('<a href="#" class="arrow_up" onClick="scroll_up_nav(this); return false;"><span>Up</span></a><a href="#" class="arrow_down" onClick="scroll_down_nav(this); return false;"><span>Down</span></a>');
		}
		
		$(this).css({ height: newHeight + 'px' });
		$(this).find('.nav_list_outer').css({ height: newHeight + 'px' });
	});
	
	
	//reset scrolling navs when first hovered
	$('#navigation .scroller').parent().hover( function() {
		$(this).find('.nav_list_inner').css({ marginTop: '0px' });
	});
}

//scroll nav items down 
function scroll_down_nav(e) {
	var innerHeight = $(e).parent().find('.nav_list_inner').height();
	var outerHeight = $(e).parent().find('.nav_list_outer').height();
	var scrollOffset = $(e).parent().find('.nav_list_inner').css('marginTop').replace('px', '').replace('-', '');
	
	var newOffset = parseInt( scrollOffset ) + 60;
	
	if ( newOffset > (innerHeight-outerHeight) ) {
		newOffset = innerHeight-outerHeight;
	}
	
	$(e).parent().find('.nav_list_inner').stop().animate({ marginTop: "-" + newOffset + "px" }, 500 );
}

//scroll nav items down 
function scroll_up_nav(e) {
	var innerHeight = $(e).parent().find('.nav_list_inner').height();
	var outerHeight = $(e).parent().find('.nav_list_outer').height();
	var scrollOffset = $(e).parent().find('.nav_list_inner').css('marginTop').replace('px', '').replace('-', '');
	
	var newOffset = parseInt( scrollOffset ) - 60;
	
	if ( newOffset < 0 ) {
		newOffset = 0;
	}
	
	$(e).parent().find('.nav_list_inner').stop().animate({ marginTop: "-" + newOffset + "px" }, 500 );
}


function naveffects(){
	$(" .level_1_nav ul ").css({display: "none"}); // Opera Fix
	$(" .level_1_nav li").hover( function(){
		$(this).find('ul:first:hidden').css({visibility: "visible",display: "none"}).show(300);
		},function(){
		$(this).find('ul:first').css({visibility: "hidden"});
	});
}
