$(document).ready( function() {
	updateSubscriberCount();
});

function updateSubscriberCount() {
	$.ajax( {
		type :"GET",
		url :"subscribercount.htm",
		dataType :"json",
		cache :false,
		async :true,
		success : function(data, textStatus) {
			$("#subscriberCount").empty().append(data.count + " ");
//			var percentage = data.percentage;
//			var c = 0.00;
//			while(c < percentage){
				$("#percentage").empty().append(data.percentage + "%");
//				c += 0.01;
//			}
		}
	});
}

function submit() {
	var name = $("#name").val();
	var city = $("#city").val();
	var comment = $("#comment").val();
	if ("" == name || "" == city) {
		$("#submitState").addClass("error");
		$("#submitState").empty().append("Bitte fülle die Pflichtfelder aus!");
	} else {
		$("#submitState").empty();
		$("#submitState").removeClass("error");
		$("#submitState").addClass("status");
		var submitted = false;
		$.ajax( {
			type :"GET",
			url :"subscribe.do",
			data : {
				name :name,
				city :city,
				comment :comment
			},
			cache :false,
			async :false,
			contentType :"application/json; charset=utf-8",
			success : function(data, textStatus) {
				submitted = true;
				$("#submitState").empty().append("Danke für Deine Stimme.");
				$("#name").val("");
				$("#city").val("");
				$("#comment").val("");
				$("#name").focus();
			},
			error : function(request, textStatus, errorThrown) {
				var error;
				eval("error = " + request.responseText);
				$("#submitState").empty().append(error.msg);
			}
		});
		if (!submitted)
			$("submitState").empty().append("Die Daten werden übertragen.");
	}
}
