// JavaScript Document

// Display new window image after links that will open in a new window
function nwLinkImg() {
	$('a[target="_blank"]').append('<div class="new_win_link" title="Link will be opened in New Window.">&nbsp;</div>')
}

/*function ddNavs() {
	var overClass="over";
//	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
//	if (badBrowser) {
//		overClass="ieOver";
//	}	
	$('div#top ul#nav li').hover(function(){ //mouse over f(x)
		$(this).addClass(overClass);
	},
	function() { //mouse out f(x)
		$(this).removeClass(overClass);
	});
}*/

// apply margins to images that are aligned right or left
function imgMarg() {
	$('img[align="right"]').addClass('right');
	$('img[align="left"]').addClass('left');
}


// actions for the dd navs
var cssTarget="ul.dd";
var closedParent="closedParent";
var openParent="openParent";
var selectedClass="selected";
var effects="on";
var startOpen="no";

$(function() {	
	if (startOpen=="yes") {
		$(cssTarget).find('li ul').parent().children('a').addClass(openParent);
	} else {
		$(cssTarget).find('li ul').css('display','none');
		$(cssTarget).find('li ul').parent().children('a').addClass(closedParent).attr('href','javascript:void(0)');
	}
	$(cssTarget).find('li ul').parent().find('a:eq(0)').click(function(){ 
			$(this).parent().children('a').toggleClass(closedParent);
			$(this).parent().children('a').toggleClass(openParent);
		if (effects=="on") {
			$(this).nextAll('ul').slideToggle('fast');
		} else {
			$(this).nextAll('ul').toggle();
		}
	});
	var currentUrl=window.location.href;
	$(cssTarget).find('a').each(function(){
		var thisHref=$(this).attr('href');
		if (currentUrl!="#" && currentUrl.indexOf(thisHref)>=0) {
			$(this).addClass(selectedClass);
			$(this).append(' &raquo;');
			$(this).parents('ul').show();
			$(this).parents('li').find('a.'+closedParent).removeClass(closedParent).addClass(openParent);
		}
	});
});

// add selected style for non-dd navs
$(function(){
	var currentUrl=window.location.href;
	$('ul.sidenav').not('ul.dd').find('a').each(function(){
		var thisHref=$(this).attr('href');
		if (currentUrl!="#" && currentUrl.indexOf(thisHref)>=0) {
			$(this).addClass(selectedClass);
			$(this).append(' &raquo;');
		}
	});
});


function autotocInit() {
	if ($('div#autoTOC').length>0) {
		$('div#autoTOC').addClass('toc').html('<h4><span class="hoverOnly">show/hide</span>Table of Contents</h4><ul id="tocList"></ul>');
		$('div#autoTOC ul#tocList').hide();
		$('div#autoTOC h4').click(function(){
			if ($(this).siblings('ul#tocList').css('display')=='block') {
				$(this).siblings('ul#tocList').slideUp();
				$.cookie('shsi_toc_visibility', '0', {expires: 7});
			} else if ($(this).siblings('ul#tocList').css('display')=='none') {
				$(this).siblings('ul#tocList').slideDown();
				$.cookie('shsi_toc_visibility', '1', {expires: 7});
			}
		});
		//$('div#autoTOC h4').hover(function(){$(this).find('span.hoverOnly').show();},function(){$(this).find('span.hoverOnly').hide();});
		$('div.body_copy h3').slice(1).before('<p class="noprint"><a href="#top">Top</a></p>');
		$('div.body_copy h3').each(function(index){
			$(this).before('<a name="a_'+index+'"></a>');
			var sectionTitle=$(this).text();
			$('ul#tocList').append('<li><a href="#a_'+index+'">'+sectionTitle+'</a></li>');
		});
	}
	if ($.cookie('shsi_toc_visibility')=='0') {
		$('div#autoTOC ul#tocList').css('display','none');	
	}
}

function widont() { //kill those widows from the h2s (the typographical ones)
	$('h2').each(function(){
		var text=$(this).text();
		if (text.lastIndexOf(' ')>0) {
			var lastSpaceIndex=text.lastIndexOf(' ');
			var firstChunk=text.substring(0,lastSpaceIndex);
			var lastChunk=text.substring((lastSpaceIndex+1),text.length);
			var outputString=firstChunk+"&nbsp;"+lastChunk;
			$(this).html(outputString);
		}
	});
}

// The f(x)s will be called upon page loading
$(function(){
	nwLinkImg();
	//ddNavs();
	imgMarg();
	autotocInit();
	widont();
});