/*
*	Poll
*	A reusable JS class for providing AJAX functionality to the Polls app
*	
*	Requires jQuery library (http://www.jquery.com),
*   jQuery.Class plug-in (http://www.taylanpince.com/blog/posts/jquery-class-plug-in)
*	
*	Taylan Pince (taylanpince at gmail dot com) - February 9, 2007
*/

$.namespace("trapeze.Poll");

trapeze.Poll = $.Class.extend({
    
    id : "",
    selector : "",
    
    error_template : '<p class="%(type)">%(message)</p>',
    percentage_template : '<strong>%(percentage)%</strong> - ',
    answer_template : '<div class="poll-percentage-wrapper"><div class="poll-percentage" style="width: 0px;"><!-- --></div></div>',
    
    render_template : function(template, values) {
        for (val in values) {
            template = template.replace("%(" + val + ")", values[val]);
        }
        
        return template;
    },
    
    submit_form : function() {
        $(this.selector).find("p.error, p.warning, p.success, p.stand-by").fadeOut("slow");
        $(this.selector).find("input[type=submit]").attr("disabled", true);
        $(this.selector).prepend(this.render_template(this.error_template, {
            "message" : "",
            "type" : "stand-by"
        }));
        
        $.ajax({
            url : $(this.selector).attr("action"),
            type : "POST",
            processData : false,
            data : $(this.selector).serialize(),
            dataType : "json",
            contentType : "application/json",
            success : this.parse_results.bind(this)
        });
        
        return false;
    },
    
    parse_results : function(data) {
        $(this.selector).find("p.stand-by").fadeOut("slow");
        
        if (data.errors) {
            for (error in data.errors) {
                for (e in data.errors[error]) {
    	            $(this.selector).prepend(this.render_template(this.error_template, {
    	                "message" : data.errors[error][e],
    	                "type" : "error"
    	            }));
                }
	        }
        } else {
            $(this.selector + "-Votes").html(data.total_votes);
            $(this.selector).find("input").hide();
            
            for (var i = 0; i < data.results.length; i++) {
                var label_selector = "label[for=" + this.id + "-answer_" + i + "]";

                $(label_selector).prepend(this.render_template(this.percentage_template, {
                    "percentage" : data.results[i].percentage
                })).append(this.answer_template);
                
                if (data.results[i].percentage > 0) {
                    $(label_selector).find(".poll-percentage").animate({
                        "width" : data.results[i].percentage + "px"
                    });
                } else {
                    $(label_selector).find(".poll-percentage").addClass("poll-percentage-empty").removeClass("poll-percentage");
                }
            }
        }
        
        $(this.selector).find("input[type=submit]").attr("disabled", false);
        $("#PollFooter").animate({
            "height" : "25px"
        });
    },
    
    init : function(id) {
        this.id = id;
        this.selector = "#" + this.id;
        
        $(this.selector).submit(this.submit_form.bind(this));
    }
    
});

