function NETVIJACall(id) {
  this.WAITING = 1;
  this.IN_PROGRESS = 2;
  this.DONE = 3;
  this.CANCELED = 4;

  this.id = id;
  this.status = this.WAITING;
  
  NETVIJA.calls.push(this);
  
  this.place = function(from_id, from_name, to_id, to_name, session_id) {
    var flashvars = {
      "uname": from_name,
      "uid": from_id,
      "call_uid": to_id,
      "guestname": to_name,
      "guestid": to_id
    };
    if(session_id != undefined) flashvars.session_id = session_id;
    this.call(flashvars);
  };

  this.answer = function(from_id, from_name, to_id, to_name, to_key, call_id, session_id) {
    this.id = call_id;

    var flashvars = {
      "uname": from_name,
      "uid": from_id,
      "call_key": to_key,
      "call_id": call_id,
      "guestname": to_name,
      "guestid": to_id,
      "mystream":'receiver_'+call_id,
      "gueststream":'caller_'+call_id
    };
    if(session_id != undefined) flashvars.session_id = session_id;
    this.call(flashvars);
  };

  this.call = function(flashvars) {

    if (typeof console != "undefined") console.log('calling');
    if (typeof console != "undefined") console.log(flashvars);

    NETVIJAUser.updateUserStatus(NETVIJAUser.BUSY);

    flashvars.callurl = NETVIJA.routes.createCall;
    flashvars.callsetstatusurl = NETVIJA.routes.setCallStatus;
    flashvars.callgetstatusurl = NETVIJA.routes.getCallStatus;
    if(NETVIJA.routes.startSession != undefined) flashvars.sessionstarturl = NETVIJA.routes.startSession;
    if(NETVIJA.routes.setSessionStatus != undefined) flashvars.sessionsetstatusurl = NETVIJA.routes.setSessionStatus;
    if(NETVIJA.routes.stopSession != undefined) flashvars.sessionstopurl = NETVIJA.routes.stopSession;

    if(!YAHOO.util.Dom.inDocument('call_dialog')) {
      var container = document.createElement('div');
      container.id = 'call_dialog';
      container.innerHTML = '<div class="hd"></div><div class="bd"><div style="text-align:center"><div id="netvijaclient_video"></div><div></div><div class="ft"></div>';
      YAHOO.util.Dom.addClass(container,'message');
    }

    var height = YAHOO.util.Dom.getViewportHeight();
    YAHOO.util.Dom.setStyle(container, 'top', (Math.max(0,height-600)/2)+'px');
    YAHOO.util.Dom.setStyle(container, 'display', 'block');
    NETVIJA.callDialog = new YAHOO.widget.Panel(container, {
      effect:{
        effect:YAHOO.widget.ContainerEffect.FADE,
        duration:0.25
      },
      fixedcenter:false,
      modal:true,
      visible:false,
      draggable:false,
      constraintoviewport: false,
      underlay: 'none',
      height: '670px'
    });

    var params = {
      menu: "false",
      scale: "noScale",
      allowFullscreen: "true",
      allowScriptAccess: "always",
      bgcolor: "#EFEFEF"
    };
    var attributes = {
      id:"netvijaclient"
    };
    NETVIJA.callDialog.render(document.body);
    swfobject.embedSWF("/netvijaclient.swf", "netvijaclient_video", "964", "600", "10.0.0", "/expressInstall.swf", flashvars, params, attributes);
    NETVIJA.callDialog.render(document.body);
    NETVIJA.callDialog.show();

    YAHOO.util.Event.on(YAHOO.util.Dom.getElementsByClassName("container-close", "a", "call_dialog").pop(), "click", function(e, obj) {
        if (typeof console != "undefined") console.log('closing dialog');
        obj.hangUp();
    }, this);
  };

  this.hangUp = function () {
    if (typeof console != "undefined") console.log('hanging up');
    NETVIJA.callDialog.hide();
    if (typeof console != "undefined") console.log('removing flash');
    swfobject.removeSWF('netvijaclient_video');
    window.setTimeout(function() {
        NETVIJA.callDialog.destroy();
        NETVIJA.callDialog = null;
    }, 500);
    this.setCallStatus(this.DONE);
    this.onFinished();
  };

  this.setCallStatus = function(status) {
    this.status = status;
    var transaction = YAHOO.util.Connect.asyncRequest('POST', NETVIJA.routes.setCallStatus, null, "id="+this.id+"&status="+status);
  }

  this.onFinished = function() {
    NETVIJAUser.updateUserStatus(NETVIJAUser.ONLINE);
    NETVIJAUser.checkUserStatus();
  }
}

