function generateRatingForm (text,value, question_id, article){		
		html = '<div class="question" id="question_' + article.replace(/[\s\W]/g,"") + '_' + question_id + '"><input type="hidden" name="results[][question_id]" value="' + question_id + '" /><input type="hidden" name="returnpath" value="' + document.URL + '" /><input type="hidden" name="results[][subject]"  value="' + article + '" /><div class="ratingwrapper">';
		html += '<span class="text">' + text + '</span> ';
		
		html += '<div><select name="results[][value]">';
			html += '<option value="1">Ungenügend!</option>';
			html += '<option value="2">Sehr Schlecht</option>';
			html += '<option value="3">Schlecht</option>';
			html += '<option value="4">Grenzwertig</option>';
			html += '<option value="5">Brauchbar</option>';
			html += '<option value="6">Geht</option>';
			html += '<option value="7">in Ordnung</option>';
			html += '<option value="8">Gut</option>';
			html += '<option value="9">Sehr Gut</option>';
			html += '<option value="10">Extremst Gut</option>';
		html += '</select></div><span class="stars-cap">- nicht bewertet -</span>';
		html += '</div></div>';
		return(html);
}

function applyStars(data,article){
		$.each(data, function(i,d){
 			containerarb = '#question_' + article.replace(/[\s\W]/g,"") + '_' + d.question.id + ' .ratingwrapper';
 			if($.cookie(article)){
				$(containerarb).stars({inputType: "select",captionEl: $(containerarb + ' .stars-cap'),disabled:true });
			}else{
				$(containerarb).stars({inputType: "select",captionEl: $(containerarb + ' .stars-cap') });
			}
				$(containerarb).stars("select", d.question.average);		  			
		});
}

function buildPoll(data,article){
	html = ('<div class="box_background"></div><!-- .box_background --><div class="wrapper"><div class="closewrapper"><a class="close" href="#">schliessen</a></div><!-- .closewrapper --><h2>Ihre Bewertung f&uuml;r &laquo;' + article + '&raquo;</h2><form action="http://polls.feuerwerk-test.de/polls/' + $poll + '/results" method="post">');
	$.each(data, function(i,d){
			html += generateRatingForm(d.question.text, d.question.average, d.question.id,article);
			$total = $total + d.question.average;
			$count = i;				
			if($total_votes < d.question.total_votes){
				$total_votes = d.question.total_votes;
			}
	});
	html += '<br /><input type="submit" value="bewerten" /></form></div><!-- .wrapper -->';
	return html;
}

function generateTotal(average,total_votes, poll){		
		html = '<div class="total rating_total"><h3 class="caption">Nutzerbewertung zu diesem Feuerwerksk&ouml;rper</h3> <div><select>';
			html += '<option value="1">Ungenügend!</option>';
			html += '<option value="2">Sehr Schlecht</option>';
			html += '<option value="3">Schlecht</option>';
			html += '<option value="4">Grenzwertig</option>';
			html += '<option value="5">Brauchbar</option>';
			html += '<option value="6">Geht</option>';
			html += '<option value="7">in Ordnung</option>';
			html += '<option value="8">Gut</option>';
			html += '<option value="9">Sehr Gut</option>';
			html += '<option value="10">Extremst Gut</option>';
		html += '</select></div><span class="stars-cap"></span> (' + total_votes + ' Bewertungen) - <a href="#" class="rate">&raquo; Produkt bewerten</a></div><!-- #rating_total -->';
  	$(poll).append(html);

		$('.rating_total', poll).stars({inputType: "select",captionEl: $('.rating_total .stars-cap', poll),disabled: true});
		$('.rating_total', poll).stars("select", Math.round(average));			  	
}
$toalert = "";

function doAjaxStuff(article,poll){
	url = "http://polls.feuerwerk-test.de/polls/" + $poll + "/results/for?callback=?";
	$.getJSON(url,
		  {
		  	article: article,
		  	format: "json"
		  },
		  function(data) {
				$toalert += " 2: " + article;
		  	if(data){	
					$total_votes = 0;					
					$total = 0;
					$count = 0;
					$pollform = buildPoll(data,article);		
					$(poll).append($pollform);
   			  applyStars(data,article);
   			  $('.wrapper', poll).hide();
			  	average = $total / ($count+1);
			  	generateTotal(average,$total_votes, poll);
			  	$('input[type="submit"]', poll).live("click",function(){
						$.cookie(article,'yes');
					});			  	
				}
		  }		  
	);
}


$(document).ready(function(){
	var article = '';
	$.each($('.poll'),function(i,poll){
		article = $(poll).attr('title');
		$poll = $(poll).attr('id').replace(/poll_/g, "");
		
	
		$toalert += " 1: " + article;
		
		doAjaxStuff(article,poll);
		$toalert += " 3: " + article;
		
		
		

		
		
		$('a.rate', poll).live("click",function(){			
			$('.box_background', poll).show();		
			$('.wrapper', poll).show();
			return false;
		});
		
		$('a.close', poll).live("click",function(){			
			$('.box_background', poll).hide();		
			$('.wrapper', poll).hide();
			return false;
		});
	});
	


});
