$(document).ready(function(){
	setup_top_menu();
	setup_locations();
	location_rotator();
});
function setup_locations(){
	$('.locations a').bind('click.locations',function(e){
		var locations = $('div#locations');
		locations.slideToggle('slow');
		return false;
	});
}
function setup_top_menu(){
	$('#top_nav ul.top_nav_main_list li a.jump_in, #top_nav ul.top_nav_main_list li a.jump_in_expanded').bind('click',function(e){
		if ($('#top_nav ul.top_nav_main_list li:eq(3)').css('display') != 'none'){
			//show
			$('#top_nav ul.top_nav_main_list li:eq(3)').hide();
			$('#top_nav ul.top_nav_main_list li:eq(4)').show();
			$('#top_nav ul.top_nav_main_list').animate({'marginLeft':'0px'}, 'slow');
		}else{
			//hide
			$('#top_nav ul.top_nav_main_list').animate({'marginLeft':'361px'}, 'slow', function(){
				$('#top_nav ul.top_nav_main_list li:eq(4)').hide();
				$('#top_nav ul.top_nav_main_list li:eq(3)').show();
			});
		}
		return false;
	});
}
function location_rotator(){
	// set some variable that we'll reuse
	var loc_list = $("#loc_selector ul")
	var loc_box = $("#primary_loc");

	// create a holding divs for the primary, next and previous images
	var current = loc_list.children("li:eq(0)");
	var next = loc_list.children("li:eq(1)");
	var prev = loc_list.children("li:last");
	
	loc_box.append('<div id="plate"><a href="'+current.children("a").attr("href")+'"><img src="'+current.children("a").attr("rel")+'" alt="'+current.children("a").attr("title")+'"></a><div class="details">'+current.find("div.details").html()+'</div></div>');
	loc_box.append('<div id="on_deck_circle"><a href="'+next.children("a").attr("href")+'"><img src="'+next.children("a").attr("rel")+'" alt="'+next.children("a").attr("title")+'"></a><div class="details">'+next.find("div.details").html()+'</div></div>');
	loc_box.append('<div id="bench"><a href="'+prev.children("a").attr("href")+'"><img src="'+prev.children("a").attr("rel")+'" alt="'+prev.children("a").attr("title")+'"></a><div class="details">'+prev.find("div.details").html()+'</div></div>');
	var plate = $("#plate");
	var circle = $("#on_deck_circle");
	var bench = $("#bench");
	
	// set the functionality for clicking on the thumbnails
	loc_list.find("li").click(function() {
		var currentLoc = loc_list.children("li.current");
		var onDeck = $(this);
		var nextLoc = onDeck.next("li").children("a");
		if (nextLoc.length == 0) {
			var nextLoc = loc_list.find("li:first a");
		}
		var prevLoc = onDeck.prev("li").children("a");
		if (prevLoc.length == 0) {
			var prevLoc = loc_list.find("li:last a");
		}
		plate.stop(false, true).fadeOut("slow");
		circle.stop(false, true).html('<a href="'+onDeck.children("a").attr("href")+'"><img src="'+onDeck.children("a").attr("rel")+'" alt="'+onDeck.children("a").attr("title")+'"></a><div class="details">'+onDeck.find("div.details").html()+'</div>').fadeIn("slow", function() {
			plate.html(circle.html()).show();
			circle.hide().html('<a href="'+nextLoc.attr("href")+'"><img src="'+nextLoc.attr("rel")+'" alt="'+nextLoc.attr("title")+'"></a><div class="details">'+nextLoc.siblings("div.details").html()+'</div>');
			bench.html('<a href="'+prevLoc.attr("href")+'"><img src="'+prevLoc.attr("rel")+'" alt="'+prevLoc.attr("title")+'"></a><div class="details">'+prevLoc.siblings("div.details").html()+'</div>');
		});
		currentLoc.removeClass("current");
		onDeck.addClass("current");
		
		return false;
	});
	
	// create and set the functionality for the manual rotate links	
	loc_box.append('<p id="rotate_prev"><a href="#">rotate previous</a></p><p id="rotate_next"><a href="#">rotate next</a></p>');
	$("#rotate_next").click(function() {
		rotate();
		return false;
	});
	$("#rotate_prev").click(function() {
		rotatePrev();
		return false;
	});
	
	// do the actual rotation
	function rotate() {
		currentLoc = loc_list.children("li.current");
		var onDeck = currentLoc.next("li");
		if (onDeck.length == 0) {
			var onDeck = loc_list.children("li:first");
		}
		var nextLoc = onDeck.next("li").children("a");
		if (nextLoc.length == 0) {
			var nextLoc = loc_list.find("li:first a");
		}
		plate.stop(false, true).fadeOut("slow");
		circle.stop(false, true).fadeIn("slow", function() {
			bench.html(plate.html());
			plate.html(circle.html()).show();
			circle.hide().html('<a href="'+nextLoc.attr("href")+'"><img src="'+nextLoc.attr("rel")+'" alt="'+nextLoc.attr("title")+'"></a><div class="details">'+nextLoc.siblings("div.details").html()+'</div>');
		});
		currentLoc.removeClass("current");
		onDeck.addClass("current");
	}
	function rotatePrev() {
		currentLoc = loc_list.children("li.current");
		var onDeck = currentLoc.prev("li");
		if (onDeck.length == 0) {
			var onDeck = loc_list.children("li:last");
		}
		var nextLoc = onDeck.prev("li").children("a");
		if (nextLoc.length == 0) {
			var nextLoc = loc_list.find("li:last a");
		}
		plate.stop(false, true).fadeOut("slow");
		bench.stop(false, true).fadeIn("slow", function() {
			circle.html(plate.html());
			plate.html(bench.html()).show();
			bench.hide().html('<a href="'+nextLoc.attr("href")+'"><img src="'+nextLoc.attr("rel")+'" alt="'+nextLoc.attr("title")+'"></a><div class="details">'+nextLoc.siblings("div.details").html()+'</div>');
		});
		currentLoc.removeClass("current");
		onDeck.addClass("current");
	}

	// preload large images
	var cache = [];
	loc_list.children("li").each(function() {
		var cacheImage = document.createElement('img');
		cacheImage.src = $(this).children("a").attr("rel");
		cache.push(cacheImage);
	})
}
