var rateArticle = {
    userScore:null,
    scoreImage:null,
    rateBox:null,
    requestParams:{ },
    ajaxResponse:null,
    requestUrl:'/service/ajax/add_rating.html?',
    config:{
        maxHeight: 0,
        minHeight: 0,
        openRate: .5,
        closeRate: .5
    },

    init:function(){
        this.scoreImage = yuiDom.get("scoreImg");
        this.rateBox = yuiDom.get("rateThis");
        this.config.maxHeight = yuiDom.getStyle(this.rateBox, "height").replace('px', '');
    },
    
    setRequestParam:function(name, value){
        this.requestParams[name] = value;
    },

    makeRequest:function(){
        var closeRateBox = new yuiAnim(this.rateBox, { height: {to: this.config.minHeight} }, this.config.closeRate, yuiEasing.easeIn);
        closeRateBox.onComplete.subscribe(function() {
            var self = rateArticle;
            var callBack = {
                success:self.handleSuccess,
                failure:self.handleFailure,
                scope:self
            }
            for(paramName in self.requestParams){
                self.requestUrl += paramName + "=" + self.requestParams[paramName] + "&";
            }
            self.requestUrl = self.requestUrl.slice(0, self.requestUrl.length -1);
            self.request = yuiConnect.asyncRequest('GET', self.requestUrl, callBack);

        });

        closeRateBox.animate();
    },

    handleSuccess:function(response){
        this.ajaxResponse = YAHOO.lang.JSON.parse(response.responseText);
        if(this.ajaxResponse.success){
            this.scoreImage.src = "/images/largeMurmur" + this.ajaxResponse.rating + ".gif";
            this.displayNewRatingBridge();
        }
        else{
            mm.Error.add(this.ajaxResponse.error);
            mm.Error.generateBridge();
        }
    },

    handleFailure:function(response){        
        mm.Error.add("The rating action failed, Please try again.");
        mm.Error.generateBridge();
    },

    displayNewRatingBridge:function(){
        // bridge method used to regain scope from the animation
        var self = rateArticle;
        self.displayNewRating();
    },

    displayNewRating:function(scopeObj){
        var userRate = "";
        userRate += "<li><img src='/images/ratedThisArticalText.gif' alt='' width='125' height='26' border='0'></li>";
        userRate += "<li><img src='/images/rateThis" +  this.userScore + ".gif' alt='' width='26' height='26' border='0'></li>";
        userRate += "<li><img src='/images/rateThisLeft.gif' alt='' width='4' height='26' border='0'></li>";
        this.rateBox.innerHTML = userRate;

        var openRateBox = new yuiAnim(this.rateBox, { height: {from: this.config.minHeight, to: this.config.maxHeight} }, this.config.openRate, yuiEasing.easeIn);
        openRateBox.animate();

        //update the total vote count
        yuiDom.get("totalVotesNumber").innerHTML++;
    },

    rateArticle:function(){
        this.makeRequest();
    },

    rateArticleBridge:function(articleId, score){
        var self = rateArticle;
        self.setRequestParam("articleId", articleId);
        self.setRequestParam("score", score);
        self.userScore = score;
        self.rateArticle();
    },

    initBridge:function(){
        var self = rateArticle;
        self.init();
    }

};

yuiEvent.onDOMReady(rateArticle.initBridge);