/**
 * BMotionWeb Execute Method Handler
 *
 */
define([
  'angular',
  'jquery',
  'bms.func',
  'bms.modal',
  'bms.ws',
  'bms.session',
  'bms.visualization'
], function(angular, $, bms) {

  return angular.module('bms.handlers.method', ['bms.modal', 'bms.ws', 'bms.session', 'bms.visualization'])
    .factory('methodEvent', ['ws', '$q', 'bmsModalService', 'bmsVisualizationService', 'bmsWsService',
      function(ws, $q, bmsModalService, bmsVisualizationService, bmsWsService) {
        'use strict';

        var event = function(view, options) {
          this.id = bms.uuid();
          this.view = view;
          this.options = this.getDefaultOptions(options);
        };

        event.prototype.getDefaultOptions = function(options) {
          return angular.merge({
            name: "",
            args: [],
            callback: function() {},
            cause: "AnimationChanged"
          }, options);
        };

        event.prototype.getId = function() {
          return this.id;
        };

        event.prototype.shouldBeChecked = function() {
          var self = this;
          var session = self.view.session;
          if (session.isBVisualization()) {
            if (session.toolData.model !== undefined) {
              var refinements = session.toolData.model.refinements;
              if (refinements) {
                return self.options.refinement ? bms.inArray(self.options.refinement, refinements) : true;
              }
            }
          }
          return true;
        };

        event.prototype.setup = function() {

          var defer = $q.defer();

          var self = this;

          var element;

          // Determine graphical element of observer
          if (self.options.element !== undefined) {
            element = self.options.element;
          } else if (self.options.selector !== undefined) {
            var container = self.view.container.contents();
            element = container.find(self.options.selector);
          }

          if (element instanceof $) {

            element.each(function() {

              var ele = $(this);
              ele.css('cursor', 'pointer');
              ele.click(function(event) {

                var normalized = bms.normalize(self.options, ['callback'], ele);
                bmsWsService.callMethod(self.view.session.id, normalized.name, normalized.args)
                  .then(function(res) {
                    normalized.callback.call(this, ele, res);
                    defer.resolve();
                  }, function(err) {
                    d.reject(err);
                  });

              });

            });

          } else {
            defer.reject("Please specify a selector or an element for execute method handler.");
          }

          defer.resolve();

          return defer.promise;

        };

        return event;

      }
    ]);

});

@@ -154,17 +154,33 @@
 ization'
+,%0A  'bms.common'
 %0A
-
 %5D, funct
@@ -308,16 +308,30 @@
 ization'
+, 'bms.common'
 %5D)%0A    .
@@ -427,16 +427,35 @@
 ervice',
+ 'bmsErrorService',
 %0A      f
@@ -524,16 +524,33 @@
 sService
+, bmsErrorService
 ) %7B%0A    
@@ -2545,45 +2545,8 @@
 s);%0A
-                    defer.resolve();%0A
     
@@ -2602,15 +2602,28 @@
     
-d.rejec
+bmsErrorService.prin
 t(er

print errors in execute method handler