$(document).ready(function() {
	var answer;
	var comment;

	$("#commentarea").hide();
	$("#result_box").hide();
	
	$("#feedback").submit(function() {
		$("#answer").attr("value", answer);
		comment = $("#comment").val();
		email = $("#email").val();
		$("#question").hide();
		$("#commentarea").hide();
		$.ajax({
			url: "feedback.php?answer=" + answer + "&comment=" + comment + "&email=" + email,
			success: function(data) {
				$('#response').html(data);
			}
		});
		return false;
	});
	$("#no").click(function() {
		feedbackclick("no");
	});
	
	$("#yes").click(function() {
		feedbackclick("yes");
	});
	
	function feedbackclick(ans) {
		answer = ans;
		$("#commentarea").show();
		return false;
	};
	$("#clear").live("click", function() {
		$(".mark").each(function($i) {
			$(this).remove();
		});
	});
	$("#add").click(function() {
		$id = $(".mark").size();
		$("#all").append(
			"<li class=\"mark\" id=\"mark" + $id + "\">"
			+ "<input size=\"10\" type=\"text\" class=\"percent\" id=\"percent" + $id + "\" value=\"0\" />"
			+ "&#37; <input size=\"6\" type=\"text\" class=\"value\" id=\"value" + $id + "\" value=\"0\" />"
			+ " <input type=\"button\" class=\"remove\" value=\"-\" />"
			+ "</li>"
		);
	});
	$("#calc").click(calc);
	function calc() {
		var $max = parseInt( $("#max").attr("value") );
		var $percent = 0;
		var $founderror = false;
		$(".percent").each(function(i) {
			$percent += parseInt(this.value);
		});
		if ( ( $percent > 100 ) || ( $percent < 0 ) ) {
			alert("Total percentage isn't 100.");
		}else{
			$least = parseInt( $("#least").attr("value") );
			$total = $least;
			$remaining = 100;
			$(".mark").each(function($i) {
				$perc = parseInt( $("#percent"+$i).attr("value") );
				$val = parseInt( $("#value"+$i).attr("value") );

				if ( ( $perc < 0 ) || ( $perc > 100 ) || ( isNaN($perc) ) ) {
					$("#percent"+$i).addClass("error");
					$founderror = true;
				}else{
					$("#percent"+$i).removeClass("error");
				}
				
				if ( ( $val < 0 ) || ( $val > $max ) || ( isNaN($val) ) ) {
					$("#value"+$i).addClass("error");
					$founderror = true;
				}else{
					$("#value"+$i).removeClass("error");
				}

				$remaining -= $perc;
				$total -= $val * ( $perc / 100 );
			});
			$total = $total / ( $remaining / 100 );
			$total = Math.ceil( $total );
			$("#rest").text($remaining);
			$("#waiting").slideUp();
			$("#result_box").slideDown();
			if ( $founderror ) {
				$("#extra").html('<span style="color: red">You have an error.</span>');
			}else{
				$("#result").text($total);
				if ( $total > 0 ) {
					if ( $total > $max ) {
						$("#extra").html('<span style="color: red">You cannot pass!</span>');
					}else{
						$("#extra").html('');
					}
				}else{
					$("#extra").html('<span style="color: blue">You pass no matter what!</span>');
				}
			}
		}
	}
	$(".remove").live("click", function() {
		$(this).parent().remove();
	});
	$("#example").click(function() {
		$("#all").empty();
		$("#all").append(""
			+ "<li class=\"mark\" id=\"mark0\">"
			+ "<input size=\"10\" type=\"text\" class=\"percent\" id=\"percent0\" value=\"20\" />"
			+ "&#37; <input size=\"6\" type=\"text\" class=\"value\" id=\"value0\" value=\"50\" />"
			+ " <input type=\"button\" class=\"remove\" value=\"-\" />"
			+ "</li>"
			+ "<li class=\"mark\" id=\"mark1\">"
			+ "<input size=\"10\" type=\"text\" class=\"percent\" id=\"percent1\" value=\"30\" />"
			+ "&#37; <input size=\"6\" type=\"text\" class=\"value\" id=\"value1\" value=\"60\" />"
			+ " <input type=\"button\" class=\"remove\" value=\"-\" />"
			+ "</li>"
		);
		$("#least").attr("value","40");
		$("#max").attr("value","100");
	});
	function step1 () {
		$("#all").empty();
		$("#all").append(""
			+ "<li class=\"mark\" id=\"mark0\">"
			+ "<input size=\"10\" type=\"text\" class=\"percent\" id=\"percent0\" value=\"0\" />"
			+ "&#37; <input size=\"6\" type=\"text\" class=\"value\" id=\"value0\" value=\"0\" />"
			+ " <input type=\"button\" class=\"remove\" value=\"-\" />"
			+ "</li>"
			+ "<li class=\"mark\" id=\"mark1\">"
			+ "<input size=\"10\" type=\"text\" class=\"percent\" id=\"percent1\" value=\"0\" />"
			+ "&#37; <input size=\"6\" type=\"text\" class=\"value\" id=\"value1\" value=\"0\" />"
			+ " <input type=\"button\" class=\"remove\" value=\"-\" />"
			+ "</li>"
			+ "<li class=\"mark\" id=\"mark2\">"
			+ "<input size=\"10\" type=\"text\" class=\"percent\" id=\"percent2\" value=\"0\" />"
			+ "&#37; <input size=\"6\" type=\"text\" class=\"value\" id=\"value2\" value=\"0\" />"
			+ " <input type=\"button\" class=\"remove\" value=\"-\" />"
			+ "</li>"
		);
	};
	function step2() {
		step1();
		$("#percent0").attr("value","10");
		$("#percent1").attr("value","10");
		$("#percent2").attr("value","40");
	}
	function step3() {
		step2();
		$("#value0").attr("value","86");
		$("#value1").attr("value","78");
		$("#value2").attr("value","74");
	}
	function step4() {
		step3();
		$("#least").attr("value","65");
		$("#max").attr("value","100");
	}
	function step5() {
		step4();
		calc();
	}
	$("#step1").click(step1);
	$("#step2").click(step2);
	$("#step3").click(step3);
	$("#step4").click(step4);
	$("#step5").click(step5);
	$(".top").click(function() {
		var source = $(this);
		$(".top").each(function() {
			if ( $(this).attr("id") != source.attr("id") ) {
				$("#" + $(this).attr("id") + "_overlay").slideUp("fast");
			}else{
				var overlay = "#" + $(this).attr("id") + "_overlay";
				$(overlay).slideToggle();
			}
		});
	});
 });

