-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.mtgallery.js
222 lines (190 loc) · 5.61 KB
/
jquery.mtgallery.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
////////////////////////////////////////////
//
// jquery.mtgallery.js
//
//
////////////////////////////////////////////
jQuery.fn.mtgallery = function(options) {
var options = jQuery.extend( {
playerHeight: 300,
playerWidth: 450,
thumbMaxHeight: 80,
thumbMaxWidth: 120,
thumbSize: 'small',
nextBack: true,
backText: 'back',
nextText: 'next',
keyNav: false
}, options);
return this.each(function() {
var $el = $(this);
var slides = [];
// wrap the element in a mtgallery div
$el.hide().wrap($('<div class="mt-gallery"/>'));
var $gal = $el.parent('.mt-gallery');
$gal.append('<div class="mt-viewer"></div><ul class="mt-thumbs"></ul>');
var $viewer = $gal.find('.mt-viewer');
var $thumbs = $gal.find('.mt-thumbs');
function embedYouTube (id, autoplay) {
var html = '';
if (autoplay) { id += '?autoplay=1'};
html += '<iframe width="'+ options.playerWidth +'" height="'+ options.playerHeight +'"';
html += ' src="http://www.youtube.com/embed/'+ id +'" frameborder="0"';
html += ' allowfullscreen></iframe>';
return html;
}
function getId (url) {
// First check for new style youtube.be links
if (url.indexOf('http://youtu.be') === 0) {
return url.substr(url.lastIndexOf("/") + 1);
};
//else parse the querystring
var ytid = url.match("[\\?&]v=([^&#]*)");
if (ytid) {
return ytid[1]
};
return false;
}
function loadSlide ($gallery, order) {
var $slides = $gallery.find('.mt-viewer').children('div');
var $target = $slides.eq(order);
var $thumb = $gallery.find('.mt-thumbs').children('li').eq(order);
$slides.find('iframe').remove(); // get rid of playing videos
$slides.hide();
$gallery.find('.current').removeClass('current');
$target.show();
if ($thumb.hasClass('youtube')) {
$target.html(embedYouTube($thumb.attr('data-yt'), true));
};
$thumb.addClass('current');
}
function backSlide ($gal) {
var target = $gal.find('.mt-thumbs li.current').index();
if (target == 0) {
target = $gal.find('.mt-thumbs li:last').index();
}
else {
target --;
}
loadSlide($gal, target);
}
function nextSlide ($gal) {
var target = $gal.find('.mt-thumbs li.current').index();
if (target == $gal.find('.mt-thumbs li:last').index()) {
target = $gal.find('.mt-thumbs li:first').index();
}
else {
target ++;
}
loadSlide($gal, target);
}
function resizeImg ($img, maxWidth, maxHeight) {
var width = $img.width();
var height = $img.height();
if (width > maxWidth || width > height) {
ratio = maxWidth / width;
$img.css("width", maxWidth); // Set new width
$img.css("height", height * ratio); // Scale height based on ratio
width = maxWidth;
height = height * ratio; // Reset height to match scaled image
$img.css({
'position': 'absolute',
'left': '0px',
'top': Math.floor((maxHeight - height)/2 ) + 'px'
}).animate({
'opacity': 1
}, 500);
};
if(height > maxHeight || height > width){
ratio = maxHeight / height;
$img.css("height", maxHeight);
$img.css("width", width * ratio);
width = width * ratio;
height = maxHeight;
$img.css({
'position': 'absolute',
'top': '0px',
'left': Math.floor((maxWidth - width)/2 ) + 'px'
}).animate({
'opacity': 1
}, 500);
}
}
function loadImg ($img, order) {
var $thumb = $img.clone().appendTo($thumbs.children().eq(order));
// When loaded, add thumbnail and resize
$thumb.load(function() {
resizeImg($(this), options.thumbMaxWidth, options.thumbMaxHeight);
});
// add main image, resize
$img.appendTo($viewer.children().eq(order)).load(function() {
resizeImg($(this), options.playerWidth, options.playerHeight)
})
}
// go through each child of selector and
// pick out links etc
$el.children().each(function(i) {
var $this = $(this);
// get only the first link
var $link = $this.find('a:first');
var href = $link.attr('href');
var ytid = getId(href);
$('<div/>').appendTo($viewer).css('z-index',9000-i);
//add current class to first list item appended
var htmlString = i == 0 ? '<li class="current"/>' : '<li/>';
var $thumbwrap = $(htmlString).appendTo($thumbs).click(function() {
loadSlide($gal, $(this).index());
});
if (ytid) {
var thumbUrl = "http://img.youtube.com/vi/" + ytid + "/0.jpg";
var $thumb = $("<img/>")
.attr("src", thumbUrl)
.load(function() {
// add thumbnail,resize
$thumbs.children().eq(i).append($(this));
resizeImg($thumb, options.thumbMaxWidth, options.thumbMaxHeight)
});
// only load youtube initially if it's the first item in list
if (i == 0) {
$viewer.children('div').eq(i).html(embedYouTube(ytid, false));
}
$thumbwrap.addClass('youtube').attr('data-yt', ytid);
}
else {
// Is an image link
var $img = $("<img/>")
.attr("src", href)
.css({'opacity':0});
loadImg($img, i
}
});
if (options.nextBack) {
$('<a class="mt-back" href="#">' + options.backText + '</a>')
.appendTo($gal)
.click(function() {
backSlide($gal);
return false;
});
$('<a class="mt-next" href="#">' + options.nextText + '</a>')
.appendTo($gal)
.click(function() {
nextSlide($gal);
return false;
});
};
if (options.keyNav) {
$(document).keydown(function(e){
if (e.keyCode == 37) {
//left
backSlide($gal);
return false;
}
if (e.keyCode == 39) {
//right
nextSlide($gal);
return false;
}
});
};
});
};