$(function()
{
   $('.formsQuestionForm').submit(function(event)
   {
      event.preventDefault();
      var formToken=$(this).find('input[name=token]').val();
      var formId=$(this).find('input[name=formId]').val();
      var formVote=$(this).find('input[name=form'+formId+']:checked').val();
      var formInfo="ERROR";

      var nowForm=$(this);

      $(this).parent('div').find('.info').text('');
      $(this).parent('div').find('.info').attr('class', 'info');

      if (!isNaN(formVote))
      {
	 $.ajax(
	 {
	    type: "POST", url: "ajax/question.php", data: "formToken="+formToken+"&formVote="+formVote+"&formId="+formId,
	    complete: function(data)
	    {
	       if (data.responseText=='0')
	       {//vote added
		  formInfo='Głos oddany';
		  updateFormView(formId, true);
		  console.info("result="+data.responseText);
	       }
	       else if(data.responseText=='-1')
	       {//user voted today already
		  formInfo='Już głosowałeś.';
	       }
	       else if(data.responseText=='-2')
	       {//user not verified
		  formInfo='Błąd weryfikacji użytkownika';
	       }
	       else if(data.responseText=='-3')
	       {//wrong data
		  formInfo='Niekompletne dane';
	       }
	       else if(data.responseText=='-5')
	       {//wrong data
		  formInfo='Głosowanie z komórek wyłączone';
	       }
	       else
	       {//other error
		  formInfo='Wystąpił nieznany błąd';
	       }
	       nowForm.parent('div').find('.info').text(formInfo);
	    }
	 });
      }
      else
      {
	 formInfo='Zaznacz jedną opcję';
	 $(this).parent('div').find('.info').text(formInfo);

      }
   });

   $("a.showResultsForm").click(function(event)
   {
      event.preventDefault();
      var formId=$(this).attr('title');
      updateFormView(formId, false);
   });

   $("#forms p.formAnswer")
   .each(function()
   {
      var tmpFormId=$(this).attr('title');
      updateFormView(tmpFormId, false);
   });
});


function updateFormView(formId, newVote)
{
   $('div.forms'+formId+'').children('.formAnswer').remove();
   $('div.forms'+formId+'').children('form').remove();

   $('div.forms'+formId+'').prepend('<img src="gfx/loader.gif" alt="Ładuję ..." />');
   	 $.ajax(
	 {
	    type: "POST", url: "ajax/question.php", data: "formId="+formId+"&updateQuestionData=true",
	    dataType : 'json',
	    success : function(data)
	    {
	       var result='';

	       if(data.length)
	       {
		  $.each(data, function (key, values)
		  {
		     result+='<p class="formAnswer" ><span class="title">'+values['title']+'&nbsp;('+values['width']+' %)</span><span class="value" style="width:'+values['width']+'%"></span></p>';
		  });
	       }
	       else
	       {
		  result='<p class="formAnswer">Nie oddano głosów</p>';
	       }
	       $('div.forms'+formId+'').children('img').remove();
	       $('div.forms'+formId+'').prepend(result);

	       if (newVote)
	       {
		  var oldVotes=$('div.forms'+formId+'').children('.votes').children('span').text();
		  oldVotes=parseInt(oldVotes);
		  oldVotes=oldVotes+1
		  $('div.forms'+formId+'').children('.votes').children(' span').text(oldVotes);
	       }
	    }
	 });
}


