$(document).ready(function(){
	$(function(){
	// ---- NAVIGATION ---- //
		if(location.host.match(/www\.leeds\.ac\.uk/)){
			var path = location.pathname.substring(1).replace(/(?:\/)index.html?$/,""); //strip index.html off end of page location - nav doesn't have it (there are some pages called e.g. strands_index.htm!!!)
			var folders = path.split("/");
			if ( path ) //should always be true - as we're in /ims/ need to check functionality with TS (non-www.leeds.ac.uk stuff).
				$('#column_nav a[href$="' + path + '"]').attr('class', 'current').parents('li').attr('class','active');
	
			if($('#column_nav .current').length == 0 ){
			//we're in a page not represented in the main navigation. Open a the parental sub-menu.
				$('#column_nav a[href$="' + folders[0] + '/' + folders[1] + '/"]').parents('li').attr('class','active');
			}

			var breadcrumbUrl = "/";
			for(i=0; i<folders.length; i++){
				if(folders[i].indexOf(".") == -1){ //don't want to include the filename - this will be added from the H1
					breadcrumbUrl += folders[i] + "/";
					breaccrumbText = ""; 
					switch(folders[i]){
						case "ims": breadcrumbText = "Institute for Medieval Studies";
							break;
//						case "ims-dev": breadcrumbText = "Institute for Medieval Studies";
//							break;
						case "imb": breadcrumbText = "International Medieval Bibliography";
							break;
						case "imc": breadcrumbText = "International Medieval Congress";
							break;
	//					case "study": breadcrumbText = "Study";
	//						break;
						case "med_online": breadcrumbText = "Medieval Studies Online";
							break;
	//					case "events": breadcrumbText = "Events";
	//						break;
	//					case "about": breadcrumbText = "About";
	//						break;
						case "fieldsofconflict": breadcrumbText = "Fields of Conflict";
							break;
	//					case "strands": breadcrumbText = "Strands";
	//						break;
	//					case "fieldsofconflict": breadcrumbText = "Fields of Conflict";
	//						break;
						default:
							breadcrumbText = folders[i].slice(0,1).toUpperCase() + folders[i].slice(1);
					}
					$('#breadcrumb ul').append('<li><a href="' + breadcrumbUrl + '">'+breadcrumbText+'</a></li>');
				} else {
					$('#breadcrumb ul').append('<li> </li>'); //placeholder for page title
				}
			}
			$('#breadcrumb ul li:last').text( $('h1').text() );
		} else {
			$('#column_nav a[href$="/ims/imc/"]').parents('li').attr('class','active');	
		}
	});
	$('#column_nav>ul').children('li:not(.active)').hoverIntent(navShow,navHide).children('ul').hide();
	function navShow(){
		$(this).children('ul').show();
	}
	function navHide(){
		$(this).children('ul').hide();
	}

//sort out ie7 images
	$('#column-center img[align="right"]').css('float','right');
	$('#column-center img[align="left"]').css('float','left');

	if(!($('#column-right').text().replace(/\s*/g,"").length || $('#column-right img').length)){
		//nothing in the right-column.
		$('#column-right').remove();
		$('#column-center').css('width','98%');
	}
	var topofpage = '<div class="topofpage"><a href="#top">[Top of page]</a></div>';
	$('#content table').after(topofpage);
	//same for h2's? - not the first one and not if there's one there already
	$('h2:not(:first)').each(function(){
		if($(this).prev().attr("class") != "topofpage"){
			$(this).before(topofpage);
		}
	});
});
