/* REFERENCE SITE LANGUAGE SPECIFIC JAVASCRIPT

   developer:   suef
   requires:    jQuery
   ========================================================================== */
$(document).ready(function(){
/* ==========================================================================
   Iconography
   ========================================================================== */
   if($('.iconography').length) {
       var iconography = '';
       var media = '';
       var other = '';
       var types = $('.iconography').attr('data-src').split(' ');
       var types_length = types.length;
       var mediaType = '';
       for(i=0; i<types_length; i++) {
            var GET = types[i].split('=');
            var type = GET[0];
            var value = GET[1];
            if(type == 's') {
                if(value == 't') {
                    media += '<span class="media service-credit"><span class="triangle"></span></span><span class="tooltip">This feature accesses external services<br>that require <a href="http://www.wolfram.com/service-credits">Wolfram Service Credits</a>.</span><span class="pipe"></span>';
                }
            }
            if(type == 'd') {
                if(value =='available') {
                    mediaType = 'Available on desktop systems';
                } else if(value == 'unavailable') {
                    mediaType = 'Unavailable on desktop systems';
                } else if(value == 'unknown') {
                    mediaType = 'Unknown on desktop systems';
                }
                if(mediaType !== '') {
                    media += '<span class="media desktop '+value+'"><span class="triangle"></span></span><span class="tooltip">'+mediaType+'</span>';
                }
            }
            if(type == 'm') {
                if(value =='available') {
                    mediaType = 'Available on Wolfram mobile app';
                } else if(value == 'unavailable') {
                    mediaType = 'Unavailable on Wolfram mobile app';
                } else if(value == 'unknown') {
                    mediaType = 'Unknown on Wolfram mobile app';
                }
                if(mediaType !== '') {
                    media += '<span class="media mobile '+value+'"><span class="triangle"></span></span><span class="tooltip">'+mediaType+'</span>';
                }
            }
            if(type == 'c') {
                if(value =='available') {
                    mediaType = 'Available on the cloud';
                } else if(value == 'unavailable') {
                    mediaType = 'Unavailable on the cloud';
                } else if(value == 'unknown') {
                    mediaType = 'Unknown on the cloud';
                }
                if(mediaType !== '') {
                    media += '<span class="media cloud '+value+'"><span class="triangle"></span></span><span class="tooltip">'+mediaType+'</span>';
                }
            }
            if(type == 'u') {
                if(value == 't') {
                    other += '<span class="updated highlight-link">Updated<span class="changes">show changes</span><span class="hide-changes">hide changes</span></span>';
                }
                if(value !== '' && value !== 't') {
                    other += '<span class="updated">Updated in '+value+'</span>';
                }
            }
            if(type == 'l') {
                if(value == 't') {
                    other += '<div class="ObjectNameTranslation"></div>';
                }
            }
            if(type == 'e') {
                if(value == 't') {
                    other += '<span class="experimental">[<span class="text">Experimental</span>]</span>';
                }
            }
            if(type == 'n') {
                if(value == 't') {
                    other += '<span class="new">New</span>';
                } else if ($.isNumeric(value)) {
                    other += '<span class="new">New in '+value+'</span>';
                }
            }
        }
        if(media !== '') {
            iconography += '<span class="media-icons">'+media+'</span>';
        }
        iconography += other;
        $('.iconography').html(iconography);
    }

/* ==========================================================================
   Audio
   ========================================================================== */
    $('.playAnimation').parents('.OCell').addClass('animation');
    if($('.Output').find('audio').length > 0) {
        $('.Output').on('click', function(){
            var clicked = document.getElementById($(this).find('audio').attr('id'));
            $('audio').each(function(e){
                var audio = document.getElementById($(this).attr('id'));
                if(audio !== clicked) {
                    if(audio.paused == false && audio.currentTime > 0) {
                        audio.pause();
                        audio.parentNode.classList.remove('paused');
                        audio.parentNode.querySelector('.tooltip').textContent = 'Play Audio';
                    }
                }
            });
            if(clicked.paused == false) {
                clicked.pause();
                clicked.parentNode.classList.remove('paused');
                clicked.parentNode.querySelector('.tooltip').textContent = 'Play Audio';
            }
            else if(clicked.paused == true) {
                clicked.play();
                clicked.parentNode.classList.add('paused');
                clicked.parentNode.querySelector('.tooltip').textContent = 'Pause Audio';
            }
            else {
                clicked.play();
                clicked.parentNode.classList.remove('paused');
                clicked.parentNode.querySelector('.tooltip').textContent = 'Play Audio';
            }
        });
    }
/* ==========================================================================
   Animation Flash
   ========================================================================== */
   $('.OCell').on('click', '.OCell.animation', function(){
        $(this).find('object').remove();
   });
});
var audios = document.querySelectorAll('audio');
if(audios !== null) {
    Array.prototype.forEach.call(audios, function(el, i){
        el.addEventListener('ended', function() {
            el.parentNode.classList.remove('paused');
            el.parentNode.querySelector('.tooltip').textContent = 'Play Audio';
        }, false);
    });
}
$('#DetailsAndOptions').on('load change click', function(){
/* ==========================================================================
   Details and Options close button
   ========================================================================== */
    $('#DetailsAndOptions .toggle.open').each(function(){
        var hideableHeight = $(this).siblings('.hideable').height();
        var sectionID = $(this).parents('section').prop('id');
        if(hideableHeight > 600) {
            if(!$(this).siblings('.hideable').find('.hideable-close-button').length) {
                $(this).siblings('.hideable').append('<a href="#'+sectionID+'" class="hideable-close-button">close</a>');
            }
        }
    });
});
$('#DetailsAndOptions').on('click', '.hideable-close-button', function(){
    $(this).parents('.hideable').prev('.NotesThumbnails').prev('.toggle').removeClass('open');
});