var pollResultFormatter = Class.create({
    initialize: function(form) {
        this.form = $(form);
        this.results = this.form.select('.pollResults').reduce();
        this.resultRows = this.results.childElements();
        this.answers = $A(this.form.select('.answer'));
        this.percentages = $A(this.form.select('.percentage-string'));
        this.percentageBars = $A(this.form.select('.percentage-bar'));
        this.endcaps = $A(this.form.select('.percentage-endcap'));
        this.labelPadding = 10;
        this.establishWidths();
        this.formatWidths();
//        console.log('Total width = %apx, Answer Width = %apx, Percentage Widths are %a, Percentage String Widths = %a, EndCap Width = %a, the Available Width  = %a', this.totalWidth, this.answerWidth, this.percentageWidths, this.percentageStringWidth, this.endcapWidth, this.availableWidth);
    },

    establishWidths: function() {
        // Subtract 1px here because sometimes it rounds up
        this.totalWidth = this.results.getWidth() - 1;
        this.answerWidth = this.determineLongestWidth(this.answers) + this.labelPadding;
        this.percentageStringWidth = this.determineLongestWidth(this.percentages);
        this.endcapWidth = this.determineLongestWidth(this.endcaps);
        this.percentageWidths = this.determinePercentageWidths();
        this.availableWidth = this.totalWidth - this.percentageStringWidth - this.answerWidth - this.endcapWidth - 5;
    },

    determineLongestWidth:function(items) {
        var lengths = $A();
        items.each(function(item, index) {
            lengths[index] = item.getWidth();
        });
        return this.longestWidth(lengths);
    },
    
    longestWidth: function(x) {
        return x.sort(function(a, b) {return a - b;}).last();
    },

    determinePercentageWidths: function() {
        var PercentageWidths = new Array();
        this.percentages.each(function(percentage, index) {
            PercentageWidths[index] = parseInt(percentage.innerHTML) / 100;
        });
        return PercentageWidths;
    },

    formatWidths: function() {
        this.setWidth(this.answers, this.answerWidth);
        this.results.setStyle({visibility:'visible'});
        this.setWidth(this.percentages, this.percentageStringWidth);
        this.setWidth(this.endcaps, this.endcapWidth);
        this.formatPercentages();
    },

    setWidth: function(items, widthVar) {
        items.each(function(item) {
            item.setStyle({
                width:widthVar + 'px'
            });
        }.bind(this));
    },

    formatPercentages: function() {
        this.percentageBars.each(function(percentage, index) {
            var percentageWidth = Math.floor((this.percentageWidths[index] * this.availableWidth));
            console.log('avail: ' + this.availableWidth + ' percentpx: ' + percentageWidth + ' percent: ' + this.percentageWidths[index]);
            new Effect.Morph(percentage, {style: 'width:' + percentageWidth + 'px;'});
        }.bind(this));
    }
});

