/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */




function Poll(form){
    
    var that = this;
    this.formSelector  = form;
    this.form = $(this.formSelector);
    this.submit = $(this.formSelector+ ' .formbtn');

    this.lang = $('input[name=lang]').val();
    this.poll=  $(this.formSelector+ ' input[name=poll]').val();
    this.widget=  $(this.formSelector+ ' input[name=widget]').val();
    this.submit.click(function(){
        that.submitPoll();
    })

}
Poll.prototype.reloadPoll = function() {
	var queryparams = window.location.search;
	var previewQueryParam = "";
	if(queryparams.indexOf("draft") != -1 || queryparams.indexOf("uri") != -1) {
		previewQueryParam = "&preview=true";
	}
    this.form.load('/style/wwfch/getPoll/?lang='+this.lang+'&widget='+this.widget+"&mode=colsize-small-editor" + previewQueryParam);
}
Poll.prototype.submitPoll = function() {
    var that = this;
    var answer = $(this.formSelector+' .poll_answer:checked');
    
    if(!CookieUtil.hasVoted(this.poll)){
        if(answer.size()){
            var data = this.form.serialize();
            data += "&language=" +  this.lang + "&answer=" + answer.val()+"&poll="+ this.poll;
            $.ajax({
                type: 'POST',
                url: "/admin/poll/send-answer",
                data: data,
                success: function(data){
                    CookieUtil.setCookie(that.poll);
                    that.reloadPoll();
                },
                dataType:  "json"
            });
        }
    } else {
        this.reloadPoll();
    }
    
}
