// Froogaloop function for Vime videos: https://f.vimeocdn.com/js/froogaloop2.min.js

var Froogaloop=function(){function e(a){return new e.fn.init(a)}function g(a,c,b){if(!b.contentWindow.postMessage)return!1;a=JSON.stringify({method:a,value:c});b.contentWindow.postMessage(a,h)}function l(a){var c,b;try{c=JSON.parse(a.data),b=c.event||c.method}catch(e){}"ready"!=b||k||(k=!0);if(!/^https?:\/\/player.vimeo.com/.test(a.origin))return!1;"*"===h&&(h=a.origin);a=c.value;var m=c.data,f=""===f?null:c.player_id;c=f?d[f][b]:d[b];b=[];if(!c)return!1;void 0!==a&&b.push(a);m&&b.push(m);f&&b.push(f);
return 0<b.length?c.apply(null,b):c.call()}function n(a,c,b){b?(d[b]||(d[b]={}),d[b][a]=c):d[a]=c}var d={},k=!1,h="*";e.fn=e.prototype={element:null,init:function(a){"string"===typeof a&&(a=document.getElementById(a));this.element=a;return this},api:function(a,c){if(!this.element||!a)return!1;var b=this.element,d=""!==b.id?b.id:null,e=c&&c.constructor&&c.call&&c.apply?null:c,f=c&&c.constructor&&c.call&&c.apply?c:null;f&&n(a,f,d);g(a,e,b);return this},addEvent:function(a,c){if(!this.element)return!1;
var b=this.element,d=""!==b.id?b.id:null;n(a,c,d);"ready"!=a?g("addEventListener",a,b):"ready"==a&&k&&c.call(null,d);return this},removeEvent:function(a){if(!this.element)return!1;var c=this.element,b=""!==c.id?c.id:null;a:{if(b&&d[b]){if(!d[b][a]){b=!1;break a}d[b][a]=null}else{if(!d[a]){b=!1;break a}d[a]=null}b=!0}"ready"!=a&&b&&g("removeEventListener",a,c)}};e.fn.init.prototype=e.fn;window.addEventListener?window.addEventListener("message",l,!1):window.attachEvent("onmessage",l);return window.Froogaloop=
window.$f=e}();

 $(document).ready(function() {


    var pop_video_width = 568;
    var pop_video_height = 360;
    var pop_data_video = 'pause';
    var pop_modal_size = '';
    var hash_vid = window.location.hash;

    $("a[rel='popupVideo']").each( function(index) {
        
        //add events on click
        $(this).click( function(event) {
            event.stopPropagation();

            _attach_event_video_click(this);
            
            event.preventDefault();

        });
        
 
        //play the video based on the hash
        //check if the object has the hash defined
        if( $(this).attr('data-video-hash') && $(this).attr('data-video-hash') != '' ){
            if( hash_vid.toLowerCase() == '#' + $(this).attr('data-video-hash').toLowerCase() ){
                _attach_event_video_click(this);
            }
        }
        

    });


    function _attach_event_video_click(obj){

        obj = typeof obj !== 'undefined' ? obj : false;
        if(!obj){
            return false;
        }
        var hash_available = false;
        pop_video_width = 568;
        pop_video_height = 360;
        pop_data_video = 'pause';

        var autoplay = '';


        if( $(obj).attr('data-video-hash') && $(obj).attr('data-video-hash') != '' ){
            hash_available = true;
        }


        if( $(obj).attr('data-video') && $(obj).attr('data-video') != '' ){
            pop_data_video = $(obj).attr('data-video');
            if( $(obj).attr('data-video') == 'autoplay' ){
                autoplay = 'autoplay';
            }
        }

        //change the size
        if($(obj).attr('data-video-size') && $(obj).attr('data-video-size') != '' && $(obj).attr('data-video-size')  == 'lg'){
            pop_video_width = 920;
            pop_video_height = 550;
            pop_modal_size = 'modal-lg';
        }

        _video_paint_modal();
        $('#video-popcorn-modal .modal-body').empty();
        $('#video-popcorn-modal .modal-title').empty();



        if($(obj).attr('ref') && $(obj).attr('ref') != '') {
            $('#video-popcorn-modal .modal-title').html($(obj).attr('ref'));
        } else {
            $('#video-popcorn-modal .modal-title').html('Video');
        }




        var video_link = $(obj).attr("href"), video_type = _video_check_type(video_link);
        var controls = 'controls="controls" ' + autoplay;

        if(video_type == 'type="video/vimeo"') {
            controls = '';
            $('#video-popcorn-modal').addClass('no-controls');
        }

        var video_player = '<video width="'+pop_video_width+'" style="width: 100%;" allowFullScreen="true" height="'+pop_video_height+'" id="popcornvideoplayer_p" ' + controls + '>';
            video_player += '<source src="'+ video_link+ '" '+video_type+' />';
        video_player += '</video>';





        $('#video-popcorn-modal').on('show.bs.modal', function(e) {
            $('#video-popcorn-modal .modal-body').html('<div style="width:100%; height:100%; background:#000;"></div>')
            setTimeout(function (){
                $('#video-popcorn-modal .modal-body').html("<div id='popcornvideoplayer'>"+video_player+"</div>");
                _addMovieControll($(obj).attr('ref'));
            }, 500);
        });

        $('#video-popcorn-modal').modal('show').on('hidden.bs.modal', function(e) {

            $("#popcornvideoplayer").remove();

            if(hash_available && hash_vid.toLowerCase() == '#' + $(obj).attr('data-video-hash').toLowerCase() ){
                window.location.hash = '';
            }
        });
    }



    function _addMovieControll(ref){

        $('#popcornvideoplayer_p').mediaelementplayer({
            success: function(media, node, player) {
                var events = ['loadstart', 'play','pause', 'ended'];


                if(pop_data_video != 'autoplay'){

                    setTimeout(function () {
                        player.pause();
                    }, 250);

                }else{
                    player.play();
                }



                for (var i=0, il=events.length; i<il; i++) {

                    var eventName = events[i];

                    media.addEventListener(events[i], function(e) {


                        var status = 'Play'
                        switch(e.type) {
                            case 'play':
                                status = 'Play'
                                break;
                            case 'pause':
                                status = 'Pause'
                                break;
                            case 'ended':
                                status = 'Complete'
                                break;
                        }

                       ga('send', 'event', 'Video Player', status, ref);
                    });
                }


            }
        });


    }

    function _video_check_type(video_url){

        /*var regexp = /^(?:https?:\/\/)?(?:www\.)?youtube\.com\/watch\?(?=.*v=((\w|-){11}))(?:\S+)?$/;
        var matches_array = video_url.match(regexp);*/

        //var url = 'https://www.windriver.com/youtube_asdbahsd.mp4';
        var sources = ['youtube.com', 'vimeo.com'];
        var default_type = 'video/mp4';

        for(var i = 0; i < sources.length; i++) {
            if(video_url.indexOf(sources[i]) != -1) {
            var new_type = sources[i].replace('.com', '');
            default_type = 'type="video/' + new_type + '"';
          }
        }

        /*var type = 'type="video/mp4"';
        if(matches_array != null){
            type = 'type="video/youtube"';
        }*/

        return default_type;

    }


    function _video_paint_modal(){

        if( $("#video-popcorn-modal").length ){
            return;
        }

        var video_modal = '';
        var modal_height = pop_video_height+40;
        video_modal += '<div class="modal fade " id="video-popcorn-modal">';
            video_modal += '<div class="modal-dialog '+pop_modal_size+'">';
                video_modal += '<div class="modal-content">';
                    video_modal += '<div class="modal-header">';
                        video_modal += '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
                        video_modal += '<h4 class="modal-title"></h4>';
                    video_modal += '</div>';
                    video_modal += '<div class="modal-body" style="height: '+modal_height+'px;">';

                    video_modal += '</div>';
                    video_modal += '<div class="modal-footer">';
                    video_modal += '<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>';
                    video_modal += '</div>';
                video_modal += '</div>';
            video_modal += '</div>';
        video_modal += '</div>';

        $('body').append(video_modal);
    }
});