// JavaScript Document
$(document).ready(function(){
	// clone the lhnav to the top of the page.
	// if we're on a page that has an a-z, put it into two columns.
	switch (document.getElementsByTagName("body")[0].id){
		case 'notes':
		case 'locations':
		case 'downloads':
			var height_to_aim_for = $("#list").height() / 2;
			var _list = $('<ol id="left_list" class="column c05" />');
			
			$("#list").before(_list);
			$("#list, #left_list").css("float", "left");
			while(_list.height() < height_to_aim_for){
				$("#list li:first").appendTo(_list);
			}
			duplicateNavigation();
			break;
		case 'offthebeatentrack':
			break;
		default:
			// do nothing
			duplicateNavigation();
			break;
	}
	// add the open close action to subnav in the knowledge section
	//$(".knowledge_subnav_inner li h3").append('&nbsp;<a class="toggle" href="#">+</a>');
	$(".knowledge_subnav_inner li h3 a").click(function(e){
		e.preventDefault();
		$(this).blur();
		timer = 160;
		item_to_toggle = $(this).parent().next();
		children = item_to_toggle.children().length;
		timer = timer * (children + 2);
		if($(this).hasClass("open")){
			//$(this).text("+");
			$(this).removeClass("open");
			$(this).parent().next().slideUp(timer);
		} else {
			//$(this).text("-");
			$(this).addClass("open");
			$(this).parent().next().slideDown(timer);
		}
	});
	cl_arr = $("body").attr("class");
	cl = "";
	if(cl_arr){
		cl_arr = cl_arr.split(" ");
		cl =cl_arr[0];
	}
	$(".knowledge_subnav_inner li").each(function(){
		if($(this).hasClass(cl)){
			$(this).find("h3 a").click();
		} else {
			$(this).find("ul").hide();
		}
	});
	
	
	
	/* Geological Timescales */
	/*
	ht = $(".geological_periods dd:last").height();

	$(".geological_periods dd:visible:gt(0)").hide();
	$(".geological_periods dt:first").addClass("active");
	$(".geological_periods dt a").click(function(e){
		e.preventDefault();
		th = $(this);
		$(".active").removeClass("active");
		$("dd:visible").fadeOut(function(){
			th.parent().addClass("active").next().fadeIn();
		});
	});

	$("body").addClass("has_js");
	ht = ($(".geological_periods dl").height() + ht);
	$(".geological_periods dl").height(ht);
	*/
	/* toggle sliding of long content */
	$(".c07 .inner div.subsection").hide();
	$("h3.subsectionheader").after('<a class="js-show-hidden-content" href="#">Show Content</a>');
	$("a.js-show-hidden-content").click(function(e){
		e.preventDefault();
		$(this).next().slideToggle(2000);
		if($(this).text() == "Show Content"){
			$(this).text("Hide Content");
		} else {
			$(this).text("Show Content");
		}
	});
	$("a.js-show-hidden-content:first").click();
	
	if($("#image_holder").length > 0){
		thumbs_height = $(".thumbs").height();
		$(".thumbs").css({"opacity": "0.6", "position": "absolute"}).hide();
		$(".browser").empty().append('<a href="#">Show Thumbnails</a>');
		
		$("#image_holder").append('<p id="g_next"><span></span><a href="#">Next &raquo;</a></p>');
		$("#image_holder").append('<p id="g_prev"><span></span><a href="#">&laquo; Prev</a></p>');

		$(".browser a").click(function(e){
			e.preventDefault();
			$(this).blur();
			t = $(this).text();
			if(t == "Show Thumbnails"){
				$(this).text("Hide Thumbnails");
				$(".thumbs").slideDown(500);
			} else {
				$(this).text("Show Thumbnails");
				$(".thumbs").slideUp(500);
			}
		});
			
		$("#image_holder").hover(function(){
			//$(".browser").animate({bottom: thumbs_height}, 500);
			//$(".thumbs").slideDown(500);
			$("#g_next, #g_prev").show();
		}, function(){
			//$(".browser").animate({bottom: 0},500);
			//$(".thumbs").slideUp(500);
			$("#g_next, #g_prev").hide();
		});
		$(".thumbs").hover(function(){
			$(".thumbs").css("opacity", "1");
		}, function(){
			$(".thumbs").css("opacity", "0.6");
		});
		
		$("#g_next, #g_prev").hide();
		$("#g_next a").click(function(e){
			e.preventDefault();
			$(this).blur();
			
			/*
			if($(".thumbs .currentimage").next().length == 0){
				$(".thumbs .currentimage").removeClass("currentimage");
				$(".thumbs li:first").addClass("currentimage");
				images.current = 0;
			} else {
				$(".thumbs .currentimage").removeClass("currentimage").next().addClass("currentimage");
			}
			*/
			new_img = ++images.current;
			if(new_img == 8){
				new_img = 1;
				images.current = 1;
			}
			swapImage(images[new_img]);
			
			
		});
		$("#g_prev a").click(function(e){
			e.preventDefault();
			$(this).blur();
			/*
				if($(".thumbs .currentimage").prev().length == 0){
					$(".thumbs .currentimage").removeClass("currentimage");
					$(".thumbs li:last").addClass("currentimage");
					images.current = 11;
				} else {
					$(".thumbs .currentimage").removeClass("currentimage").prev().addClass("currentimage");
				}
				*/
			new_img = --images.current;
			if(new_img == 0){
				new_img = 8;
				images.current = 8;
			}
			swapImage(images[new_img]);
		});
		$(".image_chooser a").click(function(e){
			//
			e.preventDefault();
			$(this).blur();
			var num = $(this).attr("href");
			num_arr = num.match(/i=([0-9]{2})/);
			num = num_arr[1];
			num = parseInt(num,10);
			images.current = num;
			swapImage(images[num]);
			
			$(".currentimage").removeClass("currentimage");
			$(this).parent().addClass("currentimage");
		});
	}
});

function swapImage(new_image_details){
	var img = new Image();
	$(".caption_text").fadeTo(1000, 0);
	$(".the_image_holder").fadeTo(1000, 0, function(){
		img.onload = function(){
			$(".the_image").attr("src", new_image_details.src);
			$(".the_image").attr("alt", new_image_details.alt);
			$(".caption_text").text(new_image_details.caption);
			$(".the_image_holder, .caption_text").fadeTo(1000, 1);
		}
		img.src=new_image_details.src;
		
	});	
}

function duplicateNavigation()
{
	$(".navigation").clone().appendTo("#local_banner");	
}

