var VotePoll = Class.create(
{
    result_template: new Template('<p class="question">#{question}</p>'+
                                  '<p class="noticia-link"">#{link_title}</p>'+
                                  '#{rows}'
                                  ),  
    
    row_template: new Template('<div class="result">'+
                               '<label class="answer">#{answer}: #{votes}</label>'+
                               '<div class="bar answer_#{id}_#{index}" style="width: #{width}%"><!-- --></div><span class="percentage">#{per}%</span>'+
                               '</div>'),                            
                            
    initialize: function(results) {
        
        this.results = results;
        
        $$('.poll').each(function(poll) {
                            poll.select('form').invoke('observe', 'submit', this.submitVote.bind(this));
                         }.bind(this));
        
        $$('.results-btn').each(function(btn) {
                                
                                }.bind(this));
        
        $$('.results-btn').invoke('observe', 'click', this.showResults.bind(this));
        
        $$('.results-back-btn').invoke('observe', 'click', this.returnToVote.bind(this));
        
        $$('.question-before-btn').invoke('observe', 'click', this.showOldquestions.bind(this));
        
        $$('.polllink').invoke('observe', 'click', this.showResults.bind(this));
        
    },
    
    submitVote: function(event) {
        event.stop();
        
        form = $(event.target);
        form.select('.message')[0].hide();
        
        form.request({ onComplete: this.resultsReturn.bind(this, form) });
    },
    
    resultsReturn: function(form, response) {
        
        res = response.responseJSON;
                        
        if(res != null && res.status == 0)
            form.select('.message')[0].update(res.message).show();
        else {
            this.results[form.select('.pollid')[0].value] = res['results'];
            this.openResults(form.select('.pollid')[0].value);
        }
    },
    
    showResults: function(evt){
        evt.stop();
        var str   = new String(evt.target);
        var pairs = str.split('/');
        idvalue   = pairs[pairs.length-1];
        this.openResults(idvalue);
    },
    
    openResults: function(poll_id) {
        
        $('polls').select('.pollsh').invoke('hide');
        
        // calc percentages
        total = 0;
        for(i in this.results[poll_id])
            total += parseInt(this.results[poll_id][i], 10);
        
        result_rows = new Array();
        
        for(i in this.results[poll_id]) {
            
            votes      = parseInt(this.results[poll_id][i], 10);
            //percentage = (votes * 100 / total).toFixed(2);
            percentage = (votes * 100 / total).toFixed(0);
            
            result_rows.push(this.row_template.evaluate({
                                                        id: poll_id,
                                                        index: i,
                                                        answer: $('answer_'+poll_id+'_'+i).innerHTML,
                                                        votes: this.results[poll_id][i],
                                                        per: percentage,
                                                        width: Math.round(percentage)
                                                        }));
        }
        
        result_obj = new Object();
        if($('question_'+poll_id) != null)
            result_obj['question'] = $('question_'+poll_id).innerHTML;
        
        if($('asw_'+poll_id) != null)
            result_obj['link_title'] = $('asw_'+poll_id).innerHTML;
        
        
        
        result_obj['rows'] = result_rows.join(''); 
        
        $('pollresults').select('.results')[0].update(this.result_template.evaluate(result_obj));
        
        $('pollresults').show();
    }, 
    
    returnToVote: function(evt){
        evt.stop();
        
        $('pollresults').hide();
        $$('.pollsh')[0].show();
        $('polllist').hide();
        
    },
    
    showOldquestions: function(evt){
        evt.stop();
        
        $('polls').select('.pollsh').invoke('hide');
        $('pollresults').hide();
        $('polllist').show();
    }
    
    
    
});

