aboutsummaryrefslogtreecommitdiffstats
path: root/web/js/010-debugger.js
blob: 31d3db8ed06a44cba68d3fa7a73610ec4894c50f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
$(document).ready(function() {
	function cachePath(path) {
		return path.replace(/ /g, "_").replace(/\//g, "-");
	}
	function imagePath(image, path, size, square) {
		var suffix;
		if (square)
			suffix = size.toString() + "s";
		else
			suffix = size.toString();
		return "cache/" + cachePath(path + "/" + image + "_" + suffix + ".jpg");
	}
	function loadAlbum(path) {
		if (path in album_cache) {
			albumLoaded(album_cache[path]);
			return;
		}
		$.ajax({
			type: "GET",
			url: "cache/" + path + ".json",
			error: function() { $(document.body).html("Couldn't fetch it."); },
			success: albumLoaded
		});
	}
	function albumLoaded(album) {
		album_cache[cachePath(album.path)] = album;
		current_album = album;
		if (current_image_cache != null)
			showPhoto();
		else if (cachePath(album.path) == current_album_cache)
			showAlbum();
	}
	function showAlbum() {
		$("html, body").animate({ scrollTop: 0 }, "slow");
		var title = "";
		var components = current_album.path.split("/");
		var last = "";
		for (var i = 0; i < components.length; ++i) {
			last += "/" + components[i];
			if (i < components.length - 1)
				title += "<a href=\"#" + cachePath(last.substring(1)) + "\">";
			title += components[i];
			if (i < components.length - 1) {
				title += "</a>";
				title += " &raquo; ";
			}
		}
		$("#title").html(title);
		var photos = "";
		for (var i = 0; i < current_album.photos.length; ++i)
			photos += "<a href=\"#" + current_album_cache + "/" + cachePath(current_album.photos[i].name) + "\"><img border=0 src=\"" + imagePath(current_album.photos[i].name, current_album.path, 150, true) + "\" height=150 width=150></a>";
		$("#photos").html(photos);
		if (current_album.albums.length)
			$("#subalbums-title").show();
		else
			$("#subalbums-title").hide();
		var subalbums = "";
		for (var i = 0; i < current_album.albums.length; ++i)
			subalbums += "<a href=\"#" + cachePath(current_album.path + "/" + current_album.albums[i].path) + "\"><li>" + current_album.albums[i].path + "</li></a>";
		$("#subalbums").html(subalbums);
		
		$("#album").fadeIn();
		$("#photo").fadeOut();
	}
	function showPhoto() {
		var index;
		for (index = 0; index < current_album.photos.length; ++index) {
			if (cachePath(current_album.photos[index].name) == current_image_cache)
				break;
		}
		if (index >= current_album.photos.length) {
			$(document.body).html("Wrong picture.");
			return;
		}
		$("#photo").html("<a href=\"#" + current_album_cache + "\"><img src=\"" + imagePath(current_album.photos[index].name, current_album.path, 640, false) + "\"></a>");
		$("#album").fadeOut();
		$("#photo").fadeIn();
	}
	var current_album_cache = "";
	var current_image_cache = "";
	var current_album = null;
	var album_cache = new Array();
	$(window).hashchange(function() {
		var new_album_cache = location.hash.substring(1);
		var index = new_album_cache.lastIndexOf("/");
		if (index != -1 && index != new_album_cache.length - 1) {
			current_image_cache = new_album_cache.substring(index + 1);
			new_album_cache = new_album_cache.substring(0, index);
		} else
			current_image_cache = null;
		if (!new_album_cache.length)
			new_album_cache = cachePath("New York Summer 2009"); //root
		if (new_album_cache != current_album_cache) {
			current_album_cache = new_album_cache;
			loadAlbum(current_album_cache);
		} else if (current_image_cache != null)
			showPhoto();
		else
			showAlbum();
	});
	$(window).hashchange();
});