//
// +----------------------------------------------------------------------+
// | armenia.ru                                                           |
// +----------------------------------------------------------------------+
// | brrn.armenia.ru                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 2008-2010 arax.ru                                      |
// +----------------------------------------------------------------------+
//
// $Id: poll.js 167 2010-05-19 22:30:36Z starfish $
//

(function ($) {
	var onDocumentReady = function (event) {
		$('#txt_custom_answer').focus (function() {
			$('#opt_custom_answer').attr('checked', 'checked');
		});

		$('#txt_custom_answer').blur (function() {
			$(this).val($.trim($(this).val()));
			if ($(this).val() == '')
				$('#opt_custom_answer').removeAttr('checked');
		});

		$('#lnk_poll_prev,#lnk_poll_next').live ('click', function() {
			var id = $(this).metadata().id;
			var url = /*'/' + lang + */'/armpoll/show/' + id;
			$.getJSON(url, function (data) {
				if (data.result)
				{
					$('div#poll').html(data.result);
				}
				else
				{
					alert(data.errorMessage.poll);
				}
			});
		});

		$('#lnk_poll_result').live ('click', function() {
			var id = $(this).metadata().id;
			var url = /*'/' + lang + */'/armpoll/result/' + id;
			$.getJSON(url, function (data) {
				if (data.result)
				{
					$('div#poll').html(data.result);
				}
				else
				{
					alert(data.errorMessage.poll);
				}
			});
		});

		$('#lnk_poll_content').live ('click', function() {
			var id = $(this).metadata().id;
			var url = /*'/' + lang + */'/armpoll/content/' + id;
			$.getJSON(url, function (data) {
				if (data.result)
				{
					$('div#poll').html(data.result);
					$('#txt_custom_answer').focus (function() {
						$('#opt_custom_answer').attr('checked', 'checked');
					});
					$('#txt_custom_answer').blur (function() {
						$(this).val($.trim($(this).val()));
						if ($(this).val() == '')
							$('#opt_custom_answer').removeAttr('checked');
					});
				}
				else
				{
					alert(data.errorMessage.poll);
				}
			});
		});

		$('#lnk_poll_response').live ('click', function() {
			var id = $('#frm-poll').metadata().id;
			var min_answer_count = $('#frm-poll').metadata().min_answer_count;
			var max_answer_count = $('#frm-poll').metadata().max_answer_count;
			var ans_set = $('input.poll_answer_btn:checked');
			if (max_answer_count && ans_set.length > max_answer_count)
			{
				alert(msgChoose3.replace(/%u/, max_answer_count));
				return;
			}
			else if (ans_set.length < min_answer_count)
			{
				if (min_answer_count == 1)
					alert(msgChoose1);
				else
					alert(msgChoose2.replace(/%u/, min_answer_count));
				return;
			}
			if ($('input#opt_custom_answer:checked').length && $('input#txt_custom_answer').val() == '')
			{
				alert(msgChooseCust);
				$('input#txt_custom_answer').focus();
				return;
			}
			var url = /*'/' + lang + */'/armpoll/response/' + id;
			$.post(url, $('#frm-poll').serialize(), function (data) {
				if (data.result)
				{
					$('div#poll').html(data.result);
				}
				else
				{
					alert(data.errorMessage.poll);
				}
			}, 'json');
		});
	};
	$(document).bind('ready', onDocumentReady);
})(jQuery);