$idCadastro = $id_cadastro;
$contNotf = parseInt($('.contNotificatacao').text()[0]);

var toSessid;

var $chat = null;

function setChat(chat)
{
    $chat = chat;
}

function getChat()
{
    return $chat;
}


//var broadcast_url = base_url.replace("http://", "").replace("/","");

var broadcast_url = base_url.replace("https://", "") + 'wss2/';

var protocol_ws = 'wss://';

/*if (base_url == "https://comdono.com/") 
{ 
    broadcast_url = 'comdono.com';
    protocol_ws = 'wss://';
}*/
 
var Broadcast = {
        	POST : "post", //"echo POST; ?>",
        	BROADCAST_URL : broadcast_url, // " echo BROADCAST_URL; ?>",
        	BROADCAST_PORT : "8080", //" echo BROADCAST_PORT; ?>",
    };
    
var Connection2 = (function(){
 
  function Connection2(url) {
        this.open = false;
        this.socket = new WebSocket(protocol_ws + url);
        this.setupConnectionEvents();
    }

  Connection2.prototype = {
    setupConnectionEvents : function () {
          var self = this;
          self.socket.onopen = function(evt) { self.connectionOpen(evt); };
          self.socket.onmessage = function(evt) { self.connectionMessage(evt); };
          self.socket.onclose = function(evt) { self.connectionClose(evt); };
      },
      connectionOpen : function(evt){
          this.open = true;
          this.addSystemMessage("Connected");
    },
      connectionMessage : function(evt){
          var data = JSON.parse(evt.data);
            
          this.addChatMessage(data);
      },
      connectionClose : function(evt){
          this.open = false;
          this.addSystemMessage("Disconnected");
      },
      sendMsg : function(message){
          this.socket.send(JSON.stringify({
              msg : message,
              sessid: getChat().sessid
          }));
      },
      loadNotification: function(message)
      {
        this.socket.send(JSON.stringify({
              msg : message,
              sessid: toSessid
          }));
      },
      atualizaNotificacoes: function(data)
      {
        atualizaNotificacoes();
        get_agenda();
      },
      addChatMessage : function(data){ 
        
        switch(data.broadType){
          case Broadcast.POST : this.addNewPost(data); break;
          case 'ini_chat' : this.addNewChat(data); break;
          case 'atualiza_notificacao' : this.atualizaNotificacoes(data); break;
          default : console.log("nothing to do");
        }
      },
        addNewChat : function(data){ 
            
            
            var res = data.data;
            ref = 'minha_conta/resumo';
            $nfHtml = '<a class="dropdown-item" href="'+base_url+ref+'"><div class="text f-left mr-5">Nova Solicitação</div><div class="box f-left"><span class="contNotNewChat">1</span></div></a>';
           
            if($idCadastro != res.id_cadastro && (($chat == null) || ($chat.id_chat != res.id_chat)))
            {
                console.log('addNewChat',res);
                $('.divSemNotificacao').hide();
                $('.divComNotificacao').show();
                $contNotf++;
                $('.contNotificatacao').text($contNotf);
                
                contNotNewChat = $('.contNotNewChat');
                
                

                divBlocosAcitaChat = $('.divBlocosAcitaChat')
                if(divBlocosAcitaChat.length > 0)
                {
                    $('.divInterecadosNegociar').show();
                    
                    //$('.divBlocosAcitaChat').append(res.divAceitaChat);
                    
                    
                    //$('.container-interessado').trigger('add.owl.carousel', [res.divAceitaChat]).trigger('refresh.owl.carousel');
                    
                    //console.log(owl.refreshed);
                    owl.trigger('add.owl.carousel', [res.divAceitaChat, 0]);
                    owl.trigger('refresh.owl.carousel');
                    //owl.refreshed;
                }
                
                if(contNotNewChat.length > 0)
                {
                    cont = parseInt(contNotNewChat.text());
                    cont++;
                    contNotNewChat.text(cont);
                }
                else
                {
                    if($idCadastro == res.id_dono)
                    {
                        $('.divNotifications').append($nfHtml);
                    }
                }
            }
          
          
        },
      addNewPost : function(data){
        var res = data.data;
        
        console.log(res);
        
        controler = $idCadastro == res.id_dono ?  'minhas_vendas': 'minhas_compras';
        ref = 'minha_conta/'+controler+'/negociacoes/'+res.id_imovel+'/'+res.id_chat;
        
        $nfHtml = '<a class="dropdown-item" href="'+base_url+ref+'"><div class="text f-left mr-5">Mensagem Chat '+res.id_chat+'</div><div class="box f-left"><span class="contNotfChat_'+res.id_chat+'">1</span></div></a>';
        
        divMsg = $('.msg_'+res.id_chat);
        if(divMsg.length > 0)
        {
            newHtml = $idCadastro == res.id_cadastro ?  res.rightHtml: res.leftHtml;
            $('.msg_'+res.id_chat).append(newHtml);
            showContent(res.id_chat, 'content_chat');
        }
            
        if($idCadastro != res.id_cadastro && (($chat == null) || ($chat.id_chat != res.id_chat)))
        {
            console.log('addNewPost');
            $('.divSemNotificacao').hide();
            $('.divComNotificacao').show();
            $contNotf++;
            $('.contNotificatacao').text($contNotf);
            contNotfChat = $('.contNotfChat_'+res.id_chat);
            if(contNotfChat.length > 0)
            {
                cont = parseInt(contNotfChat.text()[0]); 
                cont++;
                contNotfChat.text(cont);
            }
            else
            {
                if($idCadastro == res.id_dono || $idCadastro == res.id_interessado || $idCadastro == res.id_cartorio)
                {
                    $('.divNotifications').append($nfHtml);
                }
            }
        }/*
        if($idCadastro == res.id_cadastro)
        {
            setNotificationChat(res, ref);
        }*/
      },
      addSystemMessage : function(msg){
          // this.chatwindow.innerHTML += "<p>" + msg + "</p>";
      }
    };
    return Connection2;

})();


var conn = new Connection2(Broadcast.BROADCAST_URL+"?"+sessId);//$("#sessid").val()

//var conn = new Connection2("test.comdono.com/wss2/?"+sessId);//$("#sessid").val()

/*function setNotificationChat(res, ref)
{
    //`id_notificacao`, `id_cadastro`, `id_rel`, `name_id`, `tipo`, `href`, `text`, `class_spam_cont`, `cont`, `ntf_status`, `ntf_datetime`
    
    $dataNotification = {
        id_send: res.id_cadastro,
        ids: [
            res.id_cartorio,
            res.id_dono,
            res.id_interessado
            ],
        refs:[
            'minha_conta/cartorio/negociacoes/'+res.id_imovel+'/'+res.id_chat,
            'minha_conta/minhas_vendas/negociacoes/'+res.id_imovel+'/'+res.id_chat,
            'minha_conta/minhas_compras/negociacoes/'+res.id_imovel+'/'+res.id_chat,
            ],
        notification:{
            id_rel:          res.id_chat,
            name_id:         'id_chat',
            tipo:            'chat',
            href:             ref,
            text:            'Mensagem Chat '+res.id_chat,
            class_spam_cont: 'contNotfChat_'+res.id_chat,
                    }
    };
    
    $.ajax({
        url: base_url + "minha_conta/ajax/chat_ajax/set_notification",
        dataType: "json",
        type: "POST",
        data: $dataNotification,
        success: function(r)
        {
            console.log('set_notification', r);
        },
        error: function(error)
        {
            console.log();
        }
    });
}*/




















//////////////////////