mp.mvp = {};

mp.mvp.init = function(params) {
    if (!params.container) { params.container = 'mp_poll_container'; }
    if (!params.response) { params.response = {}; }
    
    this.params = params;
    this.answered = {};
    try {
        this.get_history();
    } catch(err) {}
    if (!this.answered || !this.answered[params.qname]) {
        // Do not show polls to users who have already answered
        this.show_poll();
    }
};

mp.mvp.show_poll = function() {
    // Display poll
    var html =  "<div class='mp_poll'>" + 
                    "<div class='close'>" + this.close_button() + "</div>" +
                    "<div class='question'>" + this.params.question + "</div>" +
                    "<div class='response'>" + this.build_response() + "</div>" + 
                "</div>";
    $("#" + this.params.container).html(html);
    
    // Bind click event to newly created buttons
    $('.mp_poll .button-link').click(function() {
        $.getJSON(
            mp.mvp.domain + '/poll', 
            {q: $(this).attr('q'), r: $(this).attr('r')}
        );
        // Add to list of answered polls in cookie
        mp.mvp.answered[$(this).attr('q')] = 1;        
        mp.mvp.thanks();
    });
    
    $('.mp_poll .close-button').click(function() {
        mp.mvp.answered[$(this).attr('q')] = 1;        
        mp.cookie.set('mp_poll', $.toJSON(mp.mvp.answered), 14);
        $("#" + mp.mvp.params.container).fadeOut(200);
    });
};

mp.mvp.thanks = function() {
    // Flash 'Thanks' before fading out
    mpmetrics.track('Answer poll', {'poll': this.params.qname });
    mp.cookie.set('mp_poll', mpmetrics.json_encode(mp.mvp.answered), 14);
    
    $("#" + mp.mvp.params.container + " .response").html("<div class='thanks'>Thanks!</div>");
    
    setTimeout(function() {
        $("#" + mp.mvp.params.container).fadeOut(200);
    }, 400);
};

mp.mvp.close_button = function() {
    return "<a class='close-button' href='javascript:void(0)' q='" + this.params.qname + "'>[ x ]</a>";
};

mp.mvp.build_response = function() {
    // Build response divs, buttons
    var html = '';

    if (!this.params.response.buttons) {
        this.params.response.buttons = ['Yes', 'No'];
    }
    
    html += "<table class='button-container'><tr>";
    for (var b in this.params.response.buttons) {
        if (b) {
            html += "<td align='center'>";
            html += this.button_generator(this.params.qname, this.params.response.buttons[b]); 
            html += "</td>";
        }
    }
    html += "</tr></table>";

    return html;
};

mp.mvp.button_generator = function(qname, response) {
    return "<a href='javascript:void(0);' class='button-link' r='" + response + "' q='" + qname + "'><div class='button'>" + response + "</div></a>";
};

mp.mvp.get_history = function() {
    // Get this user's poll history
    var answered = eval('(' + mp.cookie.get("mp_poll") + ')');
    
    if (answered) {
        for (var i in answered) {
            if (i) { this.answered[i] = answered[i]; }
        }
    }
};
