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