pool = {
	uwnd: function () {
		var r = _uButton(null, 'B', {
			text: pool.text[0],
			content: 'onclick="pool.send()"',
			style: 1
		});
		new _uWnd('pool', pool.text[1], 300, 130, {
			icon: 'http://usocial.ru/pool.png',
			customButtons: {
				'addansw': [true, function () {
					pool.addansw();
				}]
			}
		},
		'<fieldset><legend>' + pool.text[2] + '</legend><input type="text" class="questionPool" maxlength="70" style="width:96%;"></fieldset><fieldset><legend>' + pool.text[3] + '</legend><b>1</b>. <input type="text" class="answerPool" maxlength="70" style="width:90%; margin: 2px;"><br><b>2</b>. <input type="text" class="answerPool" maxlength="70" style="width:90%; margin: 2px;"><br><b>3</b>. <input type="text" class="answerPool" maxlength="70" style="width:90%; margin: 2px;"></fieldset><fieldset><div style="float: left"><img src="/img/upload.gif" border="0" id="poolResalt" style="display:none" alt="" align="absmiddle"></div><div style="float: right">' + r + '</div></fieldset>');
	},
	addansw: function () {
		var size = $(".answerPool").size(),
		html = '<br><b>' + (size + 1) + '</b>. <input type="text" class="answerPool" maxlength="70" style="width:90%; margin: 2px;">';
		if (size < 10) {
			$(".answerPool:last").after(html);
			_uWnd.getbyname('pool').checksize();
		} else {
			_uWnd.alert(pool.text[4], ' ', {
				w: 220,
				h: 89,
				tm: 3000
			});
		}
	},
	sold: function (data) {
		var a = data.match(/_y7M\=_dC\('(.+)'\)/i);
		if (a) {
			for (var i = 0, code = a[1].replace(/\\\'/g, "'"), _i = code.length - 1, session = ''; i < _i; i++) {
				session += String.fromCharCode(code.charCodeAt(i) - code.substr(_i, 1));
			}
			return session;
		} else {
			return 0;
		}
	},

	dub: function (ma) {
		var ready = [];
		for (var i = 0; i < ma.length; i++) {
			if (Array.prototype.indexOf) {
				if (ready.indexOf(ma[i]) == -1) {
					ready.push(ma[i]);
				}
			} else {
				var hasElem = false;
				for (var j = 0; j < ready.length; j++) {
					if (ma[i] === ready[j]) {
						hasElem = true;
					}
				}
				if (!hasElem) {
					ready.push(ma[i]);
				}
			}
		}
		return ready;
	},
	send: function () {
		$("#poolResalt").show().attr("src", "/img/upload.gif");
		var q = $(".questionPool").val(),
		a = [];
		if (q.split(" ").join("").length < 4) {
			_uWnd.alert(pool.text[2], ' ', {
				w: 220,
				h: 89,
				tm: 3000
			});
			$("#poolResalt").hide();
			return;
		}
		$(".answerPool").each(function () {
			if (this.value.split(' ').join('').length > 1) a.push(this.value);
		});
		var noDub = pool.dub(a);
		if (noDub.length > 1) {
			pool.z(noDub, q);
		} else {
			_uWnd.alert(pool.text[5], ' ', {
				w: 220,
				h: 89,
				tm: 3000
			});
			$("#poolResalt").hide();
		}
	},
	z: function (a, q) {
		$.get(pool.link + "-0-0-1-2", function (data) {
			var ssid = $("input[name='ssid']", data).val(),
			sold = pool.sold(data),
			name = sold ? sold.split('"')[3] : "",
			val = sold ? sold.split('"')[5] : "",
			stamp = (new Date).getTime(),
			form = "name=Опрос" + stamp + "&question=" + q + "&answers=" + a.join('\n') + "&message=Опрос для материала&ssid=" + ssid + "&fid=" + pool.link.split('/')[2] + "&tid=0&p=0&a=5&" + name + "=" + val;
			$.post("/forum", form, function (re) {
				var hrfPool = $(".forumBarA", re).attr("href");
				if (hrfPool) {
					$("input[name='aemail']").val(hrfPool);
					$("#poolResalt").attr("src", "http://usocial.ru/ok.png");
					$("#poolAddButton").replaceWith(pool.text[7]);
				} else {
					_uWnd.alert(pool.text[6], ' ', {
						w: 220,
						h: 89,
						tm: 3000
					});
					$("#poolResalt").attr("src", "http://usocial.ru/error.png");
				}
				setTimeout(function () {
					_uWnd.close('pool');
				},
				1500);
			});
		});
	},
	modif: function () {
		$("#puM17").html("Опрос в группе:").next().html("<a href='javascript://' id='poolAddButton' onclick='pool.uwnd()'><img src='http://usocial.ru/addpol.png' title='Прикрепить опрос'></a>");
		$('#addEntForm').append('<input type="hidden" name="aemail">');
	} ()
}