﻿TOWNHALL.namespace('homepage');
TOWNHALL.homepage.poll =
{
    guid: null,
    _vote: 0,
    init: function() {
        var cookies = TOWNHALL.util.cookies;
        this._vote = cookies.get('Poll' + this.guid);
        if (this._vote && this._vote > 0) {
            this.display();
        }
    },
    display: function() {
        document.getElementById('Selection').style.display = 'none';
        document.getElementById('Result').style.display = 'inline';
        var lbl = document.getElementById('Result' + this._vote);
        if (lbl) {
            lbl.style.fontWeight = 'bolder';
        }
    },
    click: function() {
        var list = document.getElementsByName('answer');
        for (i = 0; i < list.length; i++) {
            if (list[i].checked) {
                this._vote = list[i].value;
                break;
            }
        }
        TOWNHALL.ajax.calls.SubmitPollVote(this.guid, this._vote, this.complete, this.error);
    },
    complete: function() {
        var dt = new Date();
        TOWNHALL.util.cookies._fixCookieDate(dt);
        dt.setDate(dt.getDate() + 365);
        var guid = TOWNHALL.homepage.poll.guid;
        var vote = TOWNHALL.homepage.poll._vote;
        TOWNHALL.util.cookies.set('Poll' + guid, vote, dt, null, document.domain);
        TOWNHALL.homepage.poll.display();
    },
    error: function() {
        alert('There was an error submitting your vote.\nPlease try again');
    }
};
