summaryrefslogtreecommitdiffstats
path: root/source/javascripts/banner.js
blob: e380b150104d8bda15167531344967279a8f6fca (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
101
102
103
(function($){
	var banner = $('#banner');
	banner.children('.error').hide();

	var loaded = function(length){
		var images = banner.find('img'),
			dots = banner.children('.dots'),
			width = banner.children('.image').width(),
			fragment = document.createDocumentFragment(),
			count = 0,
			trigger = false,
			timer;
		
		images.each(function(i){
			$(this).css('left', width*i);

			var item = document.createElement('li');
			$(item).click(function(){
				shift(i);
			});
			if (i == 0) $(item).addClass('current');
			fragment.appendChild(item);
		});

		dots.append(fragment);

		var shift = function(i){
			if (trigger == false){
				var gap = width * (i - count);
				trigger = true;

				banner.find('.wrap').animate({left: '-='+gap}, 1000, 'easeOutQuart', function(){
					dots.children('li').eq(count).removeClass('current');
					dots.children('li').eq(i).addClass('current');
					banner.children('')
					count = i;
					main();
					trigger = false;
				});
			} else {
				return false;
			}
		};

		var prev = function(){
			para = count == 0 ? length - 1 : count - 1;
			shift(para);
		};

		var next = function(){
			shift((count+1)%length);
		};

		var main = function(){
			clearTimeout(timer);
			timer = setTimeout(next, 10000);
		};

		banner.on({
			'mouseenter': function(){
				clearTimeout(timer);
			},
			'mouseleave': function(){
				main();
			}
		}).on('click', '.prev', prev).on('click', '.next', next);

		main();
	};

	var random = function(){
		return (Math.round(Math.random())-0.5);
	};

	$.ajax({
		url: 'https://picasaweb.google.com/data/feed/api/user/105931860008509594725/albumid/5663590803175839297?alt=json&callback=?',
		dataType: 'json',
		type: 'GET',
		success: function(json){
			var fragment = document.createDocumentFragment(),
				arr = [];

			$(json.feed.entry).each(function(i, data){
				var link = data.media$group.media$thumbnail[0].url.replace(/\/\w\d+(-\w\d*)*\/([^\/]+)$/, '/s0/$2');
				arr.push(link);
			});

			arr.sort(random);
			var length = arr.length;

			for (var i=0; i<length; i++){
				var item = document.createElement('img');
				item.src = arr[i];
				fragment.appendChild(item);
			}

			banner.find('.wrap').append(fragment).children('img').eq(0).load(function(){
				banner.children('.loading').fadeOut(500);
				loaded(length);
			});
		}
	});
})(jQuery);