$(document).ready(function(){
	$('#poll a').click(function(){
		var id = $(this).attr('id');
		var arr = id.split('-');
		var replyId = arr[2];

		$.getJSON('/index/ajax-inquiry-vote',
			  {reply_id: replyId},
			  function(data){
				  if(data.status == 1){
					  setCookie('inquiry' + data.inquiryId, data.replyId, 36544, '/');
					  var td = $('#poll-reply-' + data.replyId).parent().next('td');
					  td.text((parseInt(td.text()) + 1) + ' x');
					  var total = $('#poll-total-hits');
					  var totalValue = parseInt(total.text()) + 1;
					  total.text(totalValue);

					  var arr = $('div#poll td[class=counter]');
					  var bar = null;
					  var counterValue = null;
					  var a = null;
					  for(var i = 0; i < arr.length; i++){
						  a = $(arr[i]).prev('td').find('a');
						  a.after($(a).text());
						  a.remove();
						  bar = $(arr[i]).parent().next('tr').find('div');
						  counterValue = parseInt($(arr[i]).text());
						  bar.css('width', Math.ceil(counterValue / totalValue * 100) + '%');							 
					  }
				  }
			  });

		return false;
	});

});

/*
var url = rewriteBase + "/ajax/inquiry-vote/";
var replyId;
$(document).ready(function() {
	$('#poll //a').click(function() {
		ar = $(this).attr('href').split('/');
		replyId = ar[ar.length - 2];	
		$.post(url,
			{
				replyId: replyId
			}, function(xml) {					
				showResult(xml);
			}
		);	
		return false;
	});
});
	
function showResult(xml) {
	status = $("status",xml).text();
	if(status == 1) {
		sum_of_hits = parseInt($('div#poll //p/strong').text()) + 1;
		$('div#poll //p/strong:last').text("" + sum_of_hits);
		$('div#poll //td[@class=counter]').each(function(i) {
			prevTd = $(this).prev('td').get(0);
			nextTr = $(this).parents('tr').next('tr').get(0);
			ar = $(this).text().split(' ');
			hits = parseInt(ar[0]);			
			div = $('div', nextTr).get(0);
			a = $('a', prevTd).get(0);
			ar = $(a).attr('href').split('/');
			_replyId = ar[ar.length - 2];	
			if(_replyId == replyId) {
				hits += 1;
				$(this).text(hits + ' x');
			} 
			percent = Math.ceil(100 * (hits/sum_of_hits));
			$(div).attr('style', 'width: ' + percent + '%');
			$(a).after($(a).text());
			$(a).remove();
		});
		$('#inquiry-result').html('<p class="success-string">DÄ›kujeme za VĂˇĹˇ hlas</p>');
	} else if (status == 2) {
		$('#inquiry-result').html('<p class="error-string">Anketa nenalezena</p>');
	} else if (status == 3) {
		$('#inquiry-result').html('<p class="error-string">JiĹľ jste hlasoval(a)</p>');
	} else  {
		$('#inquiry-result').html('<p class="error-string">DoĹˇlo k chybÄ›</p>');
	}
}
*/
