var sv_post = "/content/html/survey/vote.html";
var sv_rslt = "/content/html/survey/results.html";
var _load_results = function(){
	if($type(survey) == "number"){
		new Request.JSON({
			url: sv_rslt,
			onComplete: function(response){
				try{_call_ivw("survey-box");}catch(e){};
				$("survey_" + survey).addClass("content");
				//$("survey_" + survey).setStyles({height: $("survey_" + survey).getSize().y});
				$("survey_" + survey).set({html: null});

				if(response.messages.length > 0){
					$("survey_" + survey).grab(new Element("div", {style: "padding:15px 0px 0px 0px;"}));

					response.messages.each(function(message, id){
						$("survey_" + survey).grab(new Element("div", {html: message, style: "padding:0px 0px 0px 15px;"}));
						});
					}

				if(response.messages.length == 0){
					response.data.each(function(item, index){
						var amount;

						if(response.votes_total > 0){
							amount = (item[1] / (response.votes_total / 100)).toFixed(2);
							}
						else{
							amount = 0;
							}
							
						$("survey_" + survey).grab(new Element("div", {html: "<b>" + item[0] + "</b><br/> " + item[1] + " Votes ( " + amount + "% )", style: "padding:5px 0px 0px 15px;"}));
						});
					}

				if(response.messages.length == 0){
					$("survey_" + survey).grab(new Element("div", {html: "<p>", style: "padding:15px 0px 0px 0px;"}), "top");
					$("survey_" + survey).grab(new Element("div", {html: "</p>"}), "bottom");
					$("survey_" + survey).grab(new Element("div", {html: "<h3 class=\"subtitle\">Ergebnisse:</h3>"}), "top");
					$("survey_" + survey).grab(new Element("div", {html: "<br/><p><b>Votes gesamt:</b> " + response.votes_total + "</p><br/>"}), "bottom");
					}
				else{
					$("survey_" + survey).grab(new Element("div", {html: "<h3>Fehler:</h3>"}), "top");
					}
				}
			}).send("id=" + survey);
		}
	}

window.addEvent("load", function(){
	if($type(survey) == "number"){
		if($type($("surveyform_" + survey)) == "element"){
			$("surveyform_" + survey).set("action", sv_post);
			$("surveyform_" + survey).addEvent("submit", function(event){
				event.stop();

				this.set("send", {
					onSuccess: function(response){
						_load_results();
						}
					});

				this.elements["survey_" + survey + "_submit"].value = "Lade Daten...";
				this.send();
				});
			}
		}
	});