﻿/// <reference name="MicrosoftAjax.js"/>
Type.registerNamespace("aorta.playmeweb.webcontrols");

aorta.playmeweb.webcontrols.Comentarios = function() {
    aorta.playmeweb.webcontrols.Comentarios.initializeBase(this);

    this._botaoGatilho = null;
    this._botaoFecharLista = null;
    this._botaoFecharCadastro = null;
    this._botaoInserir = null;
    this._botaoEnviar = null;
    this._botaoCancelar = null;
    this._campoComentario = null;
    this._player = null;
    this._musicaComentario = null;

    this._comentarClickDelegate = null;
    this._fecharListaClickDelegate = null;
    this._fecharCadastroClickDelegate = null;
    this._inserirClickDelegate = null;
    this._enviarClickDelegate = null;
    this._cancelarClickDelegate = null;
    this._campoComentarioKeyUpDelegate = null;
    this._campoComentarioKeyDownDelegate = null;
    this._player_onShowMusicInfoDelegate = null;
    this._enviarSucessoCallbackDelegate = null;
    this._enviarFalhaCallbackDelegate = null;
    this._obterUltimoComentarioSucessoCallbackDelegate = null;
    this._obterUltimoComentarioFalhaCallbackDelegate = null;
}

aorta.playmeweb.webcontrols.Comentarios.prototype = {
    initialize: function() {
        aorta.playmeweb.webcontrols.Comentarios.callBaseMethod(this, 'initialize');

        this._comentarClickDelegate = Function.createDelegate(this, this._comentarClickHandler);
        this._fecharListaClickDelegate = Function.createDelegate(this, this._fecharListaClickHandler);
        this._fecharCadastroClickDelegate = Function.createDelegate(this, this._fecharCadastroClickHandler);
        this._inserirClickDelegate = Function.createDelegate(this, this._inserirClickHandler);
        this._enviarClickDelegate = Function.createDelegate(this, this._enviarClickHandler);
        this._cancelarClickDelegate = Function.createDelegate(this, this._cancelarClickHandler);
        this._campoComentarioKeyUpDelegate = Function.createDelegate(this, this._campoComentarioKeyUpHandler);
        this._campoComentarioKeyDownDelegate = Function.createDelegate(this, this._campoComentarioKeyDownHandler);
        this._player_onShowMusicInfoDelegate = Function.createDelegate(this, this._player_onShowMusicInfoHandler);

        this._obterUltimoComentarioSucessoCallbackDelegate = Function.createDelegate(this, this._obterUltimoComentarioSucessoRetornoHandler);
        this._obterUltimoComentarioFalhaCallbackDelegate = Function.createDelegate(this, this._obterUltimoComentarioFalhaRetornoHandler);
        this._enviarSucessoCallbackDelegate = Function.createDelegate(this, this._enviarSucessoRetornoHandler);
        this._enviarFalhaCallbackDelegate = Function.createDelegate(this, this._enviarFalhaRetornoHandler);

        this._player.add_showMusicInfo(this._player_onShowMusicInfoDelegate);

        if(this.get_divLista())
            this.get_divLista().style.display = 'none';

        if(this.get_divCadastro())
            this.get_divCadastro().style.display = 'none';

        if(this.get_spanQtdCharDigitados())
            this.get_spanQtdCharDigitados().innerHTML = 0;
            
        if(this.get_spanQtdCharRestantes())
            this.get_spanQtdCharRestantes().innerHTML = this.get_qtdMaxCampoComentario();

        if (this._campoComentario){
          $addHandler(this._campoComentario, 'keyup', this._campoComentarioKeyUpDelegate);
          $addHandler(this._campoComentario, 'keydown', this._campoComentarioKeyDownDelegate);
        }

        if (this._botaoGatilho){
          $addHandler(this._botaoGatilho, 'click', this._comentarClickDelegate);
          this.setCursor(this._botaoGatilho, 'pointer');
        }

        if (this._botaoFecharLista){
          $addHandler(this._botaoFecharLista, 'click', this._fecharListaClickDelegate);
          this.setCursor(this._botaoFecharLista, 'pointer');
        }

        if (this._botaoFecharCadastro){
          $addHandler(this._botaoFecharCadastro, 'click', this._fecharCadastroClickDelegate);
          this.setCursor(this._botaoFecharCadastro, 'pointer');
        }

        if (this._botaoInserir){
          $addHandler(this._botaoInserir, 'click', this._inserirClickDelegate);
          this.setCursor(this._botaoInserir, 'pointer');
        }

        if (this._botaoEnviar){
          $addHandler(this._botaoEnviar, 'click', this._enviarClickDelegate);
          this.setCursor(this._botaoEnviar, 'pointer');
        }

        if (this._botaoCancelar){
          $addHandler(this._botaoCancelar, 'click', this._cancelarClickDelegate);
          this.setCursor(this._botaoCancelar, 'pointer');
        }
    },

    dispose: function() {
        aorta.playmeweb.webcontrols.Comentarios.callBaseMethod(this, 'dispose');

        this._player.remove_showMusicInfo(this._player_onShowMusicInfoDelegate);

        if (this._campoComentario)
          $clearHandlers(this._campoComentario);

        if (this._botaoGatilho)
          $clearHandlers(this._botaoGatilho);

        if (this._botaoFecharLista)
          $clearHandlers(this._botaoFecharLista);

        if (this._botaoFecharCadastro)
          $clearHandlers(this._botaoFecharCadastro);

        if (this._botaoInserir)
          $clearHandlers(this._botaoInserir);

        if (this._botaoEnviar)
          $clearHandlers(this._botaoEnviar);

        if (this._botaoCancelar)
          $clearHandlers(this._botaoCancelar);
    },
    
    _player_onShowMusicInfoHandler: function(sender, args) {
        //Atualiza as informações da música
        var musica = this.get_player().get_musica();

        if (musica) {
            aorta.playmeweb.webcontrols.services.Comentarios.ObterUltimoComentario(this.get_player().get_canal(), musica, this._obterUltimoComentarioSucessoCallbackDelegate, this._obterUltimoComentarioFalhaCallbackDelegate);

            this.get_divCarregando().style.display = 'block';
            if (this.get_campoTituloListagem())
                this.get_campoTituloListagem().innerHTML = musica.Nome;
            eval(this.get_funcaoPost());
        }
    },
    
    _obterUltimoComentarioSucessoRetornoHandler: function(comentario){
        if(this.get_divUltimoComentario()){
            if(comentario){
                var c = comentario.Comentario;
                if(c.length > this.get_qtdCharUltimoComentario())
                    c = c.substr(0, this.get_qtdCharUltimoComentario()) + '...';
                this.get_divUltimoComentario().innerHTML = c;
                this.get_divUltimoComentario().style.display = 'block';
            }else{
                this.get_divUltimoComentario().innerHTML = 'Ainda não comentou?<br/>Comente!';
                this.get_divUltimoComentario().style.display = 'block';
            }
        }
    },

    _obterUltimoComentarioFalhaRetornoHandler: function(erro, contexto, funcaoErro){
        if(erro != null)
            Sys.Debug.trace('Ocorreu o seguinte erro na função \'' + funcaoErro + '\': ' + erro.get_message())

        if(this.get_divUltimoComentario())
            this.get_divUltimoComentario().style.display = 'none';
    },
  
    _contadorCaracteres: function(txtArea, qtd, digitados, restantes){   
        var tam = txtArea.value.length;
        var str = "";
        str = str + tam;
        
        digitados.innerHTML = str;
        restantes.innerHTML = eval(qtd - str);                
        
        if(tam > qtd){
            aux = txtArea.value;
            txtArea.value = aux.substring(0, qtd);
            
            digitados.innerHTML = qtd;
            restantes.innerHTML = 0;
        }                                
    },
    
    _campoComentarioKeyUpHandler: function(){
        if(this.get_spanQtdCharDigitados() && this.get_spanQtdCharRestantes())        
            this._contadorCaracteres(this.get_campoComentario(), this.get_qtdMaxCampoComentario(), this.get_spanQtdCharDigitados(), this.get_spanQtdCharRestantes());        
    },

    _campoComentarioKeyDownHandler: function(){    
        if(this.get_spanQtdCharDigitados() && this.get_spanQtdCharRestantes())        
            this._contadorCaracteres(this.get_campoComentario(), this.get_qtdMaxCampoComentario(), this.get_spanQtdCharDigitados(), this.get_spanQtdCharRestantes());
    },

    _comentarClickHandler: function(){
        if(this.get_divLista())
            this.get_divLista().style.display = 'block';
    },

    _fecharListaClickHandler: function(){
        if(this.get_divLista())
            this.get_divLista().style.display = 'none';
    },

    _fecharCadastroClickHandler: function(){
        if(this.get_divLista())
            this.get_divLista().style.display = 'none';

        if(this.get_divCadastro())
            this.get_divCadastro().style.display = 'none';
    },

    _inserirClickHandler: function(){
        if(this.get_divLista())
            this.get_divLista().style.display = 'none';

        var musica = this.get_player().get_musica();
        
        if(musica){
            this._musicaComentario = musica;
            if(this.get_campoTituloCadastro()){    
                this.get_campoTituloCadastro().innerHTML = musica.Nome;
            }
        }

        if(this.get_divCadastro())
            this.get_divCadastro().style.display = 'block';
    },

    _enviarClickHandler: function(){
        var validation = Page_ClientValidate();
        if(validation){
            var comentario = this.get_comentario();
            comentario.Titulo = this.get_campoTitulo().value;
            comentario.Comentario = this.get_campoComentario().value;
            comentario.Nome = this.get_campoNome().value;
            comentario.Email = this.get_campoEmail().value;

            aorta.playmeweb.webcontrols.services.Comentarios.ComentarMusica(this.get_player().get_canal(), this._musicaComentario, comentario, this._enviarSucessoCallbackDelegate, this._enviarFalhaCallbackDelegate);
        }
    },

    _cancelarClickHandler: function(){
        if(this.get_divLista())
            this.get_divLista().style.display = 'block';

        if(this.get_divCadastro())
            this.get_divCadastro().style.display = 'none';
    },

    _enviarSucessoRetornoHandler: function(){
        if(this.get_divLista())
            this.get_divLista().style.display = 'block';

        if(this.get_divCadastro())
            this.get_divCadastro().style.display = 'none';

        if(this.get_divMensagemSucesso())
            this.get_divMensagemSucesso().style.display = 'block';

        this.get_campoTitulo().value = '';
        this.get_campoComentario().value = '';
        this.get_campoNome().value = '';
        this.get_campoEmail().value = '';

        if(this.get_spanQtdCharDigitados())
            this.get_spanQtdCharDigitados().innerHTML = 0;
            
        if(this.get_spanQtdCharRestantes())
            this.get_spanQtdCharRestantes().innerHTML = this.get_qtdMaxCampoComentario();
    },

    _enviarFalhaRetornoHandler: function(erro, contexto, funcaoErro){
        if(erro != null)
            Sys.Debug.trace('Ocorreu o seguinte erro na função \'' + funcaoErro + '\': ' + erro.get_message())

        alert('Ocorreu um problema ao tentar inserir o comentário.\nAguarde alguns minutos e tente novamente.');
    },

    get_botaoGatilho: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.botaoGatilho"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._botaoGatilho;
    },
    set_botaoGatilho: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._botaoGatilho = value;
    },

    get_botaoFecharLista: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.botaoFecharLista"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._botaoFecharLista;
    },
    set_botaoFecharLista: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._botaoFecharLista = value;
    },

    get_botaoFecharCadastro: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.botaoFecharCadastro"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._botaoFecharCadastro;
    },
    set_botaoFecharCadastro: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._botaoFecharCadastro = value;
    },

    get_botaoInserir: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.botaoInserir"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._botaoInserir;
    },
    set_botaoInserir: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._botaoInserir = value;
    },

    get_botaoEnviar: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.botaoEnviar"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._botaoEnviar;
    },
    set_botaoEnviar: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._botaoEnviar = value;
    },

    get_botaoCancelar: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.botaoCancelar"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._botaoCancelar;
    },
    set_botaoCancelar: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._botaoCancelar = value;
    },

    get_campoNome: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.campoNome"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._campoNome;
    },
    set_campoNome: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._campoNome = value;
    },

    get_campoEmail: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.campoEmail"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._campoEmail;
    },
    set_campoEmail: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._campoEmail = value;
    },

    get_campoTitulo: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.campoTitulo"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._campoTitulo;
    },
    set_campoTitulo: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._campoTitulo = value;
    },

    get_campoComentario: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.campoComentario"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._campoComentario;
    },
    set_campoComentario: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._campoComentario = value;
    },

    get_divLista: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.divLista"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._divLista;
    },
    set_divLista: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._divLista = value;
    },

    get_divCadastro: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.divCadastro"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._divCadastro;
    },
    set_divCadastro: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._divCadastro = value;
    },

    get_divUltimoComentario: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.divUltimoComentario"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._divUltimoComentario;
    },
    set_divUltimoComentario: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._divUltimoComentario = value;
    },

    get_divMensagemSucesso: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.divMensagemSucesso"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._divMensagemSucesso;
    },
    set_divMensagemSucesso: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._divMensagemSucesso = value;
    },

    get_spanQtdCharDigitados: function(){
        if (arguments.length !== 0) throw Error.parameterCount();
            return this._spanQtdCharDigitados;
    },
        
    set_spanQtdCharDigitados: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
            if (e) throw e;
            this._spanQtdCharDigitados = value;
    },
    
    get_spanQtdCharRestantes: function(){
        if (arguments.length !== 0) throw Error.parameterCount();
            return this._spanQtdCharRestantes;
    },
        
    set_spanQtdCharRestantes: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
            if (e) throw e;
            this._spanQtdCharRestantes = value;
    },
    
    get_player: function(){
        /// <value type="Sys.Component" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.player"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._player;
    },
    set_player: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Sys.Component}]);
        if (e) throw e;
        this._player = value;
    },

    get_comentario: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.comentario"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._comentario;
    },
    set_comentario: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._comentario = value;
    },

    get_funcaoPost: function(){
        /// <value type="String" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.funcaoPost"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._funcaoPost;
    },
    set_funcaoPost: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: String}]);
        if (e) throw e;
        this._funcaoPost = value;
    },

    get_campoTituloListagem: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.campoTituloListagem"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._campoTituloListagem;
    },
    set_campoTituloListagem: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._campoTituloListagem = value;
    },

    get_campoTituloCadastro: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.campoTituloCadastro"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._campoTituloCadastro;
    },
    set_campoTituloCadastro: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._campoTituloCadastro = value;
    },

    get_qtdCharUltimoComentario: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.qtdCharUltimoComentario"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._qtdCharUltimoComentario;
    },
    set_qtdCharUltimoComentario: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Number}]);
        if (e) throw e;
        this._qtdCharUltimoComentario = value;
    },
    
    get_qtdMaxCampoComentario: function(){        
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._qtdMaxCampoComentario;
    },
    set_qtdMaxCampoComentario: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Number}]);
        if (e) throw e;
        this._qtdMaxCampoComentario = value;
    },

    get_divCarregando: function(){
        /// <value type="Object" locid="P:J#aorta.playmeweb.webcontrols.Comentarios.divCarregando"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._divCarregando;
    },
    set_divCarregando: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Object}]);
        if (e) throw e;
        this._divCarregando = value;
    },

    setCursor: function(object, value){
        var e = Function._validateParams(arguments, [{name: "object", type: Object}, {name: "value", type: String}]);
        if (e) throw e;
        object.style.cursor = value;
    }
}

aorta.playmeweb.webcontrols.Comentarios.registerClass('aorta.playmeweb.webcontrols.Comentarios', Sys.Component);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();