aboutsummaryrefslogtreecommitdiffstats
path: root/public/javascripts/app.js
blob: 759c5eb3ba411045077b0d70ac44391bc3341ba4 (plain)
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
var App = App || null;

(function() {
	var cApp = function() {
		this.characters = [];
		this.answer = null;
		this.answer_name = '';
		this.answer_image_url = '';
		this.playlist = []
		
		
		// CoverFlow
		this.initialise_js_cover_flow = function(playlist) {
			var that = this;
			coverflow('character-select-container').setup({
				width: '100%',
				playlist: playlist,
				coverheight: 130,
				textoffset: 68
			}).on('ready', function() {
				var the_other = that;
				this.on('click', function(e) {
					if (the_other.playlist[e].title == the_other.answer_name) {
						// Answered correctly
						the_other.pick_character({ correct: true });
					}
					else {
						the_other.pick_character({ correct: false });
					}
				});
			});
		};
		
		this.initialize_video = function(url) {
			var $video = $('<video src="' + url + '" controls>\
				Your browser does not support the <code>video</code> element.\
			</video>');
			$('section.video').append($video);
		};
		
		this.get_characters = function(series_id, params) {
			// Populate characters
			var that = this;
			var params = params || {
				answer_id: -1
			};
			var $character_container = $('#character-select .flow');
			$.get(
				'/character-images/' + series_id,
				function(response) {
					var r = JSON.parse(response);
			
					$character_container.empty()
					
					for (var i = 0; i < r.characters.length; i++) {
						that.playlist.push({
							image: r.characters[i].image,
							title: r.characters[i].name
						});
						
						that.characters.push({
							personId: r.characters[i].tms_personId,
							name: r.characters[i].name
						});
						
						if (params.answer_id == r.characters[i].tms_personId) {
							that.answer_name = r.characters[i].name;
							that.answer_image_url = r.characters[i].image
						}
					}
					that.initialise_js_cover_flow(that.playlist);
				}
			);
		};
		
		this.pick_character = function(params) {
			var params = params || {
				correct: false
			};
			
			var $open_dialog_button = $('<a id="character-chosen-dialog-button" href="#" data-transition="slideup" style="display: none;"></a>');
			$('body').append($open_dialog_button);
			var $open_dialog_button_in_dom = $('#character-chosen-dialog-button');
			
			if (params.correct) {
				$open_dialog_button_in_dom.attr('href', '/character-chosen/true');
			}
			else {
				$open_dialog_button_in_dom.attr('href', '/character-chosen/false/?character_name=' + this.answer_name + '&character_image_url=' + this.answer_image_url);
			}
			
			$open_dialog_button_in_dom.trigger('click');
		};
		
		
		return this;
	};
	
	App = new cApp();
})();