
 $(document).ready(function() {


    var pop_video_height = 568;
    var pop_video_width = 360;
    var pop_data_video = 'pause';
    var pop_modal_size = '';

    $("a[rel='popupVideo']").click( function(event) {
        event.stopPropagation();

        pop_video_height = 568;
        pop_video_width = 360;
        pop_data_video = 'pause';
        var autoplay = '';


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

        //change the size
        if($(this).attr('data-video-size') && $(this).attr('data-video-size') != '' && $(this).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($(this).attr('ref') && $(this).attr('ref') != '') {
            $('#video-popcorn-modal .modal-title').html($(this).attr('ref'));
        } else {
            $('#video-popcorn-modal .modal-title').html('Video');
        }




        var video_link = $(this).attr("href"), video_type = _video_check_type(video_link);

        var video_player = '<video width="'+pop_video_width+'" style="width: 100%;" allowFullScreen="true" height="'+pop_video_height+'" id="popcornvideoplayer_p" controls="controls" '+autoplay+' >';
            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($(this).attr('ref'));
            }, 500);
        });

        $('#video-popcorn-modal').modal('show').on('hidden.bs.modal', function(e) {
            $("#popcornvideoplayer").remove();
        });



        event.preventDefault();

    });



    function _addMovieControll(ref){

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

                setTimeout(function () {
                    player.play();
                }, 150);


                if(pop_data_video != 'autoplay'){

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

                }



                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 type = 'type="video/mp4"';
        if(matches_array != null){
            type = 'type="video/youtube"';
        }

        return 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);
    }
});