/*
*	Rater
*	Provides AJAX-based rating functionality
*	
*	Requires Trapeze jQuery distribution
*	
*	Taylan Pince (tpince at trapeze dot com) - March 20, 2009
*/

$.namespace("trapeze.Rater");

trapeze.Rater = $.Class.extend({
    
    selector : "",
    total_selector : null,
    
    add_url : "",
    remove_url : "",
    active : false,
    data : null,
    link : null,
    
    link_template : '<a href="javascript:void(0);" title="%(title)" class="%(class)">%(copy)</a>',
    
    rate_title : '',
    rated_title : '',
    rate_copy : 'Recommend this item',
    rated_copy : 'Remove your recommendation',
    rate_class : 'rate',
    rated_class : 'rated',
    
    update_rating : function(url, callback) {
        $.ajax({
            url : url,
            type : "POST",
            processData : false,
            data : this.data + "&value=1",
            dataType : "json",
            contentType : "application/json",
            success : callback
        });
    },
    
    add_rating : function() {
        this.update_rating(this.add_url, this.added_rating.bind(this));
    },
    
    added_rating : function(data) {
        if (this.total_selector) {
            this.link.html(this.rated_copy).attr({
                "title" : this.rated_title,
                "class" : this.rated_class
            }).unbind("click").click(this.remove_rating.bind(this));
            
            $(this.total_selector).text(data.total);
        } else {
            this.link.text(data.total).attr({
                "title" : this.rated_title,
                "class" : this.rated_class
            }).unbind("click").click(this.remove_rating.bind(this));
        }
    },
    
    remove_rating : function() {
        this.update_rating(this.remove_url, this.removed_rating.bind(this));
    },
    
    removed_rating : function(data) {
        if (this.total_selector) {
            this.link.html(this.rate_copy).attr({
                "title" : this.rate_title,
                "class" : this.rate_class
            }).unbind("click").click(this.add_rating.bind(this));

            $(this.total_selector).text(data.total);
        } else {
            this.link.text(data.total).attr({
                "title" : this.rate_title,
                "class" : this.rate_class
            }).unbind("click").click(this.add_rating.bind(this));
        }
    },
    
    init : function(selector, options) {
        this.selector = selector;
        this.data = $(this.selector).serialize();
        
        if (options) {
            for (opt in options) {
                this[opt] = options[opt];
            }
        }
        
        this.link = $(trapeze.render_template(this.link_template, {
            "title" : (this.active) ? this.rated_title : this.rate_title,
            "class" : (this.active) ? this.rated_class : this.rate_class,
            "copy" : (this.active) ? this.rated_copy : this.rate_copy
        })).insertBefore(this.selector);
        
        if (this.active) {
            this.remove_url = $(this.selector).attr("action");
            
            this.link.click(this.remove_rating.bind(this));
        } else {
            this.add_url = $(this.selector).attr("action");
            
            this.link.click(this.add_rating.bind(this));
        }
        
        $(this.selector).remove();
    }
    
});
