// Assumptions:
//
// remoteViewPrefix[1-n] contain remote views

function ChatViews()
{

  if (typeof ChatViews.instance == 'undefined')
  {
    ChatViews.instance = this;
  }

  this.publishing = false;
  
  this.eventManager = EventManager;
  this.eventManager();

  this.selfView = null;
  this.setSelfView = function(selfView)
  {
    this.selfView = selfView;
  }

  this.remoteViewPrefix = null;
  this.setRemoteViewPrefix = function(remoteViewPrefix)
  {
    this.remoteViewPrefix = remoteViewPrefix;
  }

  this.publish = function(myName)
  {
    //directorTrace('ChatViews.publish');
    this.fireEventDescriptor(this.buildEventDescriptor('info_requestVideoCaptureStart','Start video capture',this.getSelfView()));
    Chat.publish(myName);
    this.publishing = true;  
  }

  this.receive = function(member,viewType)
  {
    viewType = viewType || this.getDefaultViewType();
    var view = Chat.allocateView(viewType);
    if (view != null)
    {
      var nm = view.getName();
      var viewId = nm.charAt(nm.length-1);
      if (viewId == '0') viewId = nm.charAt(nm.length-2) + viewId;
      this.fireEventDescriptor(this.buildEventDescriptor('info_receiveChatView',nm + ',' + member + ',' + viewId,view));
      view.chatLastResolutionChange = getTicks();
      Chat.play(view,member);
      view.chatReceiveName = member;
    }
    else
    {
      alert('No views available');
    }
  }

 this.close = function(viewId,viewType)
  {
    if (viewId > 0)
    {
      this.fireEventDescriptor(this.buildEventDescriptor('info_closeChatView',viewId,getActorByName(this.remoteViewPrefix + viewId)));
      eval(this.remoteViewPrefix + viewId + '.stop()');
      eval(this.remoteViewPrefix + viewId + '.closeChat()');
      Chat.releaseView(this.remoteViewPrefix + viewId,viewType);
    }
    else
    {
      if(!this.publishing) return;
      this.getSelfView().stop();
      this.getSelfView().closeChat();
      this.fireEventDescriptor(this.buildEventDescriptor('info_requestVideoCaptureStop','Stop video capture',this.getSelfView()));
      this.publishing = false;
    }
  }

  this.buildEventDescriptor = function(event,optionalArgs,actor)
  {
    return ContextEventDescriptor.create(event,actor.getContextIndex(),actor.getName(),optionalArgs);
  }
  
  this.isRemoteActive = function()
  {
    return Chat.isRemoteActive();
  }

  this.getSelfView = function()
  {
    return Chat.selfView;
  }

  this.viewType = '';
  this.setDefaultViewType = function(viewType)
  {
    this.viewType = viewType;
  }
  this.getDefaultViewType = function()
  {
    return this.viewType;
  }

  this.startAudioCapture = function() {Chat.startAudioCapture();}
  this.stopAudioCapture = function() {Chat.stopAudioCapture();}
  this.setAudioCaptureRate = function(bytesPerSecond) {Chat.setAudioCaptureRate(bytesPerSecond);}
  this.configureAudioCaptureRate = function(bytesPerSecond) {Chat.configureAudioCaptureRate(bytesPerSecond);}
}

ChatViews.theInstance = function()
{
  if (typeof ChatViews.instance == 'undefined')
  {
    new ChatViews();
  }
  return ChatViews.instance;
}


ChatViews.receive = function(member,view)
{
  ChatViews.theInstance().receive(member,view);
}

ChatViews.publish = function(name)
{
  ChatViews.theInstance().publish(name);
}

ChatViews.close = function(viewId,viewType)
{
  ChatViews.theInstance().close(viewId,viewType);
}

ChatViews.publishing = function()
{
return ChatViews.theInstance().publishing;
}

ChatViews.isRemoteActive = function()
{
  return ChatViews.theInstance().isRemoteActive();
}

ChatViews.createViewType = function (pels,lines)
{
  return '' + pels + 'x' + lines;
}

ChatViews.addView = function(actor)
{
  var viewType = ChatViews.createViewType(actor.getClipWidth(),actor.getClipHeight());
  if (typeof Chat.remoteView[viewType] == 'undefined') Chat.remoteView[viewType] = new Array();
  Chat.remoteView[viewType][actor.getName()] = actor;
}

ChatViews.startAudioCapture = function() {ChatViews.theInstance().startAudioCapture();}
ChatViews.stopAudioCapture = function() {ChatViews.theInstance().stopAudioCapture();}
ChatViews.setAudioCaptureRate = function(bytesPerSecond) {ChatViews.theInstance().setAudioCaptureRate(bytesPerSecond);}
ChatViews.configureAudioCaptureRate = function(bytesPerSecond) {ChatViews.theInstance().configureAudioCaptureRate(bytesPerSecond);}

