/**
 * @package polls
 * @namespace polls
 */
Phx.namespace('Phx.Application.Modules.Polls',(function () {

    return {
        init: function(){
            Phx.Application.Modules.Polls.controller.construct();
        }
    };
    
})());


/**
 * polls Controller
 * 
 * construct called on app start
 */
Phx.Application.Modules.Polls.controller = (function (){

        /**
         * polls view instance
         * 
         * @var (object)
         */
        
        return  {
            
            bindPollSubmit : function() {
                $('input[name=pollAnswer]').bind('click', function (e) {
                    Phx.Event.stop(e);

                    // hole mir die pollId aus dem element wlches geklickt wurde
                    var pollId = this.id.split('_')[1];

                    var moduleName = $('#moduleName_' + pollId).val();

                    var params = '&answerId=' + $(this).val();
                    params += '&pollId=' + pollId;
                    params += '&showBars=' + $('#showBars_' + pollId).val();
                    params += '&showChart=' + $('#showChart_' + pollId).val();
                    params += '&showGauge=' + $('#showGauge_' + pollId).val();
                    params += '&moduleName=' + moduleName;
                    
                    Phx.AJAX.call(
                       moduleName + '_answerPoll',
                        Phx.Util.createDelegate(function(data, $ptr) {
                            
                            $('#poll-container_' + pollId).html(data.module.data.html);
                            
                            Phx.Application.Modules.Polls.controller.construct();
                        }, this),
                        self,
                        params
                    );
                });

            },

            bindPollSubmitToLogin : function() {
                $('input[name=pollAnswer_toLogin]').bind('click', function (e) {
                    $('#voting-poll-tologin').submit();
                });
            },

            bindToggle : function() {
                $(".poll-results-toggle").click(function(ev){
                    Phx.Event.stop(ev);
                    var pollId = this.id.substring('poll-toogle-X_'.length);
                    var params = '&pollId=' + pollId;
                    var moduleName = $('#moduleName_' + pollId).val();
                    
                    Phx.AJAX.call(
                       moduleName + '_diagramView',
                        Phx.Util.createDelegate(function(data, $ptr) {
                            var dialog = Phx.UI.Dialog.Alert(i18n.getText('polls_results'), {message: data.module.data.html}).show();
                            $('.polls-diagram-chart-button') .unbind('click').bind( 'click',
                                function(ev, data) {
                                    Phx.Event.stop(ev);
                                    var $diagRoot = $(this).parents('.polls-diagram');
                                    $diagRoot.find('.polls-diagram-bars').addClass('hidden');
                                    $diagRoot.find('.polls-diagram-chart').removeClass('hidden');
                                });
                            $('.polls-diagram-bars-button') .unbind('click').bind( 'click',
                                function(ev, data) {
                                    Phx.Event.stop(ev);
                                    var $diagRoot = $(this).parents('.polls-diagram');
                                    $diagRoot.find('.polls-diagram-bars').removeClass('hidden');
                                    $diagRoot.find('.polls-diagram-chart').addClass('hidden');
                                });
                        }, this),
                        self,
                        params
                    );
                    return false;
                });
            },

            construct : function () {
                this.bindPollSubmit();
                this.bindPollSubmitToLogin();
                this.bindToggle();
            }
        };
}());

