
(function($){
$(document).ready(function() {
	//remove summary box if there is no text
	if($('.summaryBox').text().length<1){
		$('.summaryBox').remove();
	}
	
	
	/******Accessibility Set up starts*****/					   
 	//skip to content animation
	$('.skipContent a, .skipTopNav a, .skipSideNav a').css('top', -32);
	$('.skipContent a, .skipTopNav a, .skipSideNav a').focus(function(){
		$(this).animate({top:0});
	});
	$('.skipContent a, .skipTopNav a, .skipSideNav a').blur(function(){
		$(this).animate({top:-32});
	});
	
	//Top Nav
	$('#topNav li a').not('ul ul li a').focus(function(){
		$('#topNav li a').attr('style' ,'');
		$('#topNav ul ul').stop(true, true).fadeOut();
		
		$(this).parent().mouseenter();
	});

	$('#topNav ul ul li a').focus(function(){
		$(this).parents().eq(2).find('a').eq(0).css({background: 'url(/internet/otd/publishing.nsf/AttachmentsByTitle/top_nav_bg_active.png/$FILE/top_nav_bg_active.png) no-repeat center -31px #b9b9b9'});
	});


$('#topNav ul ul a').last().blur(function(){										   
	$('#topNav ul ul').stop(true, true).fadeOut();
});
 	
	
	/******Top Nav Dropdown*****/
	$('#topNav li').mouseenter(function(){
		//apply this inline css to override style in css file, to make fadeout working.
		$(this).find('ul').css({left: 0})
		
		//make sure subNav's position won't exceed wrapper's width
		var navPos = $(this).position().left;
		var navWidth = $(this).outerWidth();
		var subNavWidth = $(this).find('ul').outerWidth();
		var wrapperWidth = $('#wrapper').width();
		if(navPos + subNavWidth > wrapperWidth)
		{
			var calExceeded = (0 - subNavWidth) + navWidth;
			$(this).find('ul').css({left: calExceeded})
		}
		
		$(this).find('ul').stop(true, true).hide().fadeIn();	
	});
	
	$('#topNav li').mouseleave(function(){
		$(this).find('ul').stop(true, true).fadeOut();
	});
	
	
	
	/******SearchBox default value*****/
	$('#searchBox input').focus(function(){
		if($(this).val()=='Search'){
			$(this).val('');
		}
	});
	$('#searchBox input').blur(function(){
		if($(this).val()==''){
			$(this).val('Search');
		}
	});
	$('#goBtn').val('');
	
	/******Tabbing Content*****/
	$('.tab_content').hide(); //Hide all content
	$('ul.tabs li:first').addClass('active').show(); //Activate first tab
	$('.tab_content:first').show(); //Show first tab content
	
	//On Click Event
	$('ul.tabs li').click(function() {
		$('ul.tabs li').removeClass('active'); //Remove any 'active' class
		$(this).addClass('active'); //Add 'active' class to selected tab
		$('.tab_content').hide(); //Hide all tab content
		var activeTab = $(this).find('a').attr('href'); //Find the rel attribute value to identify the active tab + content
		$(activeTab).stop(true, true).fadeIn(); //Fade in the active content
		return false;
	});
	
	/*****accordion************/
	$('.accordionContent').hide();
	$('.accordionHeader').find('br, p').remove();
	$('.accordionHeader').append('<div class="indicator">'+ unescape("show %u2193")+'</div>');
	
	
	//open , close accordion
	$('.accordionHeader').click(function(){		
		$(this).nextAll('.accordionContent').eq(0).slideToggle();
		$(this).children('.indicator').text($(this).children('.indicator').text() == unescape("hide %u2191") ? unescape("show %u2193") : unescape("hide %u2191"));
		$(this).toggleClass('show');
		return false;	
	});
	
	//remove br tags
	$('.accordionHeader').eq(0).trigger('click');
 	function getAllBrTagsBetween(fistEl, lastEl){
		var firstEl = $(fistEl);
		var lastEl = $(lastEl);
		firstEl.nextAll('br, p').each(function(){
			var siblingID = $(this).attr('class');
			var checkIndexBeforeBraek = $(this).prev().index();
			var lastContentIndex = $('.accordionContent').last().index();
			if((siblingID != lastEl.attr('class'))&&(checkIndexBeforeBraek != lastContentIndex) ){
				$(this).remove();
			}else{
				return false;
			}				
		});
	}
	getAllBrTagsBetween('.accordionHeader', '.accordionContent');

	//show all button for h2 accordion headers
	if($('.accordionHeader').length > 1){
		$('#mainContent').append('<a href="#" class="showHeaders"><div>'+ unescape("%u25BC show all topics")+'</div></a>');
		
			$('.showHeaders').click(function(){
				$(this).text($(this).text() == unescape("%u25B2 hide all topics") ? 
											   unescape("%u25BC show all topics") : unescape("%u25B2 hide all topics"));
				$(this).toggleClass('hideHeaders');
				
				if($(this).text().indexOf('hide')>0){
					$('.accordionHeader').find(':contains(show)').parent().trigger('click');
				}
				else {
					$('.accordionHeader').find(':contains(hide)').parent().trigger('click');
				}
			});
	}


// add home button to left nav
 $('#leftNav ul').eq(0).prepend('<li><a href="home">Home</a></li>');
 
 
  //table style
 if($('td strong').length >0 ){
	 	 $('table td strong').parent().css('background', '#eccbb0');
 }
 
 
 //locator
 $('#map').append('<a href="#" class="mapMenu"><div>'+ unescape("%u25B2 hide menu")+'</div></a>');
 $('.mapMenu').click(function(){
	$('#map-search').stop(true, true).toggle('normal');
	$(this).text($(this).text() == unescape("%u25C4 show menu") ? unescape("%u25B2 hide menu") : unescape("%u25C4 show menu"));
	return false;
 });
 
});

})(jQuery);
