/**
 * anq.js
 */

var anqmanager = function() {

    var result = {};

    var open = false;

    return {
        getResult : function () {

            if (this.open) {
                return false;
            }

            var sid = Ext.fly('survey_id').getValue();

            if (!sid) {
                return false;
            }

            this.open = true;

            Ext.Ajax.request({

                url : 'survey.html',

                params : {sid : sid},

                success: function(res, opts) {

                    this.result = Ext.decode(res.responseText);

                    this.outputResult();

                },

                scope : this

            });
        },

        outputResult : function () {
            if (!this.result) {
                return false;
            }

            var rank = [
                'first',
                'second',
                'third',
                'fourth',
                'fifth'
            ];
            var i = 0;
            var prev = 0;
            var tmp = 0;

            for (key in this.result) {

                if (!this.result[key].percent) {
                    this.result[key].percent = 0;
                    this.result[key].width = '1';
                }

                if (i) {
                    if (this.result[key].percent == prev) {
                        --i;
                        ++tmp;
                    } else {
                        i = tmp + i;
                        if (i > 4) {
                            i = 4;
                        }

                        tmp = 0;
                    }
                }

                if (!Ext.fly(key)) {
                    continue;
                }

                for(var j=0; j<5; ++j) {
                    Ext.fly(key).removeClass('bg_'+rank[j]);
                }

                Ext.fly(key).addClass('bg_'+rank[i]);

                Ext.fly(key).setStyle({width : this.result[key].width+'px'});

                for(var j=0; j<5; ++j) {
                    Ext.fly('_'+key).removeClass(rank[j]);
                }

                Ext.fly('_'+key).addClass(rank[i]);
                Ext.fly('_'+key).select('span').remove();

                var dh = Ext.DomHelper;
                dh.append(
                    Ext.fly('_'+key),
                    {
                        tag : 'span',
                        html : this.result[key].percent+'%'
                    }
                );

                ++i;
                prev = this.result[key].percent;
            }

            Ext.select('.survey_result').setDisplayed(true);
            Ext.select('.check').setDisplayed(false);
            Ext.select('.votebtn').setDisplayed(false);

        },

        vote : function () {

            Ext.select('.survey_result').setDisplayed(false);

            var id = '';
            for (var i=1; i<6; ++i) {
                if (!Ext.fly('ans_'+i)) {
                    break;
                }

                if (Ext.fly('ans_'+i).dom.checked) {
                    id = 'ans_'+i;
                    break;
                }

            }

            if (!id) {
                this.open = false;
                return false;
            }

            this.open = true;

            var val = Ext.fly(id).getValue();

            var sid = Ext.fly('survey_id').getValue();

            Ext.Ajax.request({

                url : 'vote.html',

                params : {
                    sid : sid,
                    ans : val
                },

                success: function(res, opts) {

                    this.result = Ext.decode(res.responseText);

                    this.outputResult();

                },

                scope : this

            });

        }

    }
}

