docs: function clickJoinOrLeaveVideoChat(isStopByInitiator, isStopByBadNetwork) {
      if(!client){
        initialise();
      }
  
      var messageText,
          btnState;
  
      $('#videos div.row').empty();
  
      // Stop
      if (callIsInProgress) {
          $('#call_btn').prop('disabled', "disabled");
  
          stopAllICEFailedTimers();
  
          client.leave({
              success: function() {
  
                  var next = function(){
                    client.destroySession({
                        success: function() {
                            clearEventsListeners();
  
                            $('.videoconf_ids').text(null);
  
                            if (isInitiator) {
                                if(isStopByBadNetwork){
                                  notifyAboutCallState('dropCall');
                                }else{
                                  notifyAboutCallState('stopCall');
                                }
                                messageText = 'Has finished video chat';
                                btnState = 'start';
                                isInitiator = false;
  
                                changeVideoChatStatus(currentDialog._id, false);
                            } else {
                                messageText = 'Has left video chat';
                                btnState = isStopByInitiator ? 'start' : 'join';
                            }
  
                            sendCallPresence(messageText);
                            updateVideoAndAudioChatButton(btnState);
  
                            // update UI
                            $("#video_chat_col").hide("slow", function() {
                                $("#chat_history_col").show("fast", function() {
                                    setTimeout(function() {
                                        $('#call_btn').prop('disabled', null);
                                    }, 1000);
  
                                    callIsInProgress = false;
                                });
                            });
                        },
                        error: function(error) {
                            console.warn("Error in 'destroySession': ", error);
                        }
                    });
                  };
  
                  client.detachVideoConferencingPlugin({
                      success: function() {
                          console.warn("Success detachVideoConferencingPlugin");
                          next();
                      },
                      error: function(error) {
                          console.warn("Error in 'detachVideoConferencingPlugin': ", error);
                          next();
                      }
                  });
  
              }
          });
  
  
      // Start
      } else {
          if(!currentDialog){
            bootbox.alert("Please create a chat dialog first");
            return;
          }
  
          $('#call_btn').prop('disabled', "disabled");
  
          // Init Video engine
          //
          client.createSession({
              success: function() {
  
                  initEventsListeners();
  
                  // If this is Safari, let's avoid video
                  if(adapter.browserDetails.browser === "safari") {
                      toastr.warning("Our video stream has been rejected because Safari does not support VP8 code. Viewers won't see us, only listen.");
                  }
  
                  client.attachVideoConferencingPlugin(false, null, {
                      success: function() {
  
                          actionsForTheInitiator();
  
                          $('.videoconf_ids').text("SID: " + client.getSessionId() + ". PID: " + client.getPluginId());
  
                          $("#chat_history_col").hide("slow", function() {
                              $("#video_chat_col").show("fast", function() {
  
                                  // show local video feed
                                  addFeedView(currentUser.id, true);
  
                                  // join video chat
                                  var roomIdToJoin = currentDialog._id;
  
                                  client.join(roomIdToJoin, currentUser.id, isAudioCallOnly, {
                                      success: function() {
                                          console.log("JOINED " + client.currentDialogId + " dialog");
  
                                          if (isInitiator) {
                                              messageText = 'Started call';
                                              btnState = 'end';
  
                                              changeVideoChatStatus(currentDialog._id, true);
                                          } else {
                                              messageText = 'Joined call';
                                              btnState = 'leave';
                                          }
  
                                          sendCallPresence(messageText);
                                          updateVideoAndAudioChatButton(btnState);
  
                                          setTimeout(function() {
                                              $('#call_btn').prop('disabled', null);
                                          }, 1000);
  
                                          callIsInProgress = true;
  
                                          //
                                          // // TEMP
                                          // setTimeout(function() {
                                          //   clickJoinOrLeaveVideoChat(false, true);
                                          //
                                          //   bootbox.alert("TEST: The connectivity is gone because of heavy data loss. Please rejoin the call.");
                                          // }, 15000);
                                      },
                                      error: function(error) {
                                          bootbox.alert("Can't join video chat. Please check your Internet connection");
                                      }
                                  });
  
                              });
                          });
  
                      },
                      error: function(error) {
                          console.error("Error in 'attachVideoConferencingPlugin': ", error);
  
                          // #define VIDEOROOM_ERROR_ID_EXISTS			436
                          // #define VIDEOROOM_ERROR_UNAUTHORIZED		433
                          // #define 'User ID xxx already exists'		400
                          //
                          if (error["error_code"] == 433 || error["error_code"] == 436 || error["error_code"] == 400) {
                              clickJoinOrLeaveVideoChat();
                          }
  
                          bootbox.alert("Error attaching plugin... " + (error["error"] || error));
                      },
                      webrtcState: function(on) {
                          console.info("LOCAL WebRTC PeerConnection is " + (on ? "up" : "down") + " now");
  
                          if(on){
                            QBVideoConferencingClient.listVideoinputDevices(function(videoinputDevices){
                              userVideoinputDevices = videoinputDevices;
                              readyLocalVideoUI(videoinputDevices);
                            });
                          }
                      },
                      consentDialog: function(on) {
                          console.info("Consent dialog should be " + (on ? "on" : "off") + " now");
                      },
                      mediaState: function(medium, on) {
                          console.info("LOCAL MediaState (own):  " + (on ? "started" : "stopped") + " receiving our " + medium);
  
                          // Trying to fix 'ICE failed' error.
                          // If it's happened - then close current session.
                          if(!on){
                            if(medium == "audio"){
                              runICEFailedTimer(medium);
                            }
                          }else{
                            if(medium == "audio"){
                              stopICEFailedTimer(medium);
                            }
                          }
                      },
                      slowLink: function(uplink, nacks) {
                        console.warn("LOCAL slowLink detected. uplink: " + uplink + ". nacks: " + nacks);
                        toastr.warning("slow link detected");
                      },
                      iceState: function(iceConnectionState){
                        console.warn("LOCAL iceState: " + iceConnectionState);
  
                        if(iceConnectionState == "disconnected" || iceConnectionState == "checking"){
                          playSoundOnNewMessage()
  
                          startSpiner("local");
                          hideLocalVideo();
                        }else if (iceConnectionState == "connected"){
                          playSoundOnNewMessage()
  
                          stopSpiner("local");
                          showLocalVideo();
                        }else if (iceConnectionState == "failed"){
                          playSoundOnNewMessage()
  
                          console.error("Local peer connection has failed because of ICE failed.");
                          console.info("Trying Local ICE restart...");
  
                          // perform ICE restart
                          //
                          client.iceRestart({
                            success: function() {
                              // console.info("iceRestart success");
                            },
                            error: function(error) {
                              console.warn("iceRestart error: " + JSON.stringify(error));
                            }
                          });
                        }
                      },
                      oncleanup: function() {
  
                      }
                  });
  
              },
  
              error: function(error) {
                  console.error("Error in 'createSession': ", error);
  
                  $('#call_btn').prop('disabled', null);
  
                  if(error == "Lost connection to the gateway (is it down?)"){
  
                  }else if(error == "Error connecting to the Janus WebSockets server: Is the gateway down?"){
                    if (callIsInProgress) {
                        clickJoinOrLeaveVideoChat();
                    }
                    bootbox.alert("Lost connection to the server. Please check your Internet connection and restart the app.");
                  }
              },
  
              destroyed: function() {
                  console.info("Session is destroyed");
              },
  
              timeoutSessionCallback: function() {
                  console.warn("timeoutSessionCallback CALL!!!!");
                  bootbox.alert("Server session timeout (because of bad Internet connection). Please check your Internet connection and restart the app");
                  clickJoinOrLeaveVideoChat(false, true);
              }
          });
  
      }
  }