
var chatscroll = new Object();

chatscroll.Pane = function(scrollContainerId){
    this.bottomThreshold = 20;
    this.scrollContainerId = scrollContainerId;
    this._lastScrollPosition = 100000000;
}

chatscroll.Pane.prototype.activeScroll = function(){

    var _ref = this;
    var scrollDiv = document.getElementById(this.scrollContainerId);
    var currentHeight = 0;

    var _getElementHeight = function(){
      var intHt = 0;
      if(scrollDiv.style.pixelHeight)intHt = scrollDiv.style.pixelHeight;
      else intHt = scrollDiv.offsetHeight;
      return parseInt(intHt);
    }

    var _hasUserScrolled = function(){
      if(_ref._lastScrollPosition == scrollDiv.scrollTop || _ref._lastScrollPosition == null){
        return false;
      }
      return true;
    }

    var _scrollIfInZone = function(){
		alert('fez');
      if( !_hasUserScrolled ||
          (currentHeight - scrollDiv.scrollTop - _getElementHeight() <= _ref.bottomThreshold)){
          scrollDiv.scrollTop = currentHeight;
          _ref._isUserActive = false;
      }
    }


    if (scrollDiv.scrollHeight > 0)currentHeight = scrollDiv.scrollHeight;
    else if(scrollDiv.offsetHeight > 0)currentHeight = scrollDiv.offsetHeight;

    _scrollIfInZone();

    _ref = null;
    scrollDiv = null;

}

function enviarMsg(nome, id){

    new Ajax.Request('/apps/chat/include/processAjax.php', {
        method: 'get',
        parameters : 'acao=escrever&txtmsg=' + encodeURIComponent($F('txtmsg')) + '&nome=' + nome + '&idsala=' + id,
        onComplete: function(request) {

            $('resultado').innerHTML = request.responseText;
            $('txtmsg').value = "";

        }

    });

}

function verificaEnter(e,nome,id){
    var keynum;

    if(window.event){ // IE
        keynum = e.keyCode;
    }else if(e.which){ // Netscape/Firefox/Opera
        keynum = e.which;
    }

    if (keynum == 13){

        enviarMsg(nome,id);

    }
}

function fechaChat(codigo,url){
    
    new Ajax.Request('/apps/chat/include/processAjax.php', {
        method: 'get',
        parameters : 'acao=fechachat&codusr=' + codigo,
        onComplete: function(request) {
            if (trim(request.responseText) != 'erro'){

                window.open(url,'_self');

            }else{

                alert('Só é possivel fechar salas quando não existe ninguem nelas!');

            }

        }
    });


}

function fechaSala(codigo){

    new Ajax.Request('/apps/chat/include/processAjax.php', {
        method: 'get',
        parameters : 'acao=fechachatusr&sala=' + codigo
    });

}

function iniciaPu(cod){

    new Ajax.PeriodicalUpdater('conversa', '/apps/chat/include/processAjax.php', {
        method: 'get',
        frequency: 3,
        decay: 0,
        parameters : 'acao=ler&idsala=' + cod
    });


}

function enviarMsgDir(id,nome){

    new Ajax.Request('/apps/chat/include/processAjax.php', {
        method: 'get',
        parameters : 'acao=escrever&txtmsg=' + nome + ' entrou na sala&nome=Administrador&idsala=' + id,
        onComplete: function(request) {

            $('resultado').innerHTML = request.responseText;
            $('txtmsg').value = "";

        }

    });

}
