]> git.0d.be Git - panikweb.git/blob - panikweb_templates/static/js/specifics.js
misc: use display: flex to stick footer at the bottom
[panikweb.git] / panikweb_templates / static / js / specifics.js
1 var urlParams;
2 (window.onpopstate = function () {
3     var match,
4         pl     = /\+/g,  // Regex for replacing addition symbol with a space
5         search = /([^&=]+)=?([^&]*)/g,
6         decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
7         query  = window.location.search.substring(1);
8
9     urlParams = {};
10     while (match = search.exec(query))
11        urlParams[decode(match[1])] = decode(match[2]);
12 })();
13
14 $(function() {
15
16         doLog = function(aTextToLog, type){
17                 var aLog = $('<div>',{'class':"log "+type,html:aTextToLog});
18                 aLog.hide().prependTo($log).show('fast').delay(3000).hide('fast', function() { 
19                         $(this).remove(); 
20                 });
21         }
22         var $main = $("#Changing");
23         var $metaNav = $("#metaNav");
24         var $log = $("#userLog");
25         tooltip_options = {
26                 position: {
27                         my: "center bottom-5",
28                         at: "center top",
29                         using: function( position, feedback ) {
30                                 $( this ).css( position );
31                                 $( "<div>" )
32                                 .addClass( "arrow" )
33                                 .addClass( feedback.vertical )
34                                 .addClass( feedback.horizontal )
35                                 .appendTo( this );
36                         }
37                 }
38         };
39
40         $('.newsRoll').tooltip(tooltip_options);
41         $('#metaNav').tooltip(tooltip_options);
42
43         /****************************************************/
44         /**** AJAX UTILITIES FOR REQUESTS ****/
45         /****************************************************/
46         String.prototype.decodeHTML = function() {
47                 return $("<div>", {html: "" + this}).html();
48         };
49         var loadPage_request = null;
50         afterLoad = function(html, textStatus, XMLHttpRequest) {
51                 loadPage_request = null;
52                 if (textStatus == "error") {
53                         doLog('Sorry! And error occur when loading page content','error');
54                 }
55                 new_html = $.parseHTML(html);
56                 new_content = $(new_html).find('#Changing>*');
57                 $main.hide().empty().append(new_content).show();
58
59                 /* calling onpopstate here is necessary to get urlParams to be
60                  * updated */
61                 window.onpopstate();
62
63                 canonical_href_node = $.grep($(new_html), function(elem, idx) {
64                         return (elem.nodeName === "LINK" && elem.attributes['rel'].value == "canonical");
65                 })[0];
66                 if (canonical_href_node) {
67                         canonical_href = canonical_href_node.attributes['href'].value;
68                         history.replaceState({}, '', canonical_href);
69                 }
70
71                 new_menu = $($.parseHTML(html)).find('#metaNav>*');
72                 $metaNav.empty().append(new_menu);
73
74                 var newTitle = html?html.match(/<title>(.*?)<\/title>/):'';
75                 if(newTitle){document.title = newTitle[1].trim().decodeHTML();}
76
77                 /*
78                 Quite UGLY but needed for styling the whole body with ID
79                 Feel free to correct and find a better way
80                 According to this link the probles is that $(html).filter('body').Attr('id') will not work!
81                 http://www.devnetwork.net/viewtopic.php?f=13&t=117065
82                 */
83                 if(sectionName = $(html).find('[data-section]').attr('data-section')){
84                         $('body').attr('id',sectionName);
85                 }else{
86                         var bodyID = html.match(/<body id="(.*?)">/);   
87                         if(bodyID){$('body').attr('id',bodyID[1].trim());}      
88                 }
89                 $.scrollTo('#Changing',1000,{offset:-$('#metaNav').height()+2});
90                 init();
91
92                 if (typeof (Piwik) == 'object') {
93                         piwikTracker = Piwik.getAsyncTracker();
94                         if (typeof (piwikTracker.trackPageView) == 'function') {
95                                 piwikTracker.setDocumentTitle(document.title);
96                                 piwikTracker.setCustomUrl(window.location.href);
97                                 piwikTracker.trackPageView();
98                                 $('.audio a').each(function() {
99                                         piwikTracker.addListener(this);
100                                 });
101                         }
102                 }
103
104         };
105
106         function afterLoadError(xhr, text, error) {
107                 afterLoad(xhr.responseText, 'error', xhr);
108         };
109
110         $(window).on("popstate", function(e) {
111                 if (e.originalEvent.state !== null) {loadPage(location.href, false);}
112         });
113
114         loadPage = function(href, push_state) {
115                 if (push_state !== false) {
116                         history.pushState({}, '', href);
117                 }
118                 if (loadPage_request !== null) {
119                         loadPage_request.abort();
120                 }
121                 loadPage_request = $.ajax({
122                         url: href,
123                         success: afterLoad,
124                         error: afterLoadError,
125                         dataType: 'html'});
126         };
127         $.fn.ajaxifyClick = function(params) {
128                 if ($('#df-wpr-sidebar').length > 0) {
129                         /* this is fiber sidebar, it doesn't work well with
130                          * seamless page loading */
131                         return;
132                 }
133                 this.each(function() {
134                         $(this).unbind('click');
135                         $(this).bind('click',function(e){
136                                 var href = $(this).attr("href");
137                                 if (href.match('\.(pdf|odt|ods|doc|xls|docx|xlsx|rtf)$')) {
138                                         /* open files */
139                                         window.open(href, '_blank');
140                                         return false;
141                                 }
142                                 if (e.which == 2) {
143                                         window.open(href, '_blank');
144                                         return false;
145                                 }
146                                 $(this).addClass('loading');
147                                 /* this checks the link points to a local document, be
148                                  * it because it's just a path, or because the server
149                                  * part points to the same domain */
150                                 if (!href) {
151                                         doLog('No href attributes, unable to load content','error');
152                                         $("#All a, #All area").removeClass('loading');
153                                         return false;
154                                 }else if (!$(this).attr('target') && (
155                                                 href.indexOf(document.domain) > -1 ||href.indexOf(':') === -1 || href.indexOf(':') > 5
156                                         )) {
157                                         loadPage(href);
158                                         return false;
159                                 }else{
160                                         $(this).attr('target','_blank');
161                                         $("#All a, #All area").removeClass('loading');
162                                         return true;
163                                 }
164                         });
165                 });
166         };
167         /****************************************************/
168         /**** AUDIOPLAYER ****/
169         /****************************************************/
170
171         var timer = null;
172         var refresh_onair_interval = 25000;
173         var ticker_interval = null;
174         /*
175         //TODO: mini icon version for player, (playpause only)
176         $('#togglePlayer').on('click',function(e) {
177                 e.preventDefault();
178                 if($(this).is('.icon-double-angle-left')){
179                         $("#player").fadeOut('fast',function(){
180                                 $("#player-container").toggleClass('minimized');                        
181                                 $('#togglePlayer').toggleClass('icon-double-angle-left icon-double-angle-right');
182                         });                     
183                 }else{
184                         $("#player").fadeIn('fast',function(){
185                                 $("#player-container").toggleClass('minimized');                        
186                                 $('#togglePlayer').toggleClass('icon-double-angle-left icon-double-angle-right');
187                         });     
188                 }
189                 return false;
190         });
191         */
192         $('#WhatsOnAir').on('load',function(){
193                 var WhatsOnAir = $(this);
194                 $('#RefreshWhatsOnAir').addClass('spinning');
195                 $.getJSON('/onair.json', function(onair) {
196                         setTimeout(function() { $('#RefreshWhatsOnAir').removeClass('spinning'); }, 5000);
197                         var onairContainer = $('<span>');
198                         if(onair.data.episode || onair.data.emission) {
199                                 if(onair.data.emission){
200                                         $('<a>',{href:onair.data.emission.url,html:onair.data.emission.title}).appendTo(onairContainer).ajaxifyClick();
201                                 }
202                                 if(onair.data.episode){
203                                         $('<span> - </span>').appendTo(onairContainer);
204                                         $('<a>',{href:onair.data.episode.url,html:onair.data.episode.title}).appendTo(onairContainer).ajaxifyClick();
205                                 }
206                         } else if (onair.data.nonstop) {
207                                 onairContainer = $('<span>' + onair.data.nonstop.title + '</span>');
208                                 if (onair.data.track_title) {
209                                         $('<span> - </span>').appendTo(onairContainer);
210                                         $('<span class="nonstop-track-title">' + onair.data.track_title + '</span>').appendTo(onairContainer);
211                                         if (onair.data.track_artist) {
212                                                 $('<span> </span>').appendTo(onairContainer)
213                                                 $('<span class="nonstop-track-artist">(' + onair.data.track_artist + ')</span>').appendTo(onairContainer);
214                                         }
215                                 }
216                         }
217                         else {
218                                 onairContainer = $('<span>Unknown (Probably Non-Stop)</span>');
219                         }
220                         var current_html = WhatsOnAir.html();
221                         var new_html = '<span>' + onairContainer.html() + '</span>';
222                         if (new_html !== current_html) {
223                                 WhatsOnAir.fadeOut();
224                                 WhatsOnAir.empty().append(onairContainer);
225                                 WhatsOnAir.fadeIn();
226                         }
227                 });
228         });
229         $('#RefreshWhatsOnAir').on('activate',function(e){
230                 $('#WhatsOnAir').trigger('load');
231                 timer = setInterval( "$('#WhatsOnAir').trigger('load');", refresh_onair_interval);
232         }).on('deactivate',function(e){
233                 $(this).removeClass('spinning');
234                 $('#WhatsOnAir').removeClass('active');
235                 clearInterval(timer);
236         }).on('click',function(e){
237                 $(this).toggleClass('active');
238                 if($(this).is('.active')){
239                         $(this).trigger('deactivate');
240                 }else{
241                         $(this).trigger('activate');
242                 }
243                 return false;
244         }).trigger('activate');
245         $("#DirectStreamPanikControler").on('click',function(e) {
246                 e.preventDefault();
247                 var stream = $('#DirectStreamPanik').get(0);
248                 if (stream.paused == false){
249                         stream.pause();
250                 }else{
251                         if (typeof (_paq) == 'object') {
252                                 _paq.push(['trackEvent', 'Audio', 'Play Stream']);
253                         }
254                         stream.play();
255                 }
256                 return false;
257         });
258         $('#DirectStreamPanik').on('play',function(){
259                 $('audio:not(#DirectStreamPanik)').each(function(){this.pause();});
260                 $('#streamSymbol').removeClass('icon-volume-up').addClass('icon-pause');
261                 $('#RefreshWhatsOnAir').trigger('activate');
262         }).on('pause',function(){
263                 //$('audio:not(#DirectStreamPanik)').each(function(){this.pause();});
264                 $('#streamSymbol').addClass('icon-volume-up').removeClass('icon-pause');
265         });
266         if($('#player-container').offset()){
267                 var topPosition = 0;
268                 topPosition = $('#mainHeader > div').offset().top + $('#mainHeader > div').height();
269                 $(window).bind('scroll load',function (event) {
270                         //$('#player-container').removeClass('fixed');
271                         var y = $(this).scrollTop() + 40;
272                         if (topPosition!== 0 && y >= topPosition) {
273                                 $('#player-container').addClass('fixed').removeClass('normal');
274                         } else {
275                                 $('#player-container').removeClass('fixed').addClass('normal');
276                         }
277                 });
278         }
279
280         var $localList = $('#localList').playlist({
281                 controlContainer: $('<div>',{'class':"playListControls"}).sortable(),
282                 playlistContainer: $('<ol>',{id:"myPlaylist",'class':"custom"}).sortable(),
283                 onLoad:function(self){
284                         $('#toggleList').on('click',function(){ 
285                                 self.playlistContainer.toggleClass('deploy');
286                         });
287                         $('#emptyList').on('click',function(){ 
288                                 self._reset();
289                         });
290
291                         if(self.isActive){
292                                 self.playlistContainer.scrollTo(self.isActive, 800 );
293                                 self.isActive.find('audio').attr('preload',"preload")
294                         }
295                         self.controlButtons['playpause'].addClass('resymbol');
296                 },
297                 onPlay:function(self){
298                         $('#DirectStreamPanik')[0].pause();
299                         self.playlistContainer.scrollTo(self.isActive, 800 );
300                 },
301                 onAdd:function(self){
302                         //self.isLastAdd[0].scrollIntoView();
303                         self.isLastAdd.find('a').ajaxifyClick();
304                         self.playlistContainer.scrollTo(self.isLastAdd, 800).delay(1000).scrollTo(self.isActive, 800 ).clearQueue();
305
306                         if (typeof (_paq) == 'object') {
307                                 _paq.push(['trackEvent', 'Audio', 'Add to playlist']);
308                         }
309                 },
310                 onUpdate:function(self){
311                         //doLog(JSON.stringify(self.playlist, null, '\t'));     
312                         if(self.playlist.length >= 1){
313                                 self.element.show();
314                                 $('#Player').addClass('withPlaylist').removeClass('withoutPlaylist');
315                         }else{
316                                 self.element.hide();
317                                 $('#Player').removeClass('withPlaylist').addClass('withoutPlaylist');
318                         }
319                 }
320         });
321
322         init = function() {
323                 $("#All a, #All area").removeClass('loading');
324                 $("#All a, #All area").ajaxifyClick();
325                 $("#search-form").unbind('submit').on('submit', function(event) {
326                         event.preventDefault();
327                         $(this).addClass('loading');
328                         loadPage($(this).attr('action') + '?' + $(this).serialize());
329                 });
330                 $(".tabs").each(function() {
331                         var self = $(this);
332                         var about= $($(this).attr("data-tab-about"));
333                         var current = $(this).find("[data-tab].active")[0];
334                         var dftShowSelector = current?".active":":first";
335                         var activeTab = $(this).find("[data-tab]"+dftShowSelector+"").addClass("active");
336                         $(this).find("[data-tab]").each(function() {
337                             $(this).on('click load',function (e) {  
338                                 e.preventDefault();
339                                 self.find(".active").removeClass("active");  
340                                 $(this).addClass("active");  
341                                 about.find("[data-tabbed]").hide();  
342                                 $($(this).attr("data-tab")).fadeIn();  
343                 
344                             });  
345                         });  
346                         activeTab.trigger('load');
347                 });
348                 $('[data-player-action]').on('click',function(){
349                         var audio = $('#'+$(this).attr('data-player-audio'));
350                         var sound_id = audio.data('sound-id');
351                         if($(this).attr('data-player-action') == "registerAudio"){
352                                 $localList.playlist("registerAudio",audio,doLog(audio.attr('title')+' has been added to your playlist.','ok'));
353                         }else if($(this).attr('data-player-action') == "playAudio"){
354                                 $localList.playlist("registerAudio",audio,doLog(audio.attr('title')+' will play soon.','ok'));
355                                 $localList.playlist("playSoundId", sound_id);
356                                 if ($(this).parent().find('.icon-pause').length) {
357                                         $(this).hide();
358                                         $(this).parent().find('.icon-pause').show();
359                                 }
360                         }else if ($(this).attr('data-player-action') == "pauseSounds") {
361                                 if ($(this).parent().find('.icon-play-sign').length) {
362                                         $(this).hide();
363                                         $(this).parent().find('.icon-play-sign').show();
364                                 }
365                                 $localList.playlist($(this).attr('data-player-action'));
366                         }else{
367                                 $localList.playlist($(this).attr('data-player-action'));
368                         }
369                 });
370                 $('[data-player-control]').each(function(){
371                         var audio = $('#'+$(this).attr('data-player-audio'));
372                         $localList.playlist("bindControl",$(this).attr('data-player-control'),audio,$(this));
373                 });
374
375                 $('[data-highlight]').on('check',function(){
376                         $($(this).attr('data-about')).find($(this).attr('data-highlight')).addClass('highlighted').removeClass('normal');
377                 }).on('uncheck',function(){
378                         $($(this).attr('data-about')).find($(this).attr('data-highlight')).removeClass('highlighted').addClass('normal');
379                 }).on('click',function(){
380                         $(this).toggleClass('icon-check icon-check-empty');
381                         if($(this).hasClass('icon-check')){$(this).trigger('check');
382                         }else{  $(this).trigger('uncheck');}
383                 });
384                 $('[data-highlight].icon-check-empty').each(function(){
385                         $(this).trigger('uncheck');
386                 });
387                 $('[data-toggle]').on('check',function(){
388                         /* make sure all other unchecked items are hidden */
389                         $('[data-toggle].icon-check-empty').each(function() {
390                                 $($(this).attr('data-about')).find($(this).attr('data-toggle')).hide();
391                         });
392                         $($(this).attr('data-about')).find($(this).attr('data-toggle')).show();
393                 }).on('uncheck',function(){
394                         $($(this).attr('data-about')).find($(this).attr('data-toggle')).hide();
395                         if ($('[data-toggle].icon-check').length == 0) {
396                                 /* special case the situation where all toggles
397                                  * are unchecked, as we want that to mean
398                                  * "everything", not "nothing".
399                                  */
400                                 $('[data-toggle].icon-check-empty').each(function() {
401                                         $($(this).attr('data-about')).find($(this).attr('data-toggle')).show();
402                                 });
403                         }
404                 }).on('click',function(){
405                         $(this).toggleClass('icon-check icon-check-empty');
406                         if($(this).hasClass('icon-check')){$(this).trigger('check');
407                         }else{  $(this).trigger('uncheck');}
408                 });
409                 $('[data-toggle].icon-check-empty').each(function(){
410                         $(this).trigger('uncheck');
411                 });
412
413                 initial_enabled_toggles = {};
414                 if (typeof(urlParams.q) == 'string') {
415                         urlParams.q.split('|').forEach(function(a) { initial_enabled_toggles[a] = 1; })
416                 }
417                 $('[data-toggle]').each(function() {
418                         if ($(this).data('toggle').substring(1) in initial_enabled_toggles) {
419                                 $(this).trigger('click');
420                         }
421                 });
422
423                 $('[data-popup-href]').on('click', function() {
424                         $.ajax({
425                                 url: $(this).data('popup-href'),
426                                 success: function (html, textStatus, jqXhr) {
427                                         $(html).appendTo($('body'));
428                                 }
429                         });
430                         return false;
431                 });
432
433                 if ($('input#id_q').val() == '') {
434                         $('input#id_q').focus();
435                 }
436
437                 $('#ticker li:not(:first)');
438                 if (ticker_interval) clearInterval(ticker_interval);
439                 function tick(){
440                     $('#ticker li:first').animate({'opacity':0}, 200, function () {
441                         $(this).appendTo($('#ticker')).css('opacity', 1);
442                     });
443                 }
444                 $("#roller button").on('click',function(e){
445                     clearInterval(ticker_interval);
446                     e.preventDefault();
447                     $($(this).attr('data-about')).prependTo('#ticker');
448                     return false;
449                 });
450                 ticker_interval = setInterval(function(){tick();  }, 20000);/**/
451
452                 function navsearch_click(event) {
453                         event.preventDefault();
454                         var query = $('#nav-search input').val();
455                         var form = $('#nav-search form');
456                         var href = '';
457                         if (query == '') {
458                                 href = $(form).attr('action');
459                         } else {
460                                 href = $(form).attr('action') + '?' + $(form).serialize();
461                         }
462                         if (event.which == 2) {
463                                 window.open(href, '_blank');
464                         } else {
465                                 $(this).addClass('loading');
466                                 loadPage(href);
467                         }
468                         return false;
469                 }
470                 $('#nav-search a').unbind('click').on('click', navsearch_click);
471                 $('#nav-search form').unbind('submit').on('submit', navsearch_click);
472
473                 $('[data-toggle-img]').bind('click', function() {
474                         var src = $(this).data('toggle-img');
475                         $(this).data('toggle-img', $(this).attr('src'));
476                         $(this).attr('src', src);
477                         $(this).toggleClass('right marged');
478                 });
479
480                 $('#Main #Emission-tabs-detail audio, div.soundcell audio').each(function(index, audio) {
481                         var audio_src = $(audio).find('source')[0];
482                         var sound_id = $(audio).data('sound-id');
483                         var $waveform = $(audio).next();
484                         $.getJSON(audio_src.src.replace('.ogg', '.waveform.json'), function(data) {
485                                 $waveform.empty();
486                                 $waveform.append('<i class="duration">' + $waveform.data('duration-string') + '</i>');
487                                 $.each(data, function(k, val) {
488                                         var val = val * 0.5;
489                                         $waveform.append('<span data-tick-index="' + k + '" style="height: ' + val + 'px;"></span>');
490                                 });
491                                 $waveform.show();
492                                 $waveform.find('span').on('click', function() {
493                                         /* if there's been something loaded */
494                                         var matching_audio = $('audio[data-sound-id=' + sound_id + ']');
495                                         if (matching_audio.length == 0) return;
496                                         matching_audio = matching_audio[0];
497                                         if (matching_audio.paused || matching_audio.ended) {
498                                                 /* ignore */
499                                                 return;
500                                         }
501                                         /* try to set time */
502                                         var total_duration = parseFloat($waveform.data('duration'));
503                                         var nb_ticks = $(this).parent().find('span').length;
504                                         var tick_index = $(this).data('tick-index');
505                                         matching_audio.currentTime = total_duration * tick_index / nb_ticks;
506                                 });
507                         });
508                 });
509
510                 $('#nav-language span').click(function() {
511                         document.cookie = 'panikweb_language=' + $(this).data('lang') + '; path=/';
512                         window.location = window.location;
513                         return false;
514                 });
515
516                 if (document.cookie.indexOf('panikdb=on') != -1) {
517                         panikdb_path = null;
518                         if (window.location.pathname.indexOf('/emissions/') == 0) {
519                                 panikdb_path = window.location.pathname;
520                         } else if (window.location.pathname.indexOf('/news/') == 0) {
521                                 panikdb_path = '/emissions' + window.location.pathname;
522                         }
523                         if (panikdb_path) {
524                                 $('<a id="panikdb" href="http://panikdb.radiopanik.org' + panikdb_path + '">Voir dans PanikDB</a>').appendTo($main);
525                         }
526                 }
527
528                 $('.gallery').each(function() {
529                   var $gallery = $(this);
530                   $gallery.find('span.image').on('click', function() {
531                     if ($(this).find('img').hasClass('portrait')) {
532                         $(this).parents('.gallerycell').addClass('portrait');
533                     } else {
534                         $(this).parents('.gallerycell').removeClass('portrait');
535                     }
536                     $gallery.find('div.first img').attr('src', $(this).data('image-large'));
537                     $gallery.find('div.first span.gallery-legend').text($(this).find('img').attr('title') || '');
538                     $gallery.find('div.first').show('fade');
539                     return false;
540                   });
541                   $gallery.find('div.first').on('click', function() { $(this).toggle('fade'); return false; });
542                 });
543
544         }
545         init();
546
547         if (! document.createElement('audio').canPlayType('audio/ogg') &&
548                 document.createElement('audio').canPlayType('audio/aac') ) {
549                 $('#ogg-m3u').hide().removeClass('resymbol');
550                 $('#aac-m3u').addClass('resymbol').show();
551         }
552
553         var konami = new Konami('/party');
554
555         $(document).on('panik:timeupdate', function(ev, data) {
556                 $waveform = $('#Main div.waveform[data-sound-id="' + data.sound_id + '"]');
557                 var elems = $waveform.find('span');
558                 var total_elems = elems.length;
559                 var done = total_elems * data.position;
560                 $waveform.find('span').each(function(k, elem) {
561                   if (k < done) {
562                         $(elem).addClass('done').removeClass('current');
563                   } else {
564                         $(elem).removeClass('done');
565                   }
566                 });
567                 $waveform.find('span.done:last').addClass('current');
568         });
569
570         $("body").keydown(function(e) {
571           var $visible_element = $('div.first:visible img');
572           if ($visible_element.length == 0) {
573             return true;
574           }
575           if ($visible_element.length > 1) {
576             /* remove all but last */
577             $visible_element.parent().find('img:not(:last)').remove();
578           }
579           var $visible_element = $('div.first:visible img');
580           var img_url = $visible_element.attr('src');
581           var all_img = $('div.gallery span[data-image-large] img');
582           var active_img = $('div.gallery span[data-image-large="' + img_url + '"] img');
583           var idx = all_img.index(active_img);
584           if (e.which == 37) { // left
585             idx--;
586             if (idx == -1) {
587               idx = all_img.length-1;
588             }
589           } else if (e.which == 39) { // right
590             idx++;
591             if (idx == all_img.length) {
592               idx = 0;
593             }
594           } else if (e.which == 27) { // escape
595             $visible_element.parent().toggle('fade');
596             return true;
597           } else {
598             return true;
599           }
600           /* create a new <img> with the new image but opacity 0, then display
601            * it using a css transition */
602           if (e.which == 37) { $visible_element.css('transform-origin', 'bottom right'); }
603           if (e.which == 39) { $visible_element.css('transform-origin', 'bottom left'); }
604           var new_img = $visible_element.clone().appendTo($visible_element.parent());
605           $(new_img).css('opacity', 0).attr('src', $(all_img[idx]).parent().data('image-large'));
606           $(new_img).css('transform', 'scale(0, 1)');
607           $(new_img)[0].offsetHeight; /* trigger reflow */
608           $(new_img).css('opacity', 1).css('transform', 'scale(1)');
609           $(new_img).parents('.gallery').find('span.gallery-legend').text($(all_img[idx]).attr('title') || '');
610           if ($(all_img[idx]).hasClass('portrait')) {
611             if (! $(new_img).parents('.gallerycell').hasClass('portrait')) {
612               $visible_element.parent().find('img:not(:last)').remove();
613               $(new_img).parents('.gallerycell').addClass('portrait');
614             }
615           } else {
616             if ($(new_img).parents('.gallerycell').hasClass('portrait')) {
617               $visible_element.parent().find('img:not(:last)').remove();
618               $(new_img).parents('.gallerycell').removeClass('portrait');
619             }
620           }
621           return false;
622         });
623
624 });