$(document).ready(function() {
	$('input, textarea').placeholder(); 
	
	$("#srcbtn").click(function() {
	
		var x = $("#keywords").val();
		var y = $("#keywords").attr('placeholder');
		if ((x != y) && (x != '')) {
			document.frmSrc.submit();
		}
	});
	
}); 

function validateEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   } else {
	   return true;
   }
}

function popWin2(url, width, height) {
	var Win = window.open(url,"displayWindow",'width=' + width + ',height=' + height + ',resizable=0,scrollbars=no,menubar=no' );
} 

function vote(lang, poll_id) {
	if (poll_id > 0) {
		// if poll id > 0 then send request to poll.php
		 var vote_id = $("input[@name='poll_vote_"+poll_id+"']:checked").val();
		 if (vote_id > 0) {
			$("#poll_res_"+poll_id).fadeOut("slow",function(){ 
				$(this).empty();
				$.getJSON("/pl/polls/vote/vote_id/"+vote_id+"/poll_id/"+poll_id, getPollData);
				$.cookie('poll_id_'+poll_id, poll_id, {expires: 365});
				$("#vote_btn_"+poll_id).hide();
				$("#vote_thx_"+poll_id).show();
			}); 
		 } else {
			alert("select your option"); 
		 }
	} else {
		alert('internal poll error');
	}
}



function animateResults(){
  $(".poll_results_chart_graph").each(function(){
      var percentage = $(this).children('span').text()+'%';
      $(this).css({width: "0%"}).animate({
				width: percentage}, 'slow');
  });
}

function getPollData(data) {
  var qNumber = data['data'].length;
  var results_html = '<div class="poll_results_chart"><div class="poll_results_chart_inner">';
  var poll_id = data['poll']['id'];
 // alert(poll_id);
  var label = "Ilosc glosow";
 // alert(qNumber);
  if (qNumber > 0) {
	  for (var i = 0; i < qNumber; i++) {
		 var votes = data['data'][i]['votes'];
		 var title = data['data'][i]['name'];
		 var percent = parseInt(data['data'][i]['procent']);
		 
		 results_html += "<div class='poll_results_chart_option'><div class='poll_title_res'>"+title+"</div><div class='poll_results_perc'>"+label+": <span class='pink'>"+votes+"</span> <strong>("+percent+"%)</strong></div><div class='poll_results_chart_graph_wrapper'><div class='poll_results_chart_graph'><span>"+percent+"</span></div></div></div>";
	  }
	  results_html += "</div></div>";
  }
 // alert(results_html);
	//$("#poll_id_"+poll_id+" > div > div.poll_variants").hide();
	$("#variants_"+poll_id).hide();
	$("#vote_btn_"+poll_id).hide();
	$("#vote_thx_"+poll_id).show();
	
  	$("#poll_res_"+poll_id).html(results_html).fadeIn("slow",function(){
  		animateResults();
 	});
} 


$(document).ready(function(){
	$(".poll-container").each(function() {
		// for each poll check session and display results if not voted
		var poll_id = $(this).attr('id');
		poll_id = poll_id.replace("poll_",'');
		
		if ($.cookie('poll_id_'+poll_id)) {
			$("#poll_res_"+poll_id).empty();
			poll_id = $.cookie('poll_id_'+poll_id);
			///poll_id = 1;
			$("#variants_"+poll_id).hide();
			$("#vote_btn_"+poll_id).hide();
			$("#vote_thx_"+poll_id).show();
			$.getJSON("/pl/polls/get-poll/poll_id/"+poll_id,getPollData);
		}
	});
  
  if ($("#poll-results").length > 0 ) {
    animateResults();
  }
});

