/**
 * Created by Mariandi on 11/03/2014.
 */
/*global angular*/
angular.module('anguvideo', [])
    .directive("anguvideo", ['$sce', function ($sce) {
        return {
            restrict: 'EA',
            scope: {
                source: '=ngModel',
                width: '@',
                height: '@'
            },
            replace: true,
            template: '<div class="anguvideo">' +
                            '<iframe class="videoClass" type="text/html" width="{{width}}" height="{{height}}" ng-src="{{url}}" allowfullscreen frameborder="0"></iframe>' +
                      '</div>',
            link: function (scope) {
                var embedFriendlyUrl = "",
                    urlSections,
                    index;

                scope.$watch('source', function (newVal) {
                    if (newVal) {
                        /*
                        * Need to convert the urls into a friendly url that can be embedded and be used in the available online players the services have provided
                        * for youtube: src="//www.youtube.com/embed/{{video_id}}"
                        * for vimeo: src="http://player.vimeo.com/video/{{video_id}}
                        */

                        if (newVal.indexOf("vimeo") >= 0) { // Displaying a vimeo video
                            if (newVal.indexOf("player.vimeo") >= 0) {
                                embedFriendlyUrl = newVal;
                            } else {
                                embedFriendlyUrl = newVal.replace("https:", "http:");
                                urlSections = embedFriendlyUrl.split(".com/");
                                embedFriendlyUrl = embedFriendlyUrl.replace("vimeo", "player.vimeo");
                                embedFriendlyUrl = embedFriendlyUrl.replace("/" + urlSections[urlSections.length - 1], "/video/" + urlSections[urlSections.length - 1] + "");
                            }
                        } else if (newVal.indexOf("youtu.be") >= 0) {

                            index = newVal.indexOf(".be/");

                            embedFriendlyUrl = newVal.slice(index + 4, newVal.length);
                            embedFriendlyUrl = "http://www.youtube.com/embed/" + embedFriendlyUrl;

                        } else if (newVal.indexOf("youtube.com") >= 0) { // displaying a youtube video

                            if (newVal.indexOf("embed") >= 0) {
                                embedFriendlyUrl = newVal;
                            } else {
                                embedFriendlyUrl = newVal.replace("/watch?v=", "/embed/");
                            }
                        }

                        scope.url = $sce.trustAsResourceUrl(embedFriendlyUrl);
                        //console.log("done",  scope.url, embedFriendlyUrl);
                    }
                });
            }
        };
    }]);
@@ -633,16 +633,32 @@
 n (scope
+, element, attrs
 ) %7B%0A    
@@ -755,16 +755,134 @@
   index;
+%0A                %0A                var youtubeParams = (attrs.hideControls ? '?autoplay=0&showinfo=0&controls=0' : '');
 %0A%0A      
@@ -2395,16 +2395,32 @@
 endlyUrl
+ + youtubeParams
 ;%0A%0A     
@@ -2631,32 +2631,48 @@
 ndlyUrl = newVal
+ + youtubeParams
 ;%0A              
@@ -2783,16 +2783,32 @@
 embed/%22)
+ + youtubeParams
 ;%0A      
@@ -3089,8 +3089,9 @@
     %7D%5D);
+%0A

allow hiding youtube controls