var Polls = Class.create({

	initialize: function(url, canVote, voted, lang, close) {
		this.canVote = canVote;
		this.lang = lang ? lang : 'ru';
		this.isClose = close;
		this.voted = voted;
		this.url = url;
		this.data = [];
		this.bindEvents();

	},

	bindEvents: function() {
		if (!this.voted && $('doVoteButtonId'))
			$('doVoteButtonId').observe('click', this.doVote.bindAsEventListener(this));

		if ($('show_result'))
			$('show_result').observe('click', this.showResult.bindAsEventListener(this));

		if ($('show_question'))
			$('show_question').observe('click', this.showQuestion.bindAsEventListener(this));

	},

	doVote: function() {
		$('doVoteButtonId').disabled = true;
		if (this.canVote && !this.voted) {
			var data = [];
			var send = true;

			$$(".questionIds").each(function(questionIdInput){
				var idQuestion = questionIdInput.value;
				var goodQuestion = false;

				$$(".question"+idQuestion).each(function(answerElement){
					if (answerElement.hasClassName('radio') || answerElement.hasClassName('checkbox')) {
						if (answerElement.checked) {
							var obj = {};
							obj.question_id	= idQuestion;
							obj.text = '';
							obj.answer_id = answerElement.value;
							data.push(obj);
							goodQuestion = true;
						}
					} else if (answerElement.hasClassName('text')) {
						if (answerElement.value.length) {
							var obj = {};
							obj.question_id	= idQuestion;
							obj.answer_id = 0;
							obj.text = answerElement.value;
							data.push(obj);
							goodQuestion = true;
						}
					}
				});

				if (!goodQuestion) {
					send = false;
					$('question_result_'+idQuestion).innerHTML = '<span style="color:red;">Выберите вариант ответа</span>';
				} else {
					$('question_result_'+idQuestion).innerHTML = '';
				}
			});

			if (send) {
				var isClose = this.isClose;
				new Ajax.Request(this.url, {
					method: 'get',
					parameters: {data: this.jsArrayToPhpArray(data), poll_id: $F('pollId'), lang:this.lang, close:this.isClose},
					onSuccess: function(transport) {

						if (isClose) {
							window.open('','_parent','');
							window.parent.close();
						} else {
							$('poll_block').innerHTML = transport.responseText;
						}
					}
				});
			}
		} else {
			$('vote_status').innerHTML = 'Голосовать могут только зарегистрированные пользователи';
		}
		$('doVoteButtonId').disabled = false;

	},

	jsArrayToPhpArray: function(a) {
		var a_php = "";

		for (var i = 0; i<a.length; i++){
			a_php = a_php + "i:" + i + ";";

			var text = '';
			if (typeof(a[i].text) != 'undefined'){
				text = escape(a[i].text);
			}

			a_php = a_php + "a:3:" + '{s:11:"question_id";i:' + a[i].question_id  + ';s:9:"answer_id";i:' + a[i].answer_id + ';s:4:"text";s:' + text.length + ':"' + text +'";}';
		}
		a_php = "a:" + a.length + ":{" + a_php + "}";

		return a_php;
	},

	showResult: function() {
		$('poll_form').hide();
		$('poll_results').show();
	},

	showQuestion: function() {
		$('poll_form').show();
		$('poll_results').hide();
	}
});