// handler's queue

function HandlersQueue(){
  var handlers = [];
  var handlersLength = 0;
  this.log = function(){
    return;
  };
  this.get = function(ndx){
    if( ndx != null && ndx > -1 ) {
      return handlers[ndx];
    }
  };
  this.list = function(){
    return handlers;
  };
  this.add = function(priority, func){
    if( priority != null && func != null ) {
      handlers.push( { priority: priority, func: func } );
      handlersLength = handlers.length;
      handlers.sort(
        function( a, b ){
          if( a.priority < b.priority ) return -1;
          if( a.priority > b.priority ) return 1;
          return 0;
        }
      );
      return handlers;
    }
    return null;
  };
  this.remove = function(ndx){
    if( ndx == null ) {
      return null;
    }
    else if( ndx == 0 ) {
      handlers.shift();
    }
    else if( ndx == ( handlersLength - 1 ) ) {
      handlers.pop();
    }
    else {
      handlers.splice( ndx, 1 );
    }
    handlersLength = handlers.length;
    return handlers;
  };
  this.run = function(){
    for( var i = 0; i < handlersLength; i++ ) {
      handlers[i].func();
    }
  };
}
