console.log('zetcom.multimedia.js loaded ' + Date());

/**
 * Zetcom AG
 * Author: Romain de Wolff
 * Date: 28.05.2015
 *
 * Enable mutlimedia video as standard image
 *
 * Prerequisits :

 */
// Handle multimedia display
// make mp4 playable video
// condition : multimedia module and compatible file extension
// currently compatible file extension : MP4
/*
$(window).load(function() {
	console.log('window.load');
});
$(document).ready(function() {
	console.log('document.ready');
});
$(window).unload(function() {
	console.log('window.unload');
});
*/

// test LINK : http://emp-web-67.zetcom.ch/multimedia/test.mp4


$(document).ready(function() {

    if ($('#multimediaDetailList').length) {

        // we are in multimedia detail list (inline)
        var multimediaID = $('.mulId .tspValue').html();
        var multimediaExt = $('.mulExtent .tspValue').html();
        var indexID;
        var hasMP4 = false;

        // iterate over all the multimedia elements. take the first one that has the extention mp4
        $(".mulExtent .tspValue").each(function(index) {
            if ($(this).text() === 'mp4') {
                multimediaExt = 'mp4';
                // save the index to take the correct mulID
                indexID = index;
                hasMP4 = true;
                return false;
            }

        });
        // search for the correct mp4 if there is one existing
        if (hasMP4) {
            $(".mulId .tspValue").each(function(index) {
                if (index === indexID) {
                    multimediaID = $(this).text();
                }
            });

        }

        // need the object ID to show standard image of object in video player while video is loading
        var objID = $('.objID .tspValue').html();

        // debug
        console.log('Mul.Id:', multimediaID);
        console.log('Mul.Extent:', multimediaExt);

        // media type : video, file or other. Other are considered images.
        var videoExt = ['mp4'];

        var fileExt = [
            'pdf',
            'mov',
            'mpg',
            'mp4',
            'flc',
            'doc',
            'wmv',
            'ppt',
            'zip',
            'txt',
            'xls',
            'xlsx',
            'pps',
            'docx',
            'pptx'
        ];

        // if the multimedia extension is a video extension
        if (videoExt.indexOf(multimediaExt) > -1) {
            var multimediaUrl = '?service=MultimediaAsset&objectId=' + multimediaID;
            //var multimediaUrl = 'http://emp-web-67.zetcom.ch/multimedia/test.mp4';
            //var multimediaUrl = 'http://vjs.zencdn.net/v/oceans.mp4';
            console.log('multimediaUrl href:', multimediaUrl);
            // prepare the HTML code for the Media player
            var multimediaElementPlayer = '';
            // MZ2 added poster attribute to define image to be displayed before loading video - this image should be the standard image of the object
            multimediaElementPlayer = '<video src="' + multimediaUrl + '" type="video/mp4" id="player1" playsinline="true" controls="controls" preload="none" poster="?service=ImageAsset&module=collection&objectId=' + objID + '&resolution=superImageResolution"></video>';
            // replace the standard image (.listImg) with the video
            $('#collectionDetailItem .listImg').html(multimediaElementPlayer);
            // play the video
            MediaElement('player1', {
                flashName: '?service=WebAsset&url=mediaelement/flashmediaelement.swf&contentType=application/x-shockwave-flash',
                success: function(player, node) {
                    // customer asked to disable auto play
                    // player.play();
                    // $('#' + node.id + '-mode').html('mode: ' + player.pluginType);
                },
                error: function(err) {
                    console.log('media element ERROR', err);
                }
            });
            //$('#collectionReferences').remove();// remove the references (if it contains videos) on the bottom
        } else if (fileExt.indexOf(multimediaExt) > -1) {
            // it's a file, let's put the link on the main image
            // get the file url
            var fileUrl = $('.mulFilename a').attr('href');
            // update the standard image url
            $('.detailImg .fancybox').attr('href', fileUrl);
            // remove the fancybox class on the image
            $('.detailImg a').removeClass('fancybox');
            // add target attribute so it's opened in a new window
            $('.detailImg a').attr('target', '_blank');
        }
    };

    // remove the multimedia extension (don't needs to be displayed, used)
    $('.mulId').remove(); // remove mul. Id
    $('.mulExtent').remove(); // remove mul. extension

});