'use strict';
/**
 * Implements directive for video player. Use flow player or YouTube.
 */
angular.module('qusion.ui.player', [])
    .directive('qnPlayer', [
        function () {
            return {
                require: '?ngModel',
                link: function (scope, element, attr, ngModel) {

                    var player = null;

                    scope.$watch('video', function (value) {
                        if (value != null) {
                            console.log(value);
                            if (value.Extension == 'youtube') {
                                element.children().remove();
                               element.append('<iframe width="640" height="360" src="http://www.youtube.com/embed/'+value.FileName+'" frameborder="0" allowfullscreen></iframe>');// text(embed);
                            } else {
                                if (player == null) {
                                    var format = value.Extension.substr(1);

                                    var clip = {};
                                    clip[format] = "/qusion/video/stream/" + value.ID;
                                    player = element.flowplayer({
                                        swf: "/areas/qusion/scripts/vendor/widgets/flowplayer.swf",
                                        playlist: [
                                            [clip]
                                        ]
                                });
                                }
                            }
                            
                        }
                    });
                    
                    
                }
            };
        }
    ]);