var isIE;
$(document).ready(function(){
	$("#url_param").val(document.referrer);
	//works out if we are using ie or not
	if (jQuery.support.scriptEval == true) {isIE=false}else{isIE=true};
	//highlights current top nav item with the relevant section
	var currpage =$("#bread li:eq(1)").attr('id');
	var currpagename = $("#bread li:eq(1)").text();
	if (currpage) {
		currpage = currpage.substring(2,currpage.length);
		$("#navbar a[href*='" + currpage + "']:not('.no_highlight')").addClass('activenav');
	}

	//if there is an h2 in the body content, add a link to top of page and build a table of contents
	if ($("#body_content h2").length > 0) {
		//set the header info for date and client name
		var disp_text;
		if ($("#item_client").length>0) {disp_text = "<p class='item_head'>" + $("#item_client").val() + "</p>"};
		if ($("#item_date").length>0) {disp_text = disp_text + "<p class='item_head'>" + $("#item_date").val() + "</p><p/>"};
		if ($(disp_text).length > 0) {$(disp_text).insertAfter($("#body_content h2:first"))};
		//now set the top link if it's a bulletin
		if ($("#mytype").val()=="2") {
		var counter = 0;
		var topstring = "<p><a class='top_link' href='#top'>Back to top</a></p>";
		$("#body_content h2.toc:first").append("<a name='link0'></a>");
		$("#body_content").append(topstring);
		//create the toc
		$("#body_content").prepend("<div id=\"toc\"><dl><dt>Table Of Contents</dt></dl></div>");
		$("#body_content h2.toc").each(function(){
			//$(this).attr('id','link'+counter);
			$(this).prepend("<a name='link" + counter + "'></a>");
			if (counter>0) {
			$(topstring).insertBefore($(this));
			}
			$("#toc dl").append("<dd><a href='#link" + counter + "'>" + $(this).text() + "</a></dd>");
			counter++;
		});
		}
	}
	
	//if there is an h3 in the body, then we can assume it is a "contact blah". We need to assign a link to this
	if ($("#body_content h3:last").length > 0) {
		//get the h3 text
		var cont_string = $("#body_content h3:last").text().toLowerCase();
		var author = false;
		//find the position of the string "CONTACT:"
		var s = cont_string.toLowerCase().indexOf("contact:");
		if (s==-1) {s = cont_string.toLowerCase().indexOf("author:");author = true};
		var intro_text;
		var mystring;
		var sor;
		var link_text;
		//if the string exists
		if (s>-1){
			//get the text after the word "CONTACT" or "AUTHOR"
			if (author) {mystring = cont_string.slice(s+7);}else{mystring = cont_string.slice(s+8);}
			//get the text before the word "CONTACT" or "AUTHOR"
			if (author) {intro_text = cont_string.slice(0,s+8);}else{intro_text = cont_string.slice(0,s+9);}
			//trim the name
			mystring = $.trim(mystring);
			var mylist = buildNameList(mystring, " or ");
			//build the replacement text string
			link_text = intro_text + mylist;
		}
		//if we returned a name, we need to do some replacing
		if (!(link_text=="")) {
			$("#body_content h3:last").html(link_text);
		}
	}
	//add a back to previous section link at the bottom of all content
	var backto =$("#bread li a:last");
	var backtolink = backto.attr('href');
	var backtotext = backto.text();
	if (!($("#id").val()==currpage)){$("#back_to_parent").html("<a href='" + backtolink + "'>&#62; Back to " + backtotext + "</a>")};
});//document.ready


function buildNameList(x, y){
	var myarray = new Array();
	var retstring = "";
	var substrings = x.split(y);
	var mynametrim = "";
	if (substrings.length-1>0) {
	for (z=0;z<=substrings.length-1;z++)
		{
			mynametrim = substrings[z].replace(" ","");
			mynametrim = mynametrim.replace("-","");
			mynametrim = mynametrim.replace("'","");
			mynametrim = mynametrim.replace(".","");
			myarray[z]="<a href='/a/" + mynametrim + "'>" + substrings[z] + "</a>";
			retstring = myarray.join(" OR ");
			
		}
		}
		else
		{
		mynametrim = x.replace(" ","");
		mynametrim = mynametrim.replace("-","");
		mynametrim = mynametrim.replace("'","");
		mynametrim = mynametrim.replace(".","");
		retstring="<a href='/a/" + mynametrim + "'>" + x + "</a>";
		}
	return retstring;
}

