

var qpHelpCenterChat = {
        
  window: {
    width:   540,
    height:  500
  },
  
  listener: {
    main:     null,
    members:  null,
    interval: 1000
  },
  
  
  init: function(enviroment) {
    
    this.addHandlers();
    $('#qp_chat_message').focus();
    
    switch(enviroment)
    {
      case 'frontend':
        this.addFrontendHandlers();
        break;
      case 'backend':
        this.addBackendHandlers();
        break;
    }
  },
  
  addHandlers: function() {
    form = $('.qp_admin-help_center-chat-messenger FORM');
    chat_id = form.find('#qp_chat_id').val();
    uri = form.attr('action');

    form.submit(function() {
          
      message = form.find('#qp_chat_message').val();
      public_message = form.find('input[name="qp_chat[public]"]:checked').val();
      qpHelpCenterChat.sendMessage(uri, chat_id, message, public_message);
            
      form.find('#qp_chat_message').val('');
      return false;
    });
    
  },
  
  addFrontendHandlers: function() {
  },
   
  addBackendHandlers: function() {
    $('.qp_admin-help_center-chat-members H1').css('cursor', 'pointer');
    $('.qp_admin-help_center-chat-members H1').click(function() {
      $(this).parent().find('UL').slideToggle();
    });
  },
    
  startChat: function(uri) {
    leftVal = (screen.width - this.window.width) / 2;
    topVal =  (screen.height - this.window.height) / 2 - 40;
    window.open (uri, 
      'qp_help_center_chat', 
      'status=1, toolbar=1, scrollbars=1, width='+ this.window.width + 
      ', height=' + this.window.height + 
      ', top=' + topVal + 
      ', left=' + leftVal);
  },
    
    
  joinChat: function(uri) {
    leftVal = (screen.width - this.window.width) / 2;
    topVal =  (screen.height - this.window.height) / 2 - 40;
    uri = uri+'';
    var id = 0;
    var pos = uri.lastIndexOf('chat_id');
    if (pos >= 0){
        id = parseInt(uri.substr(pos+8));
    }
    if (!id){
        id = 1;
    }
    window.open (uri, 
      'qp_admin_help_center_window_'+id,
      'status=1, toolbar=1, scrollbars=1, width='+ this.window.width + 
      ', height=' + this.window.height + 
      ', top=' + topVal + 
      ', left=' + leftVal);
  },

  sendMessage: function(uri, chat_id, message, public_message ) {
      
    if(!chat_id || !message)
      return;
      
    $.ajax({
      url:  uri,
      cache: false,
      data: 'chat_id=' + chat_id + '&message=' + encodeURIComponent(message) + '&public_message=' + encodeURIComponent(public_message),
      error:    function(data) {
        alert('Não foi possível cancelar a chamada!\n' +
          'Verifique sua conexão e tente novamente.'
        );
      }
    });
  },
  
  listen: function(uri)
  {
    this.listener.main = setInterval(function() { 
      qpHelpCenterChat.updateChat(uri); 
    }, this.listener.interval);
  },
  
  
  listenMembers: function(uri)
  {
    this.listener.members = setInterval(function() { 
      qpHelpCenterChat.updateMemberList(uri); 
    }, this.listener.interval * 3);
  },
  
  
  updateChat: function(uri)
  {
    $.ajax({
      url:   uri,
      cache: false,
      success: function(data) {
        chat = $('.qp_admin-help_center-chat-container');
        
        if(chat.html() != data) {
          chat.html(data);
          chat.attr('scrollTop', chat.attr('scrollHeight'));
        }
      }
    });
  },
  
  
  updateMemberList: function(uri)
  {
    $.ajax({
      url:   uri,
      cache: false,
      success: function(data) {
        container = $('.qp_admin-help_center-chat-members UL');
        container.html(data);
      }
    });
  },
  
  exitChat: function(uri)
  {
    if(confirm("Você tem certeza que deseja encerrar esta chamada ?"))
      $.ajax({
        url:     uri,
        cache: false,
        success: function() {
          window.close();
        }
      });
  }
  
};
