 (function(){ 'use strict';

  var app = angular.module('directives.SMPlayer', []);

  app.run(["$templateCache", function( $templateCache ){
    $templateCache.put('_sm.html','<div class="smplayer" id="sm2-container"><div class="smplayer-header"><div class="btn-group"><button class="btn-primary btn-small" ng-click="toggle()">toggle</button><button class="btn-primary btn-small" ng-click="stop()">stop</button></div><span ng-click="collapse = !collapse">PLAYLIST</span></div><ul class="smplayer-playlist" collapse="!collapse"><li class="smplayer-playlist-item" ng-repeat="item in items" ng-click="collapse = !collapse"><a ng-click="play(item.nr,\'assets/audio/{{item.url}}\')" ng-bind-html="item.nr+\'. \'+item.title">&nbsp;</a></li></ul></div>');
  }]);

  /**
   * @ngdoc directive
   * @name directives.SMPlayer:smPlayer
   * @restrict E
   * @element sm-player
   * @scope
   * @description 
   *   SoundManager Player Element
   *   
   <example module="directives.SMPlayer">
     <file name="index.html">
      <sm-player playlist="playlistItemsObject"></sm-playlist>
     </file>
     <file name="script.js">
      app.contoller('smPlayerDemoController',function($scope){
        $scope.items = [{
          nr: 1,
          title: 'Awesome music 1',
          url: 'path/to/awesomemusic.ogg'
        }]
      });
     </file>
   </example>
   */
  app.directive('smPlayer', function(){
    return {
      restrict: 'E',
      templateUrl: '_sm.html',
      scope: {playlist: '=playlist' },
      controller: ["$scope", "$element", "$attrs", "$window", function( $scope, $element, $attrs, $window ){
        var SoundManager = $window.soundManager; // global sm object
        $scope.items = $attrs.playlist;
        $scope.current = false;
        /**
         * @ngdoc method
         * @name play
         * @methodOf directives.SMPlayer:smPlayer
         * @param  {int|string} id  Playlist item ID
         * @param  {string} url Playlist item URL
         * @description
         *     Play an item
         */
        $scope.play = function( id, url ){
          $scope.current = {id:id,url:url};
          SoundManager.stopAll().createSound({'id':$scope.current.id,'url':$scope.current.url}).play();
        };
        /**
         * @ngdoc method
         * @name stop
         * @methodOf directives.SMPlayer:smPlayer
         * @description
         *     Stop current item
         */
        $scope.stop = function(){
          SoundManager.stop( $scope.current.id );
          $scope.current = false;
        };
        /**
         * @ngdoc method
         * @name toggle
         * @methodOf directives.SMPlayer:smPlayer
         * @description
         *     Toggle playing item
         */
        $scope.toggle = function(){
          if ( $scope.current !== false ){
            SoundManager.togglePause( $scope.current.id );}
          else {
            $scope.current = $scope.items[0];
            $scope.play( $scope.current.id, $scope.current.url );
          }
        };
      }]
    };
  });
 })();