
window.onload = function(){
	$('a[href^="http://"]:not(a[href^="http://www.twmuseums.org.uk/slavery"])')
		.attr({
			target: "_blank"
		});

	var expandDiv = document.getElementById("divExpandDiv");
	if(expandDiv){
		thisList = expandDiv.getElementsByTagName("ul")[0];
		$(thisList).hide();
		expandDiv.getElementsByTagName("a")[0].onclick = function(){
			$(thisList).slideToggle(1000);
			return false;
		};
	}

	var thumbList = document.getElementById("thumbList");
	if(thumbList){
		thumbList = $(thumbList);
		var listElements = $("#thumbList li");
		var totalWidth = 0;
		for(var i = 0; listElements.length > i; i++){
			$(listElements[i]).find("a").click(
				function(){
					$("#viewerLarge a").attr("href", $(this).attr("href").replace("large", "original"));
					$("#viewerLarge a").attr("title", $(this).attr("title"));
					$("#viewerLarge .hidden-description").html($(this.parentNode).find(".hidden-description").html());
					$("#viewerLarge img").attr("src", $(this).attr("href"));
					$("#thumbTitle").html($(this).attr("title"));
					return false;
				}
			);
			totalWidth = (totalWidth + listElements[i].offsetWidth)+10;
		}

		$("#thumbList").css("width", totalWidth);

		var marginLeft = thumbList.css("margin-left");
		marginLeft = marginLeft.substr(0, marginLeft.length - 2);
		
		var slideWidth = parseInt($("#viewerThumbs").css("width").replace("px", ""));
		var slideJump = 50;
		
		document.getElementById("controlLeft").onclick = function(){
			var marginLeft = ($(thumbList).css("margin-left").replace("px", ""));

			if((marginLeft + slideJump) < 0){
				$("#thumbList").animate({
					marginLeft: "+="+slideJump+"px"
				}, 300 );
			}else{
				$("#thumbList").animate({
					marginLeft: "0px"
				}, 300 );
			}
			return false;
		};

		document.getElementById("controlRight").onclick = function(){
			var marginLeft = parseInt( ($(thumbList).css("margin-left").replace("px", "")) );
			marginLeft = (marginLeft - marginLeft - marginLeft) + slideWidth;
			marginLeft = parseInt(marginLeft);
			if((marginLeft + slideJump) < totalWidth){
				thumbList.animate({
					marginLeft: "-="+slideJump+"px"	
				}, 300 );
			}else{
				var finalWidth = (totalWidth - slideWidth);
				finalWidth = (finalWidth - finalWidth - finalWidth) + 10;
				thumbList.animate({ 
					marginLeft: finalWidth+"px"
				}, 300 );
			}
			return false;
		};
	}
}

