var _Vote = function() {
	this.poll     = new Object();
	this.fullpath = '';
	this.is_load  = false;
	
	this.load = function(poll_id) {
		this.poll = new Object();
		this.is_load = false;
		
		var scope = this;
		
		if (this.fullpath != '') {
			JsHttpRequest.query(
			    this.fullpath+'?action=ajaxGetPoll&poll_id='+poll_id + '&trash=' + Math.random(),
				{  },
				function(result, error){
					if (error) 
						alert(error);
					if (result['error'].length) {
						for (var i in result['error']) {
							alert(result['error'][i]);
						}
					} else {
						scope.is_load = true;
						scope.poll = result.poll;
						scope.refresh();
					}
			    }
			);
		}
	}
    
    this.show = function(poll_id) {
		if (!this.is_load) this.load(poll_id);
		_getId('vf').style.display = 'block';
	}

	this.refresh = function() {
		_getId('poll').innerHTML = '';
		/*Заголовок*/
		var table2 = document.createElement('table');
		var table = document.createElement('tbody');
		table2.appendChild(table);
		var header = document.createElement('tr');
		header.className = 'ask';
		table.appendChild(header);
		var header_img_td = document.createElement('td');
		header_img_td.style.width = '30px';
		header.appendChild(header_img_td);
		var header_img = document.createElement('img');
		header_img.src = '/images/question2.gif';
		header_img.width = '13';
		header_img.height = '16';
		header_img.style.marginBottom = '-4px';
		header_img_td.appendChild(header_img);
		var question_td = document.createElement('td');
		header.appendChild(question_td);
		question_td.innerHTML = this.poll['question'];
		/*Вопросы*/
		
		for(var i in this.poll['answers']) {
            if (this.poll['answers'][i] != null) {
				if (this.poll['answered']) {
                    var answer = this.poll['answers'][i];
                    var answers_tr = document.createElement('tr');
                    table.appendChild(answers_tr);
                    var answer_button_td = document.createElement('td');
                    answers_tr.appendChild(answer_button_td);
                    var answer_label_td = document.createElement('td');
                    answers_tr.appendChild(answer_label_td);
                    var answer_label = document.createElement('label');
                    answer_label.setAttribute('for', 'answer_' + answer['id']);
                    answer_label.innerHTML = answer['answer']+' ('+answer['percent']+'%)';
                    answer_label_td.appendChild(answer_label);
					var ans_div = document.createElement('div');
					ans_div.className = 'res-bar';
					answer_label_td.appendChild(ans_div);
					
					var ans_bar_div = document.createElement('div');
					ans_bar_div.style.width = answer['percent']+'%';
					ans_bar_div.innerHTML = '&nbsp;';
					ans_div.appendChild(ans_bar_div);
				} else {
					var answer = this.poll['answers'][i];
					var answers_tr = document.createElement('tr');
					table.appendChild(answers_tr);
					var answer_button_td = document.createElement('td');
					answers_tr.appendChild(answer_button_td);
                    var IE='\v'=='v';
                    if (IE) {
                        var answers_button = document.createElement('<input name="answer">');
                    }
                    else {
                        var answers_button = document.createElement('input');
                        answers_button.name = 'answer';
                    }
					answers_button.type = 'radio';
					/*answers_button.setAttribute('name', 'answer');*/
					answers_button.value = answer['id'];
					answers_button.id = 'answer_' + answer['id'];
					answer_button_td.appendChild(answers_button);
					var answer_label_td = document.createElement('td');
					answers_tr.appendChild(answer_label_td);
					var answer_label = document.createElement('label');
					answer_label.setAttribute('for', 'answer_' + answer['id']);
					answer_label.innerHTML = answer['answer'];
					answer_label_td.appendChild(answer_label);
				}
			}
		}
        
		/*Ссылки снизу*/
		var links_tr = document.createElement('tr');
		links_tr.className = 'links';
		var links_empty_td = document.createElement('td');
		links_empty_td.innerHTML = '&nbsp;';
		links_tr.appendChild(links_empty_td);
		var links_td = document.createElement('td');
		links_tr.appendChild(links_td);
		var vote_link = document.createElement('a');
		vote_link.href = '#';
        vote_link.onclick = function() {
            Vote.send_vote();
			return false;
	    }
		vote_link.className = 'vote-link';
		vote_link.innerHTML = 'Проголосовать';
		var archive_link = document.createElement('a');
		archive_link.className = 'archive';
		archive_link.href = this.fullpath + 'archive/';
		links_td.appendChild(archive_link);
		if (!this.poll['answered']) {
			links_td.appendChild(vote_link);
		}
		archive_link.innerHTML = 'Архив голосований';
		table.appendChild(links_tr);
		_getId('poll').appendChild(table2);
		//_getId('poll').innerHTML = _getId('poll').innerHTML;
		//alert(_getId('poll').outerHTML);
	}
	
	this.send_vote = function() {
        if (this.fullpath != '') {
			var poll_id = this.poll['id'];
			var form = document.forms.form_poll;
			var answer_id = 0;
			
			for(var i = 0; i < form.answer.length; i++) {
				if (form.answer[i].checked) {
					answer_id = form.answer[i].value;
				}
			}
			
			if (answer_id == 0) {
				alert( 'Пожалуйста, выберите один из вариантов ответа' );
				return false;
			}
			
			var scope = this;
			
            JsHttpRequest.query(
                this.fullpath+'?action=ajaxSendVote&poll_id='+poll_id+'&answer_id='+answer_id + '&trash=' + Math.random(),
                {  },
                
				function(result, error){
                    if (error) 
                        alert(error);
                    if (result['error'].length) {
                        for (var i in result['error']) {
                            alert(result['error'][i]);
							_getId('vf').style.display='none';
							return;
                        }
                    }
					alert('Спасибо, ваш голос учтен');
					_getId('vf').style.display='none';
                    Vote.load( result['poll_id'] );
                }
            );
        }
	}
	
	return this;
}

var Vote = new _Vote();
