$(function(){

$("img").each(function(){
	if($(this).attr("alt")=="Cfbt")
	{
/*
		var imgLink = $(this).parent().html();
		$(this).after("<a href=\"http://www.cfbt.com\" target=\"_blank\">"+imgLink+"</a>");
		$(this).remove();
*/
		$(this).wrap('<a href=\"http://www.cfbt.com\" target=\"_blank\"></a>');
		return false;
	}
});

	
	/*	add in bits of html that are added in for design purposes e.g. curves on panels etc
		keeps it cleaner for accessibility / screenreaders / search engines
	*/

	
	/*
		add footer graphic in to the bottom of any panels
	*/
	$("#content .panel").append("<div class=\"panel-footer\">&nbsp;</div>");

	/*
		add in divider lines between the navigation options
	*/
	$("#navigation li:not(:last-child)").after("<li class=\"divider\">&nbsp;</li>");

	/*
		if you click on the search box, remove text
	*/
	$("#navigation #search input[type='text']").focus(function(){$(this).val("");});

	/*
		add hover classes and curves in to the main navigation
	*/
	$("#navigation ul li:not(.divider)").mouseover(
		function(){
			$(this).addClass("hover");
		}
	).mouseout(
		function(){
			$(this).removeClass("hover");
		}
	).append("<span class=\"leftcurve\">&nbsp;</span><span class=\"rightcurve\">&nbsp;</span>");

	
	/*	
		sub sub nav has its own style, 
		so remove the bottom border of the parent
	*/
	$('#sub-navigation ul li').each(
		function(){
			if($(this).children().size() > 1)
			{//we don't want <a> tags, we want child lists<ul> <lidd>
				$(this).css({"border-bottom":"0"});
			}
		}
	);

	$('#sub-navigation .selected').each(
		function(){
			$(this).find("a").first().css({"text-decoration":"underline","font-weight":"bold"});
		});

	//$('#form-search').click(function() {$('#form-submit').submit();});
	
	$("#sub-navigation li[id]").each(function(){

		//highlight the sub nav item
		var idstring =	$(this).attr("id");
		var _pos = idstring.lastIndexOf("_") + 1;
		var parentid = idstring.substring(_pos, idstring.length);
		$("li[id*='n_"+parentid+"_']").addClass("child");

		//get the selected items parent (if exists)
		var parent = $("li[id$='_"+parentid+"']");
		if(parent != null)
			//alert(parent.text());
		
		//highlight the top bar item
		_ppos = idstring.indexOf("_");
		var pageid = idstring.substring(_ppos+1,_pos-1);

		$("#navigation #p_"+pageid+"").addClass("selected");
	});

/*
	var o = $("#sub-navigation .selected");
	if(o != null)
	{
		var idstring = o.attr("id");
if(idstring!=undefined)
{
		var _pos = idstring.lastIndexOf("_") + 1;
		var parentid = idstring.substring(_pos, idstring.length);
		var pageid = idstring.substring(_ppos+1,_pos-1);
		
		var txt = $("#sub-navigation li[id$='_"+pageid+"']").text();
		txt = (txt.length==0)?"&nbsp;":txt;
		$("#sub-navigation .title").html(txt);
}
	}
*/

//set a heading in the subnav box - if empty put a space in to keep the height
var txt = $("#navigation .selected").text();
txt = (txt.length==0) ? "Oakfield Prep" : txt;
$("#sub-navigation .title").html(txt);


	//alternate bg color on tables
	$("table tr:nth-child(odd)").css({"background-color":"#ececec"});

	//remove any word formatting
	$("table p").each(function(){
		var html = $(this).html();
		$(this).after(html);
		$(this).remove();
	});

	//style alternating items in the search results
	$("#search-holder div:nth-child(even)").slice(2).css({"background-color":"#ececec","padding":"10px"});


	//footer links are adding in some unwanted code, so lets sort it here for now.
	$("a").each(function(){
		var href = $(this).attr("href");
		var findstr = "system/footer/";
		var pos = href.indexOf(findstr);
if(pos != -1)
{
		href = href.substring(pos+findstr.length,href.length);
		//$(this).attr("href","\""+href+"\"");
}
	});
	accessibleTitles("h1");
	accessibleTitles("h2");
	accessibleTitles("h3");
	accessibleTitles("h4");
	accessibleTitles("h5");
	accessibleTitles("h6");
});

function accessibleTitles(tag)
{//sort the headings out
	$("."+tag+"").each(
	function(){
		$(this).after("<"+tag+" class=\""+tag+"\">"+$(this).html()+"</"+tag+">");
		$(this).remove();
	});
}
