$(document).ready(function(){
	//ezt majd kiszedni!!!	
	$(".kerdes").css("cursor","pointer");
	$(".kerdes").click(function(){
		$(this).after('<input type="text" value="'+$(this).text().replace(/\"/g,"&quot;")+'" /><a class="kerdesok">OK</a>');
		$(this).next("input").css("width","50%");
		$(this).hide();
		$(".kerdesok").css("cursor","pointer");
		$(".kerdesok").click(function(){
			if($(this).prev("input").val() != ''){
				$(this).prev("input").prev(".kerdes").html($(this).prev("input").val());
				$(this).prev("input").prev(".kerdes").show();
				$(this).prev("input").remove();
				$(this).remove();
			}						
		});
	});
	$(".valasz").css("cursor","pointer");
	$(".valasz").click(function(){
		$(this).after('<input type="text" value="'+$(this).text().replace(/\"/g,"&quot;")+'" /><a class="valaszok">OK</a>');
		$(this).next("input").css("width","50%");
		$(this).hide();
		$(".valaszok").css("cursor","pointer");
		$(".valaszok").click(function(){
			if($(this).prev("input").val() != ''){
				$(this).prev("input").prev(".valasz").html($(this).prev("input").val());
				$(this).prev("input").prev(".valasz").show();
				$(this).prev("input").remove();
				$(this).remove();
			}						
		});
	});
	$(".ok").css("cursor","pointer");
	$(".ok").mouseover(function(){
		$(this).css("opacity",".5");
	});
	$(".ok").mouseout(function(){
		$(this).css("opacity","1");
	});
	var lekok = false;
	$(".ok").click(function(){
		var th = $(this);
		if(!lekok & th.parent(".correcting").parent(".answers").parent(".item").find("input").length == 0){
			lekok = true;
			var ansid = th.parent(".correcting").parent(".answers").parent(".item").find(".ansnr").html().replace("#K","");
			var kerdes = th.parent(".correcting").parent(".answers").parent(".item").find(".kerdes").html();
			var valaszok = new Array();
			for(i = 0; i < th.parent(".correcting").parent(".answers").parent(".item").find(".valasz").length; i++){
				valaszok[i] = th.parent(".correcting").parent(".answers").parent(".item").find(".valasz:eq("+i+")").html();
			}
			$.ajax({
				type: 'POST',
				url: "pages/ajax.mod2.php",
				data: {ANSID : ansid, KERDES : kerdes, VALASZOK : valaszok},
		    	success: function(data) {
					if(data == 'YES'){
						$(".item#K"+ansid).animate({
							"opacity":"0",
							"height":"0"
						}, function(){
							$(this).remove();
						});
					} else {
						alert('A javítás közben nem várt probléma lépett fel, próbálja meg újra!');
					}
					lekok = false;
				}
			});
		}
	});
	$(".corrector,.mistake").css("cursor","pointer");
	$(".corrector,.mistake").mouseover(function(){
		$(this).css("opacity",".5");
	});
	$(".corrector,.mistake").mouseout(function(){
		$(this).css("opacity","1");
	});
	var lekmod = false;
	$(".corrector,.mistake").click(function(){
		if(!lekmod){
			var ansid = $(this).parent(".correcting").parent(".answers").parent(".item").find(".ansnr").html().replace("#K","");
			var thclass = ($(this).attr("class") == 'corrector')?'1':'2';
			lekmod = true;
			$.ajax({
				type: 'POST',
				url: "pages/ajax.mod.php",
				data: {ANSID : ansid, ACTION : thclass},
		    	success: function(data) {
					if(data == 'YES'){
						$(".item#K"+ansid).animate({
							"opacity":"0",
							"height":"0"
						}, function(){
							$(this).remove();
						});
					} else {
						alert('Az elfogadás közben nem várt probléma lépett fel, próbálja meg újra!');
					}
					lekmod = false;
				}
			});
		}
	});
	
	//----------------
	
	$("#openTest,#openNew,#openExport").css("cursor","pointer");
	$("#openTest,#openNew,#openExport").mouseover(function(){
		$(this).css("background-color","#993333");	
		$(this).css("color","#fcf4d4");										
	});
	$("#openTest,#openNew,#openExport").mouseout(function(){
		$(this).css("background-color","");	
		$(this).css("color","");										
	});
	$("#openTest,#openNew").click(function(){
		$("."+$(this).attr("id")+"_t").show();
		$("."+$(this).attr("id")+"_h").hide();
	});
	$("#openExport").click(function(){
		window.location.href = $(this).find("span:first").attr("class");	
	});
	$(".openForm").click(function(){
		var linkto = $(this).attr("lang");
		$(this).attr("lang","");
		window.location.href = linkto;
	});
	
	var start = 0;
	$("#tester").tester();
	$("input[type=button],input[type=submit],input[type=reset],input[type=radio],label").css("cursor","pointer");
	$("label").click(function(){
		$(this).parent(".item").find("label").removeClass("selected");
		$(this).addClass("selected");
	});
	$("input[type=radio]").click(function(){
		$(this).parent(".item").find("label").removeClass("selected");
		$(this).prev("label").addClass("selected");
	});
	$("#send").click(function(){
		var missed = "";
		for(i = 0; i < $(".item").length; i++){
			if($(".item:eq("+i+")").find("input[type=radio]:checked").length == 0){
				missed += $(".questionDiv:eq("+i+")").children(".question").html()+"\n";
				$(".item:eq("+i+")").css("background-color","red");
			} else {
				$(".item:eq("+i+")").css("background-color","");
			}
		}
		if(missed){
			//alert("Az alábbi kérdésekre nem válaszolt:\n\n"+missed);
			missed = missed.split("\n");
			for(i = 0; i < missed.length; i++){
				if(missed[i] != ''){
				}
			}
			return false;
		}
	});
	$("#reset").click(function(){		
		for(i = 0; i < $(".item").length; i++){
			$(".item").css("background-color","");
		}
	});
	var lekerdezes = false;
	var openQ = false;
	$(".answer").click(function(){
		$("#"+$(this).next("label").attr("for")).attr("checked",true);
		$(this).parent(".item").find(".answer").removeClass("answeract");
		$(this).addClass("answeract");
		if(!openQ){
			start++;
			openQ = true;
			if($("#tester").find(".item").length > start){
				var th =  $(this).parent(".item");
				th.animate({
					"opacity":".0"	
				}, function(){
					th.hide();
					$(".item:eq("+start+")").css("opacity","0");
					$(".item:eq("+start+")").show();
					$(".item:eq("+start+")").animate({
						"opacity":"1" 
					});
					openQ = false;
				});
			} else {
				var th =  $(this).parent(".item");
				th.animate({
					"opacity":".0"	
				}, function(){
					th.hide();
					var nrs = new Array();
					var vals = new Array();
					var ids = new Array();
					for(i = 0; i < $("#tester").find(".item").length; i++){
						nrs[i] = (i+1);
						vals[i] = $("#tester").find(".item:eq("+i+")").find("input[type=radio]:checked").val();
						ids[i] = $("#tester").find(".item:eq("+i+")").find("input[type=radio]:checked").attr("name");
					}
					if(!lekerdezes){
						lekerdezes = true;
						$.ajax({
							type: 'POST',
				    		url: "pages/ajax.analis.php",
							data: {IDS : ids, NRS : nrs, VALS : vals},
		    				success: function(data) {
								$("#tester").html(data);
								$(".wrong").wrong();
							}
						});
					}
					openQ = false;
				});
			}
		}
	});
});

(function($){
   	$.fn.wrong = function(){
		$(this).css("cursor","pointer");
		$(this).mouseover(function(){
			$(this).css("background-color","#999999");
		});
		$(this).mouseout(function(){
			$(this).css("background-color","");
		});
		$(this).click(function(){
			$(".comments").html('<span class="comments_title"><a class="close"><img src="img/close.png" width="20" height="20" alt="close" /></a></span><span class="comments_answers"></span>');
			$(".comments").find(".comments_title").append($(this).find("input[type=hidden]:eq(0)").val());
			for(i = 1; i < $(this).find("input[type=hidden]").length; i++){
				$(".comments").find(".comments_answers").append($(this).find("input[type=hidden]:eq("+i+")").val()+'<br />');
			}
			$(".comments").css("opacity",".0");
			$(".comments").show();
			$(".comments").animate({
				"opacity" : "1"
			}); 
			$(".close").mouseover(function(){
				$(this).css("opacity",".5");							
			});
			$(".close").mouseout(function(){
				$(this).css("opacity","1");							
			});
			$(".close").click(function(){
				$(".comments").animate({
					"opacity" : ".0"
				}); 
				$(".comments").hide();
			});
		});
		/*$(".comments").mouseout(function(){
			if($(this).css("display") != 'none'){
				$(this).hide();
			}
		});*/
	}
	$.fn.tester = function(){
		//<input class="answer" type="button" value="'.substr(ANSWERS,$j,1).'" />
		$(this).find(".item:gt(0)").hide();
		$(this).find("input[type=button],input[type=reset],input[type=submit]").remove();
		for(i = 0; i < $(this).find(".item").length; i++){
			for(j = 0; j < $(this).find(".item:eq("+i+")").find("label").length; j++){
				var lab = $(this).find(".item:eq("+i+")").find("label:eq("+j+")");
				var ans = $("#"+lab.attr("for"));
				$(this).find(".item:eq("+i+")").find(".answers").append('<span class="content"><span class="nr">'+ans.val()+': </span>'+lab.html()+'</span>');
				lab.before('<input class="answer" type="button" value="'+ans.val()+'" />');
				lab.hide();
				ans.hide();
				ans.next("br").hide();
			}
		}
	}
})(jQuery);
