/**
* Voting widget to rate content.
* @author Patrick Paul-Hus
* A custom component handling the voting on components extending the base Ext.Component
*/
BQ.widget.Voting = Ext.extend(Ext.Component, {
    stars: null,
    myVote: null,
    label: null,
    options: {
        id: null,
        labelId: null,
        contentId: null,
        headerId: null,
        voteCountId: null
    },

    initComponent: function() {
        //alert('initComponent');
        this.labels = new Array();
        this.labels[5] = BQJSLabels.vote_level1;
        this.labels[4] = BQJSLabels.vote_level2;
        this.labels[3] = BQJSLabels.vote_level3;
        this.labels[2] = BQJSLabels.vote_level4;
        this.labels[1] = BQJSLabels.vote_level5;

        this.stars = Ext.DomQuery.select('#' + this.options.id + ' a');
        this.label = Ext.get(this.options.labelId);

        for (var i = 0, length = this.stars.length; i < length; i++) {
            var star = Ext.fly(this.stars[i]);
            star.on("mouseover", this.over, this);
            star.on("mouseout", this.out, this);
            star.on("click", this.vote, this);
        }
        BQ.widget.Voting.superclass.initComponent.call(this);
    },

    /**
     * vote
     * the handler for clicks on voting stars
     * @param {Ext.EventObject} ev
     * @param {HTMLElement} el
     */
    vote: function(ev, el) {
        ev.stopEvent();
        this.myVote = Ext.fly(ev.getTarget()).getAttributeNS("", "rel");

        /**
         * send a request to the API to register the vote on this content
         * @param {Object} request
         */
        Ext.Ajax.request({
            url: BQJSConfig.api_json + '/BQAContent/vote',
            method: 'POST',
            success: function(request) {
                var response = Ext.decode(request.responseText);
                if (response.type == BQ_SUCCESS) {
                    
                    /**
                     * remove all the current listeners since voting is now done
                     */
                    for (var i = 0, length = this.stars.length; i < length; i++) {
                        var item = Ext.fly(this.stars[i]);
                        item.removeAllListeners();
                        item.on("click", function(ev) {
                            ev.stopEvent();
                            return false;
                        });
                        if (item.getAttributeNS("", "rel") <= this.myVote) {
                            item.addClass("on");
                        }
                        else {
                            item.removeClass("on");
                        }
                    }

                    this.label.update(BQJSLabels.vote_allowed_success);
                    Ext.get(this.options.headerId).update(BQJSLabels.vote_your_rating);

                    /** 
                     * change the class on the stars that include the rating.
                     */
                    var rating = response.data.rating;
                    for (var i = 0; i <= 5; i++) {
                        if (rating >= i) {
                            var stars = Ext.DomQuery.select("#star-rating span." + i);
                            for (var j = 0, length = stars.length; j < length; j++) {
                                var star = Ext.fly(stars[j]);
                                star.addClass("on");
                            }
                        }
                    }

                    /**
                     * change labels and take into account the plurality of the number of votes
                     */
                    if (response.data.vote == 1) {
                        var stars = Ext.DomQuery.select("#rating-vote-count .count");
                        for (var i = 0, length = stars.length; i < length; i++) {
                            var temp = Ext.fly(stars[i]);
                            temp.update(response.data.vote + " " + "vote");
                        }
                    }
                    else {
                        var stars = Ext.DomQuery.select("#rating-vote-count .count");
                        for (var i = 0, length = stars.length; i < length; i++) {
                            var temp = Ext.fly(stars[i]);
                            temp.update(response.data.vote + " " + "votes");
                        }
                    }
                    if (response.data.vote == 0) {
                        var stars = Ext.DomQuery.select("#rating-vote-count .count");
                        for (var i = 0, length = stars.length; i < length; i++) {
                            var temp = Ext.fly(stars[i]);
                            temp.update(response.data.vote + " " + "votes");
                        }
                    }
                }
                else {
                    for (var i = 0, length = this.stars.length; i < length; i++) {
                        var item = Ext.fly(this.stars[i]);
                        item.removeAllListeners();
                        item.on("click", function(ev) {
                            ev.stopEvent();
                            return false;
                        });
                    }
                    this.label.update(response.msg);
                }
            },
            
            /**
             * if something went wrong with the Ajax voting call, we don't currently do much
             */
            failure: function() {

            },
            params: {
                contentId: this.options.contentId,
                force: this.myVote
            },
            scope: this
        });
    },

    /**
     * over
     * handler that lights up the various stars that the user might be hovering over
     * @param {Object} ev
     * @param {Object} el
     */
    over: function(ev, el) {
        var current_link = Ext.fly(ev.getTarget());
        var starIndex = current_link.getAttributeNS("", "rel");

        for (var i = 0, length = this.stars.length; i < length; i++) {
            var star = Ext.fly(this.stars[i]);
            var starNumber = star.getAttributeNS("", "rel");
            if (starIndex >= starNumber) {
                star.addClass("on");
            }
        }
    },

    /**
     * out
     * handler for when the mouse is no longer hovering over any voting stars
     */
    out: function() {
        this.clearStars();
        this.label.update("");
    },

    /**
     * clearStars
     * helpder method that will remove any current traces of "on"-ness for a star
     */
    clearStars: function() {
        for (var i = 0, length = this.stars.length; i < length; i++) {
            var star = Ext.fly(this.stars[i]);
            star.removeClass("on");
        }
    }
});