884dac05f640983e7f170ab7ed31a9d8ef3ace5e
[Portfolio.git] / skins / photo_film_viewer.js
1 /*
2 copyright 2008-2014 Benoit Pin - Centre de recherche en informatique - MINES ParisTech
3 http://plinn.org
4 Licence Creative Commons http://creativecommons.org/licenses/by-nc/2.0/
5 */
6
7 var FilmSlider;
8
9 (function(){
10
11 var keyLeft = 37, keyRight = 39;
12 var isTextMime = /^text\/.+/i;
13 var isAddToSelection = /.*\/add_to_selection$/;
14 var imgRequestedSize = /size=(\d+)/;
15 var DEFAULT_IMAGE_SIZES = [500, 600, 800];
16
17 FilmSlider = function(filmBar, slider, ctxInfos, image, toolbar, breadcrumbs) {
18 var thisSlider = this;
19 this.filmBar = filmBar;
20 this.filmBarWidth = getObjectWidth(this.filmBar);
21 var film = filmBar.firstChild;
22 if (film.nodeType === 3) { film = film.nextSibling; }
23 this.film = film;
24 this.slider = slider;
25 this.rail = slider.parentNode;
26 this.sliderSpeedRatio = undefined;
27 this.sliderRatio = undefined;
28 this.selectedSlide = undefined;
29 this.selectedSlideInSelection = undefined;
30 this.cartSlide = document.getElementById('cart_slide');
31 this.image = image;
32 this.stretchable = image.parentNode;
33 this.viewMode = 'medium';
34
35 this.buttons = [];
36 this.toolbar = toolbar;
37 if (breadcrumbs) {
38 var bcElements = breadcrumbs.getElementsByTagName('a');
39 this.lastBCElement = bcElements[bcElements.length-1];
40 var imgSrcParts = image.src.split('/');
41 this.lastBCElement.innerHTML = imgSrcParts[imgSrcParts.length-2];
42 this.hasBreadcrumbs = true;
43 }
44 else {
45 this.hasBreadcrumbs = false;
46 }
47
48 var buttons = toolbar.getElementsByTagName('img');
49 var b, name, i;
50 for (i=0 ; i<buttons.length ; i++) {
51 b = buttons[i];
52 name = b.getAttribute('name');
53 if (name) { this.buttons[name] = b; }
54 }
55
56 this.pendingImage = new Image();
57 this.pendingImage.onload = function() {
58 thisSlider.refreshImage();
59 };
60 this.initialized = false;
61
62 this.film.style.left='0';
63 this.film.style.top='0';
64
65 this.filmLength = ctxInfos.filmLength;
66 this.center = ctxInfos.center;
67 this.slideSize = ctxInfos.slideSize;
68 this.ctxUrlTranslation = ctxInfos.ctxUrlTranslation;
69
70 this.ddHandlers = {'down' : function(evt){thisSlider.mouseDownHandler(evt);},
71 'move' : function(evt){thisSlider.mouseMoveHandler(evt);},
72 'up' : function(evt){thisSlider.mouseUpHandler(evt);},
73 'out' : function(evt){thisSlider.mouseOutHandler(evt);}
74 };
75
76 if (browser.isMobile) {
77 this.rail.className = 'hidden';
78 }
79 else {
80 this.resizeSlider();
81 }
82 this.addEventListeners();
83 };
84
85 if (!browser.isMobile) {
86 FilmSlider.prototype.resizeSlider = function(evt) {
87 var filmBarWidth = this.filmBarWidth;
88 if (!filmBarWidth) { return; }
89 var filmWidth = this.slideSize * this.filmLength;
90 var sliderRatio = this.sliderRatio = filmBarWidth / filmWidth;
91 var sliderWidth = filmBarWidth * sliderRatio;
92 this.rail.style.width = filmBarWidth + 'px';
93 this.rail.style.display = 'block';
94 this.rail.style.visibility = 'visible';
95 if (sliderRatio < 1) {
96 this.slider.style.width = Math.round(sliderWidth) + 'px';
97 this.slider.style.visibility = 'visible';
98 }
99 else {
100 this.slider.style.visibility = 'hidden';
101 }
102
103 this.winSize = {'width' : getWindowWidth(),
104 'height' : getWindowHeight()};
105 this.maxRightPosition = filmBarWidth - sliderWidth;
106 this.sliderSpeedRatio = - (filmBarWidth - sliderWidth) / (filmWidth - filmBarWidth);
107 if (!this.initialized) {
108 this.centerSlide(this.center);
109 this.selectedSlide = this.filmBar.getElementsByTagName('img')[this.center].parentNode;
110 this.initialized = true;
111 }
112 };
113 }
114
115 else {
116 // pas de barre de scroll horizontal pour les tablettes
117 FilmSlider.prototype.resizeSlider = function(evt) {
118 this.filmMaxX = - (getObjectWidth(this.film) - this.filmBarWidth);
119 if (!this.initialized) {
120 this.centerSlide(this.center);
121 this.selectedSlide = this.filmBar.getElementsByTagName('img')[this.center].parentNode;
122 this.initialized = true;
123 }
124 };
125 }
126
127 FilmSlider.prototype._checkSizeAfterLoad = function(evt) {
128 this._barSizes = [];
129 this.filmBarWidth = this._barSizes[this._barSizes.length] = getObjectWidth(this.filmBar);
130 this.resizeSlider();
131 var self = this;
132 this._checkSizeIntervalId = setInterval(function(evt){self._checkSize(evt);}, 25);
133 setTimeout(function(evt){self._checkSizeStability();}, 250);
134 };
135
136 FilmSlider.prototype._checkSize = function(evt) {
137 this._barSizes[this._barSizes.length] = getObjectWidth(this.filmBar);
138 if (this._barSizes.length >= 2 &&
139 this._barSizes[this._barSizes.length-2] !== this._barSizes[this._barSizes.length-1]) {
140 this.filmBarWidth = this._barSizes[this._barSizes.length-1];
141 this.initialized = false;
142 this.resizeSlider();
143 }
144 };
145
146 FilmSlider.prototype._checkSizeStability = function(evt) {
147 var self = this;
148 var i;
149 for (i=0 ; i<this._barSizes.length - 1 ; i++) {
150 if (this._barSizes[i] !== this._barSizes[i+1]) {
151 this._barSizes = [];
152 setTimeout(function(evt){self._checkSizeStability();}, 250);
153 return;
154 }
155 }
156 clearInterval(this._checkSizeIntervalId);
157 delete this._barSizes;
158 delete this._checkSizeIntervalId;
159 };
160
161 FilmSlider.prototype.fitToScreen = function(evt) {
162 this._fitToScreen();
163 var thisSlider = this;
164 addListener(window, 'resize', function(evt){thisSlider._fitToScreen();});
165 };
166
167 FilmSlider.prototype._fitToScreen = function(evt) {
168 var wh = getWindowHeight();
169 if (!browser.isMobile) {
170 var rb = getObjectTop(this.rail) + getObjectHeight(this.rail); // rail bottom
171 }
172 else {
173 var rb = getObjectTop(this.filmBar) + getObjectHeight(this.filmBar); // film bottom
174 }
175 var delta = wh - rb;
176 var sh = getObjectHeight(this.stretchable);
177 var newSize = sh + delta;
178 this.stretchable.style.height = newSize + 'px';
179
180 var ratio = this.image.height / this.image.width;
181 var bestFitSize = this.getBestFitSize(ratio);
182 var currentSize = parseInt(imgRequestedSize.exec(this.image.src)[1], 10);
183 if (currentSize !== bestFitSize) {
184 var src = this.image.src.replace(imgRequestedSize, 'size=' + bestFitSize);
185 this.pendingImage.src = src;
186 }
187 };
188
189 FilmSlider.prototype.getBestFitSize = function(ratio) {
190 var fw = getObjectWidth(this.stretchable) - 1;
191 var fh = getObjectHeight(this.stretchable) - 1;
192
193 var i, irw, irh;
194 if (ratio < 1) {
195 for (i=DEFAULT_IMAGE_SIZES.length -1 ; i>0 ; i--) {
196 irw = DEFAULT_IMAGE_SIZES[i];
197 irh = irw * ratio;
198 if (irw <= fw && irh <= fh) { break; }
199 }
200 }
201 else {
202 for (i=DEFAULT_IMAGE_SIZES.length -1 ; i>0 ; i--) {
203 irh = DEFAULT_IMAGE_SIZES[i];
204 irw = irh / ratio;
205 if (irw <= fw && irh <= fh) { break; }
206 }
207 }
208 return DEFAULT_IMAGE_SIZES[i];
209 };
210
211 if (!browser.isMobile) {
212 FilmSlider.prototype.centerSlide = function(slideIndex) {
213 if (this.sliderRatio > 1) { return; }
214 var filmBarWidth = getObjectWidth(this.filmBar);
215 var x = slideIndex * this.slideSize;
216 x = x - (filmBarWidth - this.slideSize) / 2.0;
217 x = x * this.sliderSpeedRatio;
218 var p = new Point( -x, 0 );
219 this.setSliderPosition(p);
220 };
221 }
222 else {
223 FilmSlider.prototype.centerSlide = function(slideIndex) {
224 var filmBarWidth = getObjectWidth(this.filmBar);
225 var x = slideIndex * this.slideSize;
226 x = x - (filmBarWidth - this.slideSize) / 2.0;
227 this.setFilmPosition(-x);
228 };
229 }
230
231 FilmSlider.prototype.setSliderPosition = function(point) {
232 if(point.x < 0) { point.x = 0; }
233 if (point.x > this.maxRightPosition) { point.x = this.maxRightPosition; }
234 this.slider.style.left = point.x + 'px';
235 this.setFilmPosition(point);
236 };
237
238 if (!browser.isMobile) {
239 FilmSlider.prototype.setFilmPosition = function(point) {
240 this.film.style.left = point.x / this.sliderSpeedRatio + 'px';
241 };
242 }
243 else {
244 FilmSlider.prototype.setFilmPosition = function(x) {
245 x = Math.min(0, x);
246 x = Math.max(this.filmMaxX, x);
247 this.film.style.left = String(x) + 'px';
248 };
249 }
250
251 FilmSlider.prototype.getSliderPosition = function() {
252 var x = parseInt(this.slider.style.left, 10);
253 var y = parseInt(this.slider.style.top, 10);
254 var p = new Point(x, y);
255 return p;
256 };
257
258 FilmSlider.prototype.getFilmPosition = function() {
259 var x = parseInt(this.film.style.left, 10);
260 var y = parseInt(this.film.style.top, 10);
261 var p = new Point(x, y);
262 return p;
263 };
264
265 FilmSlider.prototype.loadSibling = function(previous) {
266 var slide = null;
267 if (previous) {
268 slide = this.selectedSlide.parentNode.previousSibling;
269 if (slide && slide.nodeType===3) { slide = slide.previousSibling; }
270 }
271 else {
272 slide = this.selectedSlide.parentNode.nextSibling;
273 if (slide && slide.nodeType===3) { slide = slide.nextSibling; }
274 }
275
276 if (!slide) { return; }
277 else {
278 var target = slide.getElementsByTagName('a')[0];
279 raiseMouseEvent(target, 'click');
280 var index = parseInt(target.getAttribute('portfolio:position'), 10);
281 this.centerSlide(index);
282 }
283 };
284
285 FilmSlider.prototype.addEventListeners = function() {
286 var thisSlider = this;
287 addListener(window, 'resize', function(evt){thisSlider.resizeSlider(evt);});
288 addListener(this.filmBar, 'click', function(evt){thisSlider.thumbnailClickHandler(evt);});
289 addListener(this.toolbar, 'click', function(evt){thisSlider.toolbarClickHandler(evt);});
290 addListener(window, 'load', function(evt){thisSlider.fitToScreen(evt);});
291 addListener(window, 'load', function(evt){thisSlider._checkSizeAfterLoad(evt);});
292 addListener(window, 'load', function(evt){thisSlider.startThumbnailsLoadQueue(evt);});
293
294 // dd listeners
295 addListener(this.slider, 'mousedown', this.ddHandlers.down);
296 if(browser.isDOM2Event){
297 if (browser.isAppleWebKit) {
298 this.filmBar.addEventListener('mousewheel', function(evt){thisSlider.mouseWheelHandler(evt);}, false);
299 }
300 else {
301 addListener(this.filmBar, 'DOMMouseScroll', function(evt){thisSlider.mouseWheelHandler(evt);});
302 }
303 }
304 else if (browser.isIE6up) {
305 addListener(this.filmBar, 'mousewheel', function(evt){thisSlider.mouseWheelHandler(evt);});
306 }
307 if (browser.isMobile) {
308 this.filmBar.addEventListener('touchstart', function(evt){thisSlider.touchStartHandler(evt);}, false);
309 this.filmBar.addEventListener('touchmove', function(evt){thisSlider.touchMoveHandler(evt);}, false);
310 this.filmBar.addEventListener('touchend', function(evt){thisSlider.touchEndHandler(evt);}, false);
311 }
312
313 addListener(document, 'keydown', function(evt){thisSlider.keyDownHandler(evt);});
314 addListener(document, 'keypress', function(evt){thisSlider.keyPressHandler(evt);});
315 };
316
317
318 FilmSlider.prototype.mouseDownHandler = function(evt) {
319 this.initialClickPoint = new Point(evt.clientX, evt.clientY);
320 this.initialPosition = this.getSliderPosition();
321 this.dragInProgress = true;
322 addListener(document, 'mousemove', this.ddHandlers.move);
323 addListener(document, 'mouseup', this.ddHandlers.up);
324 addListener(document.body, 'mouseout', this.ddHandlers.out);
325 };
326
327
328 FilmSlider.prototype.mouseMoveHandler = function(evt) {
329 if(!this.dragInProgress) { return; }
330
331 clearSelection();
332 evt = getEventObject(evt);
333 var currentPoint = new Point(evt.clientX, evt.clientY);
334 var displacement = currentPoint.diff(this.initialClickPoint);
335 this.setSliderPosition(this.initialPosition.add(displacement));
336 };
337
338 FilmSlider.prototype.mouseUpHandler = function(evt) {
339 this.dragInProgress = false;
340 evt = getEventObject(evt);
341 this.mouseMoveHandler(evt);
342 };
343
344
345 FilmSlider.prototype.mouseOutHandler = function(evt) {
346 evt = getEventObject(evt);
347 var x = evt.clientX;
348 var y = evt.clientY;
349 if (x < 0 ||
350 x > this.winSize.width ||
351 y < 0 ||
352 y > this.winSize.height
353 ) {
354 this.mouseUpHandler(evt);
355 }
356 };
357
358 FilmSlider.prototype.translateImgUrl = function(url) {
359 var canonicalImgUrl;
360 if (this.ctxUrlTranslation[0]) {
361 canonicalImgUrl = url.replace(this.ctxUrlTranslation[0],
362 this.ctxUrlTranslation[1]);
363 }
364 else {
365 canonicalImgUrl = url;
366 }
367 return canonicalImgUrl;
368 };
369
370 FilmSlider.prototype.thumbnailClickHandler = function(evt) {
371 var target = getTargetedObject(evt);
372 while (target.tagName !== 'A' && target !== this.filmBar) { target = target.parentNode; }
373 if (target.tagName !== 'A') { return; }
374 else {
375 if (this.viewMode === 'full') {
376 this.mosaique.unload();
377 this.mosaique = null;
378 this.viewMode = 'medium';
379 }
380 disableDefault(evt);
381 disablePropagation(evt);
382 target.blur();
383 history.pushState(target.href, '', target.href);
384
385 var imgBaseUrl = target.href;
386 var canonicalImgUrl = this.translateImgUrl(imgBaseUrl);
387
388 var ajaxUrl = imgBaseUrl + '/photo_view_ajax';
389 var thisFS = this;
390
391 //this.pendingImage.src = canonicalImgUrl + '/getResizedImage?size=600';
392 var thumbnail = target.getElementsByTagName('IMG')[0];
393 var bestFitSize = this.getBestFitSize(thumbnail.height/thumbnail.width);
394 this.pendingImage.src = canonicalImgUrl + '/getResizedImage?size=' + bestFitSize;
395
396 // update buttons
397 var fullScreenLink = this.buttons.full_screen.parentNode;
398 fullScreenLink.href = canonicalImgUrl + '/zoom_view';
399
400 var toggleSelectionBtn = this.buttons.toggle_selection;
401 var toggleSelectionLink = toggleSelectionBtn.parentNode;
402 this.selectedSlideInSelection = (target.className==='selected');
403 if (this.selectedSlideInSelection) {
404 toggleSelectionBtn.src = portal_url() + '/unselect_flag_btn.gif';
405 toggleSelectionBtn.alt = toggleSelectionLink.title = 'Retirer de la sélection';
406 toggleSelectionLink.href = canonicalImgUrl + '/remove_to_selection';
407 }
408 else {
409 toggleSelectionBtn.src = portal_url() + '/select_flag_btn.gif';
410 toggleSelectionBtn.alt = toggleSelectionLink.title = 'Ajouter à la sélection';
411 toggleSelectionLink.href = canonicalImgUrl + '/add_to_selection';
412 }
413
414 var showBuyableButtonLink = this.buttons.show_buyable.parentNode;
415 showBuyableButtonLink.href = canonicalImgUrl + '/get_slide_buyable_items';
416 this.cartSlide.innerHTML = '';
417 this.cartSlide.style.visibility='hidden';
418
419
420 var metadataButton = this.buttons.edit_metadata;
421 if (metadataButton) {
422 var metadataEditLink = metadataButton.parentNode;
423 metadataEditLink.href = canonicalImgUrl + '/photo_edit_form';
424 }
425
426
427 var req = new XMLHttpRequest();
428 req.onreadystatechange = function() {
429 switch (req.readyState) {
430 case 1 :
431 showProgressImage();
432 break;
433 case 2 :
434 try {
435 if (! isTextMime.exec(req.getResponseHeader('Content-Type'))) {
436 req.onreadystatechange = null;
437 req.abort();
438 hideProgressImage();
439 window.location.href = thisFS._fallBackUrl;
440 }
441 }
442 catch(e){}
443 break;
444 case 4 :
445 hideProgressImage();
446 if (req.status === 200) { thisFS.populateViewer(req); }
447 break;
448 }
449 };
450
451 req.open("GET", ajaxUrl, true);
452 req.send(null);
453
454 // update old displayed slide className
455 var className = this.selectedSlide.className;
456 var classes = className.split(' ');
457 var newClasses = [];
458 var name, i;
459
460 for (i=0 ; i<classes.length ; i++) {
461 name = classes[i];
462 if (name !== 'displayed') {
463 newClasses.push(name);
464 }
465 }
466
467 this.selectedSlide.className = newClasses.join(' ');
468
469 // hightlight new displayed slide
470 this.selectedSlide = target;
471 className = this.selectedSlide.className;
472 classes = className.split(' ');
473 classes.push('displayed');
474 this.selectedSlide.className = classes.join(' ');
475 }
476 };
477
478 FilmSlider.prototype.toolbarClickHandler = function(evt) {
479 var target = getTargetedObject(evt);
480 var button, link, url;
481 if(target.tagName === 'IMG' && target.getAttribute('name')) {
482 switch(target.getAttribute('name')) {
483 case 'previous' :
484 disableDefault(evt);
485 disablePropagation(evt);
486 button = target;
487 link = button.parentNode;
488 link.blur();
489 this.loadSibling(true);
490 break;
491 case 'next' :
492 disableDefault(evt);
493 disablePropagation(evt);
494 button = target;
495 link = button.parentNode;
496 link.blur();
497 this.loadSibling(false);
498 break;
499 case 'full_screen':
500 disableDefault(evt);
501 disablePropagation(evt);
502 target.parentNode.blur();
503 if (this.viewMode === 'full') {
504 this.mosaique.unload();
505 this.mosaique = null;
506 this.viewMode = 'medium';
507 return;
508 }
509 var main = document.getElementById('photo_viewer');
510 url = target.parentNode.href;
511 url = url.substring(0, url.length - '/zoom_view'.length);
512 var margins = {'top':0, 'right':-1, 'bottom':0, 'left':0};
513 this.mosaique = new Mosaique(main, url, margins);
514 this.viewMode = 'full';
515 break;
516
517 case 'toggle_selection':
518 disableDefault(evt);
519 disablePropagation(evt);
520 button = target;
521 link = button.parentNode;
522 link.blur();
523
524 var req = new XMLHttpRequest();
525 url = link.href;
526 req.open("POST", url, true);
527 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
528 req.send("ajax=1");
529
530 // toggle button
531 var parts = url.split('/');
532 var canonicalImgUrl = parts.slice(0, parts.length-1).join('/');
533
534 if (isAddToSelection.test(url)) {
535 button.src = portal_url() + '/unselect_flag_btn.gif';
536 button.alt = link.title = 'Retirer de la sélection';
537 link.href = canonicalImgUrl + '/remove_to_selection';
538 this.selectedSlide.className = 'selected displayed';
539 this.image.parentNode.className = 'selected';
540 this.selectedSlideInSelection = true;
541 }
542 else {
543 button.src = portal_url() + '/select_flag_btn.gif';
544 button.alt = link.title = 'Ajouter à la sélection';
545 link.href = canonicalImgUrl + '/add_to_selection';
546 this.selectedSlide.className = 'displayed';
547 this.image.parentNode.className = '';
548 this.selectedSlideInSelection = false;
549 }
550 break;
551
552 case 'show_buyable':
553 disableDefault(evt);
554 disablePropagation(evt);
555 button = target;
556 link = button.parentNode;
557 link.blur();
558 var slide = this.cartSlide;
559 slide.innerHTML = '';
560 slide.style.visibility = 'visible';
561 var cw = new CartWidget(slide, link.href);
562 cw.onCancel = function() {
563 CartWidget.prototype.onCancel.apply(this);
564 slide.style.visibility = 'hidden';
565 };
566 cw.onAfterConfirm = function() {
567 slide.style.visibility = 'hidden';
568 };
569 break;
570
571
572
573
574 /*
575 case 'edit_metadata' :
576 disableDefault(evt);
577 disablePropagation(evt);
578 target.blur();
579 if (this.viewMode === 'full') {
580 this.mosaique.unload();
581 this.mosaique = null;
582 this.viewMode = 'medium';
583 return;
584 }
585 var fi = new FragmentImporter(absolute_url());
586 fi.useMacro('metadata_edit_form_macros', 'iptc', 'image_metadata');
587 break;
588 */
589 }
590 }
591 };
592
593
594 if(browser.isDOM2Event) {
595 if (browser.isAppleWebKit) {
596 FilmSlider.prototype.mouseWheelHandler = function(evt) {
597 disableDefault(evt);
598 var pos = this.getSliderPosition();
599 pos.x -= evt.wheelDelta / 40;
600 this.setSliderPosition(pos);
601 };
602 }
603 else {
604 FilmSlider.prototype.mouseWheelHandler = function(evt) {
605 disableDefault(evt);
606 var pos = this.getSliderPosition();
607 pos.x += evt.detail * 3;
608 this.setSliderPosition(pos);
609 };
610 }
611 }
612 else if (browser.isIE6up) {
613 FilmSlider.prototype.mouseWheelHandler = function() {
614 var evt = window.event;
615 evt.returnValue = false;
616 var pos = this.getSliderPosition();
617 pos.x -= evt.wheelDelta / 40;
618 this.setSliderPosition(pos);
619 };
620 }
621
622 FilmSlider.prototype.touchStartHandler = function(evt) {
623 this.filmStartX = parseInt(this.film.style.left, 10);
624 this.touchStartX = evt.changedTouches[0].screenX;
625 this.touchStartTime = (new Date()).getTime();
626 };
627
628 FilmSlider.prototype.touchMoveHandler = function(evt) {
629 disableDefault(evt);
630 var delta = this.touchStartX - evt.changedTouches[0].screenX;
631 var posX = this.filmStartX - delta;
632 this.setFilmPosition(posX);
633 this.lastMoveTime = (new Date()).getTime();
634 };
635
636 FilmSlider.prototype.touchEndHandler = function(evt) {
637 var x = evt.changedTouches[0].screenX;
638 var delta = x - this.touchStartX;
639 if (delta) {
640 disableDefault(evt);
641 var now = (new Date()).getTime();
642 if (now - this.lastMoveTime < 100) {
643 // au delà de 100 ms de maintient, on annule l'inertie
644 var speed = delta / (now - this.touchStartTime)
645 var x0 = parseInt(this.film.style.left, 10);
646 var t0 = (new Date()).getTime();
647 var d = 500; // milisecondes
648 var delta = 0;
649 var dt = 25
650 var self = this;
651
652 function animate() {
653 // inertie
654 var t = (new Date()).getTime() - t0;
655 if (t < d) {
656 setTimeout(animate, dt);
657 delta = delta + (1-t/d) * speed * dt; // décelleration linéaire
658 self.setFilmPosition(x0 + delta);
659 }
660 }
661 animate();
662 }
663 }
664 this.touchStartX = undefined;
665 };
666
667
668 FilmSlider.prototype.keyDownHandler = function(evt) {
669 evt = getEventObject(evt);
670 switch (evt.keyCode) {
671 case keyLeft :
672 this.loadSibling(true);
673 break;
674 case keyRight :
675 this.loadSibling(false);
676 break;
677 default:
678 return;
679 }
680 };
681
682
683 FilmSlider.prototype.keyPressHandler = function(evt) {
684 var target = getTargetedObject(evt);
685 if (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA') { return; }
686 evt = getEventObject(evt);
687 var charPress = String.fromCharCode((evt.keyCode) ? evt.keyCode : evt.which);
688 switch(charPress) {
689 case 'f':
690 case 'F':
691 raiseMouseEvent(this.buttons.full_screen, 'click');
692 break;
693 }
694 };
695
696 FilmSlider.prototype.populateViewer = function(req) {
697 var elements = req.responseXML.documentElement.childNodes;
698 var i;
699 for(i=0 ; i < elements.length ; i++ ) {
700 element = elements[i];
701 switch (element.nodeName) {
702 case 'fragment' :
703 var dest = document.getElementById(element.getAttribute('id'));
704 if (dest) { dest.innerHTML = element.firstChild.nodeValue; }
705 break;
706 case 'imageattributes' :
707 var link = this.buttons.back_to_portfolio.parentNode;
708 link.href = element.getAttribute('back_to_context_url');
709 link = this.buttons.show_buyable.parentNode;
710 var buyable = element.getAttribute('buyable');
711 if(buyable === 'True') { link.className = null; }
712 else if(buyable === 'False') { link.className = 'hidden'; }
713 this.image.alt = element.getAttribute('alt');
714 this.updateBreadcrumbs(element.getAttribute('last_bc_url'),
715 element.getAttribute('img_id'));
716 break;
717 }
718 }
719 };
720
721 FilmSlider.prototype.refreshImage = function() {
722 this.image.style.visibility = 'hidden';
723 this.image.src = this.pendingImage.src;
724 this.image.width = this.pendingImage.width;
725 this.image.height = this.pendingImage.height;
726 this.image.style.visibility = 'visible';
727 if (this.selectedSlideInSelection) { this.image.parentNode.className = 'selected'; }
728 else { this.image.parentNode.className = ''; }
729 };
730
731 FilmSlider.prototype.updateBreadcrumbs = function(url, title) {
732 if (this.hasBreadcrumbs) {
733 this.lastBCElement.href = url;
734 this.lastBCElement.innerHTML = title;
735 }
736 };
737
738 FilmSlider.prototype.startThumbnailsLoadQueue = function(evt) {
739 var thumbnails = this.film.getElementsByTagName('img');
740 if (thumbnails.length === 1) { return; }
741 this.thumbnailsLoadingOrder = [];
742 var leftSize = this.center;
743 var rightSize = thumbnails.length - this.center - 1;
744 var i;
745 for (i=1 ; i<=Math.min(leftSize, rightSize) ; i++) {
746 this.thumbnailsLoadingOrder.push(thumbnails[this.center + i]);
747 this.thumbnailsLoadingOrder.push(thumbnails[this.center - i]);
748 }
749 if (leftSize > rightSize) {
750 for (i = this.center - rightSize - 1 ; i >= 0 ; i--) {
751 console.log(i);
752 this.thumbnailsLoadingOrder.push(thumbnails[i]);
753 }
754 }
755 else if (leftSize < rightSize) {
756 for (i = this.center + leftSize ; i < thumbnails.length ; i++) {
757 this.thumbnailsLoadingOrder.push(thumbnails[i]);
758 }
759 }
760 var next = this.thumbnailsLoadingOrder.shift();
761 var self = this;
762 addListener(next, 'load', function(evt){self._loadNextThumb(evt);});
763 next.src = this.translateImgUrl(next.parentNode.href) + '/getThumbnail';
764 };
765
766 FilmSlider.prototype._loadNextThumb = function(evt) {
767 var next = this.thumbnailsLoadingOrder.shift();
768 if (!next) {return;}
769 var self = this;
770 addListener(next, 'load', function(evt){self._loadNextThumb(evt);});
771 next.src = this.translateImgUrl(next.parentNode.href) + '/getThumbnail';
772 };
773
774
775 FilmSlider.prototype.startSlideShow = function() {
776 this.slideShowSlide = this.pendingSlideShowSlide = this.selectedSlide;
777 return this.slideShowSlide.href;
778 };
779
780 FilmSlider.prototype.slideShowNext = function() {
781 var nextSlide = this.slideShowSlide.parentNode.nextSibling;
782 if (nextSlide && nextSlide.nodeType===3) { nextSlide = nextSlide.nextSibling; }
783
784 if (nextSlide) {
785 nextSlide = nextSlide.getElementsByTagName('a')[0];
786 this.pendingSlideShowSlide = nextSlide;
787 return this.pendingSlideShowSlide.href;
788 }
789 else {
790 var row = this.slideShowSlide.parentNode.parentNode;
791 var first = row.firstChild;
792 if (first.nodeType===3) { first = first.nextSibling; }
793 this.pendingSlideShowSlide = first.getElementsByTagName('a')[0];
794 return this.pendingSlideShowSlide.href;
795 }
796 };
797
798 FilmSlider.prototype.slideShowPrevious = function() {
799 var previousSlide = this.slideShowSlide.parentNode.previousSibling;
800 if (previousSlide && previousSlide.nodeType===3) { previousSlide = previousSlide.previousSibling; }
801
802 if (previousSlide) {
803 previousSlide = previousSlide.getElementsByTagName('a')[0];
804 this.pendingSlideShowSlide = previousSlide;
805 return this.pendingSlideShowSlide.href;
806 }
807 else {
808 var row = this.slideShowSlide.parentNode.parentNode;
809 var last = row.lastChild;
810 if (last.nodeType===3) { last = last.previousSibling; }
811 this.pendingSlideShowSlide = last.getElementsByTagName('a')[0];
812 return this.pendingSlideShowSlide.href;
813 }
814 };
815
816 FilmSlider.prototype.slideShowImageLoaded = function() {
817 this.slideShowSlide = this.pendingSlideShowSlide;
818 };
819
820 FilmSlider.prototype.stopSlideShow = function() {
821 raiseMouseEvent(this.slideShowSlide, 'click');
822 var index = parseInt(this.selectedSlide.getAttribute('portfolio:position'), 10);
823 this.centerSlide(index);
824 };
825
826
827 /* UTILS */
828 function Point(x, y) {
829 this.x = Math.round(x);
830 this.y = Math.round(y);
831 }
832 Point.prototype.diff = function(point) { return new Point(this.x - point.x, this.y - point.y); };
833 Point.prototype.add = function(point) { return new Point(this.x + point.x, this.y + point.y); };
834 Point.prototype.mul = function(k) { return new Point(this.x * k, this.y *k); };
835 Point.prototype.toString = function() { return "(" + String(this.x) + ", " + String(this.y) + ")"; };
836
837 }());