﻿/// <reference name="MicrosoftAjax.js"/>
Type.registerNamespace("aorta.playmeweb.webcontrols");

aorta.playmeweb.webcontrols.Votos = function() {
    aorta.playmeweb.webcontrols.Votos.initializeBase(this);

    this._botaoVotarPositivo = null;
    this._botaoVotarNegativo = null;
    this._campoValorPositivo = null;
    this._campoValorNegativo = null;
    this._player = null;
    this._mensagemSucesso = null;
    this._nomeCookie = null;
    this._qtdDiasExpitacao = 1;
    this._votos = null;

    this._votarPositivoClickDelegate = null;
    this._votarNegativoClickDelegate = null;
    this._player_showMusicInfoDelegate = null;

    this._obterVotosSucessoCallbackDelegate = null;
    this._obterVotosFalhaCallbackDelegate = null;
    this._votarSucessoCallbackDelegate = null;
    this._votarFalhaCallbackDelegate = null;
}

aorta.playmeweb.webcontrols.Votos.prototype = {
    initialize: function() {
        aorta.playmeweb.webcontrols.Votos.callBaseMethod(this, 'initialize');

        this._habilitarVotacao(false);

        this._player_showMusicInfoDelegate = Function.createDelegate(this, this._player_showMusicInfoHandler);
        this._votarPositivoClickDelegate = Function.createDelegate(this, this._votarPositivoClickHandler);
        this._votarNegativoClickDelegate = Function.createDelegate(this, this._votarNegativoClickHandler);
        this._obterVotosSucessoCallbackDelegate = Function.createDelegate(this, this._obterVotosSucessoCallbackHandler);
        this._obterVotosFalhaCallbackDelegate = Function.createDelegate(this, this._obterVotosFalhaCallbackHandler);
        this._votarSucessoCallbackDelegate = Function.createDelegate(this, this._votarSucessoCallbackHandler);
        this._votarFalhaCallbackDelegate = Function.createDelegate(this, this._votarFalhaCallbackHandler);

        this._player.add_showMusicInfo(this._player_showMusicInfoDelegate);

        if (this._botaoVotarPositivo) {
            $addHandler(this._botaoVotarPositivo, 'click', this._votarPositivoClickDelegate);
            this.setCursor(this._botaoVotarPositivo, 'pointer');
        }

        if (this._botaoVotarNegativo) {
            $addHandler(this._botaoVotarNegativo, 'click', this._votarNegativoClickDelegate);
            this.setCursor(this._botaoVotarNegativo, 'pointer');
        }
    },

    dispose: function() {
        aorta.playmeweb.webcontrols.Votos.callBaseMethod(this, 'dispose');

        //Remove os handlers para os eventos
        this._player.remove_showMusicInfo(this._player_showMusicInfoDelegate);

        if (this._botaoVotarPositivo)
            $clearHandlers(this._botaoVotarPositivo);

        if (this._botaoVotarNegativo)
            $clearHandlers(this._botaoVotarNegativo);
    },

    _player_showMusicInfoHandler: function(sender, args) {
        var musica = this.get_player().get_musica();

        //Configura o nome do cookie de votação para ser por música
        this._nomeCookie = 'aorta.jaVotou_' + musica.ProgramadorIdMusica;

        this._habilitarVotacao(!this.get_jaVotou());

        //Recupera os dados da votação
        aorta.playmeweb.webcontrols.services.Votos.ObterVotos(this.get_player().get_canal(), musica, this._obterVotosSucessoCallbackDelegate, this._obterVotosFalhaCallbackDelegate);
    },

    _obterVotosSucessoCallbackHandler: function(votos) {
        if (!votos.TotalVotosPositivos) votos.TotalVotosPositivos = 0;
        if (!votos.TotalVotosNegativos) votos.TotalVotosNegativos = 0;
        if (votos.TotalVotosPositivos == 0 && votos.TotalVotosNegativos == 0) votos.TotalVotosPositivos = 1;
        this._votos = votos;
        this._atualizarVotos();
        this._habilitarVotacao(!this.get_jaVotou());
    },

    _obterVotosFalhaCallbackHandler: function(erro, contexto, funcaoErro) {
        if (erro != null)
            Sys.Debug.trace('Ocorreu o seguinte erro na função \'' + funcaoErro + '\': ' + erro.get_message())

        this._habilitarVotacao(false);
    },

    _votar: function(positivo) {
        this._habilitarVotacao(false);
        if (positivo)
            this._votos.TotalVotosPositivos += 1;
        else
            this._votos.TotalVotosNegativos += 1;
        aorta.playmeweb.webcontrols.services.Votos.Votar(this.get_player().get_canal(), positivo, this.get_player().get_musica(), this._votarSucessoCallbackDelegate, this._votarFalhaCallbackDelegate);
    },

    _votarSucessoCallbackHandler: function() {
        //this._habilitarVotacao(false);
        this._atualizarVotos();
        this._criarCookie(this._nomeCookie, true, this._qtdDiasExpitacao);
    },

    _votarFalhaCallbackHandler: function(erro, contexto, funcaoErro) {
        if (erro != null)
            Sys.Debug.trace('Ocorreu o seguinte erro na função \'' + funcaoErro + '\': ' + erro.get_message());
    },

    _atualizarVotos: function() {
        var valPos = this._votos.TotalVotosPositivos;
        var valNeg = this._votos.TotalVotosNegativos;
        var valTot = valPos + valNeg;

        if (this.get_campoValorPositivo())
            this.get_campoValorPositivo().innerHTML = String(parseInt(((valPos / valTot) * 100))) + '%';
        if (this.get_campoValorNegativo())
            this.get_campoValorNegativo().innerHTML = String(parseInt(((valNeg / valTot) * 100))) + '%';
    },

    _habilitarVotacao: function(habilitar) {
        if (this.get_botaoVotarPositivo()) {
            $clearHandlers(this._botaoVotarPositivo);
            if (habilitar)
                $addHandler(this._botaoVotarPositivo, 'click', this._votarPositivoClickDelegate);
            this.setCursor(this._botaoVotarPositivo, habilitar ? 'pointer' : 'default');
            this.get_botaoVotarPositivo().style.filter = !habilitar ? 'alpha(opacity=40)' : 'alpha(opacity=100)';
            this.get_botaoVotarPositivo().style.opacity = !habilitar ? '.4' : '1';
        }
        if (this.get_botaoVotarNegativo()) {
            $clearHandlers(this._botaoVotarNegativo);
            if (habilitar)
                $addHandler(this._botaoVotarNegativo, 'click', this._votarNegativoClickDelegate);
            this.setCursor(this._botaoVotarNegativo, habilitar ? 'pointer' : 'default');
            this.get_botaoVotarNegativo().style.filter = !habilitar ? 'alpha(opacity=40)' : 'alpha(opacity=100)';
            this.get_botaoVotarNegativo().style.opacity = !habilitar ? '.4' : '1';
        }
    },

    _votarPositivoClickHandler: function() {
        this._votar(true);
    },

    _votarNegativoClickHandler: function() {
        this._votar(false);
    },

    _criarCookie: function(nome, valor, diasExpiracao) {
        var expires = '';

        if (diasExpiracao) {
            var data = new Date();
            data.setTime(data.getTime() + (diasExpiracao * 24 * 60 * 60 * 1000));
            expires = "; expires=" + data.toGMTString();
        }

        document.cookie = nome + "=" + valor + expires + "; path=/";
    },

    _obterCookie: function(nome) {
        var nomeEQ = nome + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ')
                c = c.substring(1, c.length);
            if (c.indexOf(nomeEQ) == 0)
                return c.substring(nomeEQ.length, c.length);
        }

        return null;
    },

    get_botaoVotarPositivo: function() {
        /// <value type="String" locid="P:J#aorta.playmeweb.webcontrols.Votos.botaoVotarPositivo"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._botaoVotarPositivo;
    },
    set_botaoVotarPositivo: function(value) {
        var e = Function._validateParams(arguments, [{ name: "value", type: Object}]);
        if (e) throw e;
        this._botaoVotarPositivo = value;
    },

    get_botaoVotarNegativo: function() {
        /// <value type="String" locid="P:J#aorta.playmeweb.webcontrols.Votos.botaoVotarNegativo"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._botaoVotarNegativo;
    },
    set_botaoVotarNegativo: function(value) {
        var e = Function._validateParams(arguments, [{ name: "value", type: Object}]);
        if (e) throw e;
        this._botaoVotarNegativo = value;
    },

    get_campoValorPositivo: function() {
        /// <value type="String" locid="P:J#aorta.playmeweb.webcontrols.Votos.campoValorPositivo"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._campoValorPositivo;
    },
    set_campoValorPositivo: function(value) {
        var e = Function._validateParams(arguments, [{ name: "value", type: Object}]);
        if (e) throw e;
        this._campoValorPositivo = value;
    },

    get_campoValorNegativo: function() {
        /// <value type="String" locid="P:J#aorta.playmeweb.webcontrols.Votos.campoValorNegativo"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._campoValorNegativo;
    },
    set_campoValorNegativo: function(value) {
        var e = Function._validateParams(arguments, [{ name: "value", type: Object}]);
        if (e) throw e;
        this._campoValorNegativo = value;
    },

    get_player: function() {
        /// <value type="String" locid="P:J#aorta.playmeweb.webcontrols.Votos.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_mensagemSucesso: function() {
        /// <value type="String" locid="P:J#aorta.playmeweb.webcontrols.Votos.mensagemSucesso"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._mensagemSucesso;
    },
    set_mensagemSucesso: function(value) {
        var e = Function._validateParams(arguments, [{ name: "value", type: String}]);
        if (e) throw e;
        this._mensagemSucesso = value;
    },

    get_jaVotou: function() {
        /// <value type="String" locid="P:J#aorta.playmeweb.webcontrols.Votos.jaVotou"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return (this._obterCookie(this._nomeCookie) != null);
    },

    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.Votos.registerClass('aorta.playmeweb.webcontrols.Votos', Sys.Component);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();