function voteProto (id, multiple) {
	this.vote_id = id;
	this.is_multiple = multiple == 1;
	
	this.Vote = function() {
		var answers = [];
		$('#vote_'+this.vote_id+' :checked').each (
			function(el) {answers.push ($(this).attr('id').replace(/^answer_/, ''));}
		);
		this.loadData({
			url:'/module/vote.vote@vote_id:'+this.vote_id+'/',
			type: 'POST',
			data: {answer:answers.join(',')}
		});
	}
	this.viewResults = function() {
		this.loadData( {
				url: '/module/vote.results@vote_id:'+this.vote_id+'/'
		});
	}
	this.viewVote = function() {
		this.loadData( {
				url: '/module/vote.show@vote_id:'+this.vote_id+'/'
		});
	}
	this.loadData = function(params) {
		if (!params.url) {
			return;
		}
		 $.ajax({
			type: params.type || 'GET',
			url: params.url,
			data: params.data || {},
			dataType: 'html',
			Vote: this,
			success: function(data) {this.Vote.appendData(data);}
		});
	}
	this.appendData = function(data) {
		var vb = $('#vote_'+this.vote_id).get(0).parentNode;
		$(vb).animate({opacity:'hide'},'slow', function() {vb.innerHTML = data; $(vb).animate({opacity:'show'},'slow');});
		//$('#vote_'+this.vote_id+'_submit').
		//.innerHTML = data;
		//alert(data);
		return;
		$('#vote_'+this.vote_id+'_submit').animate({opacity:'hide'},'slow');
		$('#vote_'+this.vote_id+' table tr').animate({opacity:'hide'},'slow');
		var total_rate = 0;
		for (var i in data) {
			total_rate += (data[i].count_votes*1);
		}
		for (var i in data) {
			var percent = total_rate > 0 ? Math.round(data[i].count_votes / total_rate * 100) : 0;
			$('<tr style="display:none;"><td colspan="2" class="vote_res">'+data[i].answer_text+'<div style="width:'+percent+'%;">'+percent+'%</div></td></tr>')
				.appendTo($('#vote_'+this.vote_id+' table tbody'))
				.show('slow');
		}
	}
}


function $n(node) {
	return document.getElementById(node);
}

function replyForm(b) {
	var rl = $n('reply_link_' + b);
	var f = $n('reply_form');
	if ($n('i_reply_to').value == b && f.style.display != 'none') {
		rl.className = 'reply_link jl';
		$('#reply_form').animate({opacity:'hide'},300);
	} else {
		var p = $n('comment_' + b + '_body');
		$('#reply_form').animate({opacity:'hide'},300,'linear', function() {
			nf = p.appendChild(f.cloneNode(true));
			f.parentNode.removeChild(f);
			$n('reply_link_' + $n('i_reply_to').value).className = 'reply_link jl';
			$n('i_reply_to').value = b;
			nf.id = 'reply_form';
			$('#reply_form').animate({opacity:'show'},300,'linear', function() {nf.getElementsByTagName('textarea').item(0).focus();});
			rl.className = 'reply_link_active';
			//nf.getElementsByTagName('textarea').item(0).focus();
		});
	}
}

function editForm(comment_id) {
	var t = $n('comment_' + comment_id + '_text');
	var ef = $n('edit_form_' + comment_id);
	if (ef.style.display != 'none') {
		$('#edit_form_'+comment_id).animate({opacity:'hide'},300, 'linear', function() {
			$('#comment_' + comment_id + '_text').animate({opacity:'show'},300);
		});
		$n('edit_link_'+comment_id).className = 'comment_link';
	} else {
		var ta = ef.getElementsByTagName('textarea').item(0)
		ta.value = t.innerHTML.replace(/<br[\s\/]?>/ig, "\n");
		//ta.focus();
		$n('edit_link_'+comment_id).className = 'comment_link a';
		$('#comment_'+comment_id+'_text').animate({opacity:'hide'},300, 'linear', function() {
			$('#edit_form_' + comment_id).animate({opacity:'show'},300, 'linear', function() {
				ta.focus();}
			);
		});
	}
}

