$(document).ready(function() {
	// Tour Photos User Interface Effects
	
	// On hover, li background is darker, and other li's fadeout slightly
	
	album_position = -150; // Used in up / down navigation at bottom
	album_position_down = 300;
	
	$("#tour_albums li").hover(function() {
		$(this).css({ "background-color" : "#292929" });
		
		$(this).children("img").css({ "cursor" : "pointer" });

		$("#tour_albums li").not(this).animate({
			"opacity" : ".5"
		}, { queue:false, duration:300});
		
	}, function() {
		$(this).css({
			"background-color" : "#303030",
			"cursor" : "auto"
		});
		
		$("#tour_albums li").not(this).animate({
			"opacity" : "1"
		}, { queue:false, duration:300});
	});
	
	$("#tour_albums li img").click(function() {
		$(this).parent().children(":last-child").fadeIn("slow");
		
		image_folder = $(this).parent().prevAll("#tour_albums li").length + 1;
		image_items = $(this).parent().attr('class').split(' ').slice(-1);
		
		template_path = $("#template_path").text();
		
		var i;
		for(i = 1; i<= image_items; i++) {			
			$(this).parent().children(".photo_container").children(".photo_buffer").append('<a href="' + template_path + '/tour_photos/' + image_folder + '/full_image/' + i + '.jpg" rel="' + image_folder + '"><img src="' + template_path + '/tour_photos/' + image_folder + '/thumbnails/' + i + '.jpg" /></a>');
		}
		
		if (image_items / 3 <= 5) { // Hides up / down controls if images fit onto page
			$(this).parent().children(".photo_container").children(".album_controls").hide();
		}
		
		$("#tour_albums .photo_buffer a").fancybox({ // This has to be in this function due to elements being appended
		'overlayOpacity' : .7,
		'overlayColor' : '#000'
	});
	});
	
	$("#tour_albums li .photo_container .exit").click(function() {
		$("#tour_albums li div.photo_container div.photo_buffer img").remove();
		
		$(this).parent().parent().fadeOut("slow");
		
		// Rest of this function resets controls for albums
		
		album_position = -150;
		
		$("#tour_albums li .photo_container .photo_buffer").css({
			"top" : "0px"
		});
		
		$(".album_controls .up_control").css({
			"background-position" : "-34px 0px",
			"cursor" : "default"
		});
		
		$(".album_controls .down_control").css({
			"background-position" : "0px 0px",
			"cursor" : "pointer"
		});
		
		return false;
	});
	
	$(".album_controls .up_control").click(function() {
		
		if(album_position >= -150) {
			$(this).css({
				"background-position" : "-34px 0px",
				"cursor" : "default"
			});
		}
		
		else if(album_position >= -300  && album_position < 0) {
			$(this).css({
				"background-position" : "-34px 0px",
				"cursor" : "default"
			});
			
			$(this).parent().parent().children(".photo_buffer").animate({
				"top" : album_position + album_position_down + "px"
			});
			
			album_position = album_position + 150;
		}
		
		else {
			$(this).css({
				"background-position" : "0px 0px",
				"cursor" : "pointer"
			});
			
			$(this).parent().parent().children(".photo_buffer").animate({
				"top" : album_position + album_position_down + "px"
			});
			
			album_position = album_position + 150;
		}
		
		$(this).parent().children(".down_control").css({
			"background-position" : "0px 0px",
			"cursor" : "pointer"
		});
		
		return false;
		
	});
	
	$(".album_controls .down_control").click(function() {
		
		album_images = $(this).parent().parent().parent().attr('class');
		
		end_value = (Math.ceil((image_items - 15) / 5) * -150); // has to be negative, only used for going down album
		
		if(album_position > end_value) {
			$(this).parent().parent().children(".photo_buffer").animate({
				"top" : album_position + "px"
			});
			
			album_position = album_position - 150;
		}
		
		else if(album_position <= end_value + 150 && album_position >= end_value) {
			$(this).css({
				"background-position" : "-34px 0px",
				"cursor" : "default"
			});
			
			$(this).parent().parent().children(".photo_buffer").animate({
				"top" : album_position + "px"
			});
			
			album_position = album_position - 150;
		}
		
		else {
			$(this).css({
				"background-position" : "-34px 0px",
				"cursor" : "default"
			});
		}
		
		$(this).parent().children(".up_control").css({
			"background-position" : "0px 0px",
			"cursor" : "pointer"
		});
		
		return false;
	});
});
