/* fwRatingBox.js - Pagination widget
 *
 * @Author: Ryan McGrath (redux version, library agnostic), Some other guy did the original (no clue who ;P)
 * @Requires http://images.webs.com/JS/fw.js ;P
 */

fw.Libs.load([
	'http://link.members.webs.com/JS/checkLogin.jsp',
	'http://images.webs.com/JS/fwStarRating.js',
	'http://images.webs.com/JS/fwComments.js'
]);

var fwRatingBox = new CompatClass({
    type: 'ratingBox',

    initialize: function(listID, userID, options) {
        this.containerID = 'fwRb'+listID;
        if(fw.Instances.get(this.containerID)) return;
        this.listID = listID;
        this.userID = userID;
        this.options = {
            stars: 5,
            rating: 0,
            limit: 10,
            duration: 30,
            showRatings: true,
            showComments: true,
            showLogin: false,
            draw:true,
            disablePost: false,
            isInline: false,
            isPopup: false,
            isEditable: false,
            startHidden: false,
			showPostHide: true
        }
        fw.CompatExtend(this.options, options || {});
        if(!this.options.showPostHide)
            this.options.showRatings = false;

        this.prepare();
        if(this.options.draw)
            this.draw();
        this.instance = fw.Instances.add(this);
    },

    setHoverCount: function(value){
        if(this.yourRate.disableMouseOut) return;
        var msg = 'Leave a ' + value + '-Star Rating';
        this.hoverText.innerHTML = msg;
    },

    resetHoverText: function(){
        if(this.yourRate.disableMouseOut) return;
        this.hoverText.innerHTML = 'Leave a Rating';
    },

    prepare: function() {
        i1 = new Image(); i1.src="http://images.webs.com/Images/arrowDown.gif";
        i2 = new Image(); i2.src="http://images.webs.com/Images/arrowRight.gif";

        if(this.options.showRatings){
            this.yourRate = new fwStarRating(this.containerID+'-yourRate', {
                draw: false,
                isEditable: !this.options.disablePost,
                stars: 5,
                onMouseOver: this.setHoverCount.CompatBind(this),
                onMouseOut: this.resetHoverText.CompatBind(this),
                onChange: this.postRating.CompatBind(this)
            });
        }

        if(this.options.showComments){
            this.commentBox = new fwComments(this.containerID+'-comments', this.listID, this.userID, {
                disablePost: this.options.disablePost,
                startHidden: this.options.startHidden,
                showRatings: this.options.showRatings,
                showLogin: this.options.showLogin,
                showPostHide: this.options.showPostHide,
                isEditable: this.options.isEditable,
                parent: this,
				onLoad: (this.options.showRatings ? this.initStars.CompatBind(this) : null), draw: false,
				limit: this.options.limit, duration: this.options.duration
            })
        }

        this.HTML = ''
        +'<div class="fwRatingBox" id="'+this.containerID+'" style="text-align:left;">'
        +(this.options.showRatings
            ?'<div style="float:left; font-size:14px;"><span style="font-weight:bold;" id="'+this.containerID+'hoverText">Leave a Rating</span>'
				+this.yourRate.HTML+'<span style="float:left; font-weight:0;">&nbsp;(<span style="font-weight:bold;" id="'+this.containerID+'-rCount">0</span> votes)</span>'
            +'</div>'
            :'')
        +'<div style="clear:both; height:5px"><!----></div>'
        +(this.options.showComments ? this.commentBox.HTML : '')
        +'</div>';
    },

    draw: function() {
        document.write(this.HTML);
        this.activate();
    },

    activate: function() {
        this.container = document.getElementById(this.containerID);
        this.hoverText = document.getElementById(this.containerID+'hoverText');
        if(this.options.showRatings){
            this.yourRate.activate();
            this.ratingCount = document.getElementById(this.containerID+'-rCount');
        }
        if(this.options.showComments){
            this.commentBox.activate();
            if(this.options.showPostHide)
                this.postBox = this.commentBox.postDiv.CommentBox;
        } else {
            this.initStars();
        }
    },

    initStars: function() {
        if(this.options.showComments){
            this.yourRate.rating = function(){return this.userRating > 0 ? this.userRating : 0}.CompatBind(this.commentBox)();
            if(this.yourRate.rating){
                this.yourRate.options.isEditable = false;
            }
            this.yourRate.resetStars();
        } else {
            this.getRating();
        }
    },

    getRating: function() {
        var linkServer = 'http://link.members.webs.com';
        if(this.userID == 35982078) {
            // guardafilm
            linkServer = 'http://link2.members.webs.com';
        }

		fw.jjax.req(linkServer + '/Members/Feedback/getRating.jsp',{
            appendTo: this.container,
            postBody: {
                userID: this.userID,
                commentSettingID: this.listID,
                callback: 'fw.Instances.get("'+this.containerID+'").getRatingCallback',
                rand: Math.random()
            }
        });
    },

    getRatingCallback: function(errorMsg, ratingUser, ratingAvg, ratingCount) {
        if(errorMsg) {
            alert(errorMsg);
            return;
        }

        if(ratingCount>0) {
            this.ratingCount.innerHTML = ratingCount;
            this.yourRate.rating = ratingAvg;
            this.yourRate.resetStars();
        }

        if(ratingUser>0) {
            /* this.yourRate.deActivate(); -- wtf was ever being done here? Don't arbitrarily deactive junk. */
            this.hoverText.innerHTML = 'Average Rating';
        }
    },

    postRating: function() {
		fw.jjax.req('http://link.members.webs.com/Members/Comments/postComment.jsp',{
            appendTo: this.container,
            postBody: {
                userID: this.userID,
                commentSettingID: this.listID,
                ratingValue: this.yourRate.rating,
                next: 'callback',
                callback: 'fw.Instances.get("'+this.containerID+'").postRatingCallback',
                rand: Math.random()
            }
        });
        this.yourRate.deActivate();
    },

    postRatingCallback: function(error, response) {
        if(error) {
            alert(response);
            return;
        }

        ratingAvg = response.split(',')[0];
        ratingCount = response.split(',')[1];
        this.ratingCount.innerHTML = ratingCount;
        this.yourRate.rating = ratingAvg;
        this.yourRate.disableMouseOut = false;
        this.yourRate.resetStars();
        this.hoverText.innerHTML = 'Average Rating';
    }
});