function update_comment(comment_id) {
	var ef = $n('edit_form_'+comment_id);
	var ta = ef.getElementsByTagName('textarea').item(0);
	var text = ta.value;
	startProc();
	$.ajax({
			dataType: 'html',
			type: 'POST',
			url: '/?module=comments&action=update&comment_id='+comment_id,
			data: {text: text},
			success: function (data) {
				//alert(data);
				if (/^COMERROR/.test(data)) {
					alert(data.replace(/^COMERROR\s/, ''));
				} else {
					$n('comment_' + comment_id + '_text').innerHTML = data;
					$n('comment_' + comment_id + '_text').style.display = 'none';
				}
				$('#edit_form_'+comment_id).animate({opacity:'hide'},'slow', 'linear', function() {
					$('#comment_' + comment_id + '_text').animate({opacity:'show'},'slow');
				});
				$n('edit_link_'+comment_id).className = 'comment_link';
				stopProc();
			}
	});
}

function removeComment(comment_id) {
	if (confirm('Вы уверены, что хотите удалить данный комментарий?')) {
		startProc();
		$.ajax({
			dataType: 'html',
			type: 'GET',
			url: '/?module=comments&action=remove&comment_id='+comment_id,
			success: function (data) {
				if (/^COMERROR/.test(data)) {
					alert(data.replace(/^COMERROR\s/, ''));
				} else {
					$n('comment_'+comment_id+'_body').innerHTML = data;
					$n('comment_'+comment_id+'_body').className = 'body removed';
					$n('userpic_'+comment_id).src = '/i/0.gif';
				};
				stopProc();
			}
		});
	}
}

function submit_comment() {
	var rto = $n('i_reply_to').value;
	startProc();
	$.ajax({
		dataType: 'html',
		type: 'POST',
		url: '/?module=comments&action=add&mode=chunk&entity_id='+$n('entity_id').value,
		data: {
			text: $n('comment_text').value,
			reply_to: rto
		},
		success: function (data) {
			$n('reply_link_' + rto).className = 'reply_link jl';
			$n('reply_form').style.display = 'none';
			var wrap = $n('comment_' + rto +'_children').appendChild(document.createElement('div'));
			wrap.style.display = 'none';
			wrap.innerHTML = data;
			$(wrap).animate({opacity:'show'},'slow');
			stopProc();
		}
	});
	return false;
}

function startProc() {
	var p = $n('proccessing');
	if (!p) {
		var p = document.body.appendChild(document.createElement('img'));
		p.src = '/i/process.gif';
		p.id = 'proccessing';
		p.style.zIndex = 669;
	}
	p.style.position = 'absolute';
	p.style.left = Math.round(getClientCenterX()-( p.width /2))+'px';
	p.style.top = Math.round(getClientCenterY()-( p.height /2))+'px';
	p.style.display = 'block';
}

function stopProc() {
	$n('proccessing').style.display = 'none';
}

function getBodyScrollTop()
{
	return (self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop));
}

function getBodyScrollLeft()
{
	return (self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft));
}

function getDocumentHeight()
{
	return (document.body.scrollHeight > document.body.offsetHeight)?document.body.scrollHeight:document.body.offsetHeight;
}

function getDocumentWidth()
{
	return (document.body.scrollWidth > document.body.offsetWidth)?document.body.scrollWidth:document.body.offsetWidth;
}

function getClientCenterX()
{
	return parseInt(getClientWidth()/2)+getBodyScrollLeft();
}

function getClientCenterY()
{
	return parseInt(getClientHeight()/2)+getBodyScrollTop();
}

function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

$(document).ready(function() {
	$('.dropdown').each(function() {
			var container = this.parentNode;
			var dropdown = this;
			$(container).addClass('has_dropdown');
			$(dropdown).css({display:'none'});
			$(dropdown).find('.dropdown_item').css({padding:'10px 0 0 0'});
			$(container).hover (
				function() {
					$(this).addClass('has_dropdown_active');
					var position_test_node = document.createElement('span');
					container.insertBefore(position_test_node, container.firstChild);
					$(position_test_node).css({position:'absolute', border:'1px solid #C00'});
					var parent_pos = $(position_test_node).offset();
					container.removeChild(position_test_node);
					
					var main_width = $('#main').width();
					var left_offset = parent_pos.left - 6;
					
					if (left_offset + $(dropdown).width() > (main_width - 20) ) {
						left_offset = main_width - $(dropdown).width() - 30;
					}
					
					$(dropdown).css({
						display:'block',
						left: left_offset + 'px',
						top: parent_pos.top + $(container).height() - 1 +3+ 'px'
					});
					
					
					//document.title = parent_pos.top + '/' + $(container).height();
					
					if ($(dropdown).width() < $(container).width()) {
						$(dropdown).css({width:$(container).width()+1+'px'});
					}
				},
				function () {
					$(this).removeClass('has_dropdown_active');
					$(dropdown).css({
							display:'none'
					});
				}
			);
	});
});