// 2010 09 25
var T1 = new Date(2010, 8, 25);
//var T2 = new Date(2009, 8, 19);
 

$(document).ready(function(){
		$("#content").corner();
		$("#right-cats").corner();
		$(".question-count").corner();
		$("#weekly-questions").corner();

		$("#_T1").countdown({until: T1, format: 'd', layout: '{dn}'});
		//$("#_T2").countdown({until: T2, format: 'd', layout: '{dn}'});

		$("#countdown-outer").click(function() { 
				$("#countdown").slideToggle('normal');
				
			});

		// to nie uzywane
		//$("#countdown-outer").mouseleave(function() { 
		//		$("#countdown").slideUp('normal');		
		//	});

		$("#reset-partial-stat-tooltip").bt('Poniżej, w kolumnie znajdują się (jeśli były już rozwiązywane pytania) ikony resetowania statystyk danej kategorii. Użycie opcji resetowania statystyk poszczególnych kategorii umożliwia ponowne rozwiązywanie pytań z danej kategorii.',
											{
												fill: '#FFF',
													trigger: 'hover',
													width:400,
													cornerRadius: 10,
													strokeWidth: 0,
													shadow: true,
													shadowOffsetX: 3,
													shadowOffsetY: 3,
													shadowBlur: 8,
													shadowColor: 'rgba(0,0,0,.9)',
													shadowOverlap: false,
													noShadowOpts: {strokeStyle: '#999', strokeWidth: 2},
													positions: ['left']
													}); 

	});
	    

function answerClick(checklink, obj) {

	$(".a-btn").each(function(index, elem) { 
			if(elem == obj) {
				$(elem).attr("value", "sprawdzam...")
			}
			$(elem).attr("disabled", "disabled");
		});

	$.getJSON(checklink, function(data, status) { 
			if (status != 'success') {
				alert("Wystąpił błąd. Odśwież stronę i spróbuj ponownie.");
				return;
			}
			
			$(obj).attr("value", "sprawdzono");

			if (data['result'] == 1) {
				getAnswerTextArea( $(obj) ).css("color", "#33cc00");

			}else {
				getAnswerTextArea( $(obj) ).css("color", "red");
				getAnswerTextArea( $("#ans_"+data['correctAnswer']) ).css("color", "#33cc00");
			}

			if (data['info'] == "normal") {
				normalTest(data);
				
			}else if (data['info'] == 'repeat') {
				repeatTest(data);

			}else if (data['info'] == 'quickly_questions') {
				quicklyQuestionsTest(data)
			}

		});
}



function normalTest(data) {
	$("#stat-all").text(data['allCount']);
	$("#stat-done").text(data['doneCount']);
	$("#stat-pass").text(data['passPercString']);
	$("#stat-failure").text(data['failurePercString']);
	
	$("#deeds").html(data["deeds"]);
	
	if (data['allCount'] > data['doneCount']) {
		$("#next-btn").attr("alt", data['nextLink']).css("display", "inline");
			
	}else {
		$("#finish-info").css("display", "inline");
	
	}

}


function repeatTest(data) {
	$("#stat-all").text(data['allCount']);

	$("#deeds").html(data["deeds"]);

	if (data['allCount'] > 0) {
		$("#next-btn").attr("alt", data['nextLink']).css("display", "inline");
			
	}else {
		$("#finish-info").css("display", "inline");
	
	}

}



function quicklyQuestionsTest(data) {
	$("#stat-all").text(data['allCount']);
	$("#stat-done").text(data['doneCount']);
	$("#stat-pass").text(data['passPercString']);
	$("#stat-failure").text(data['failurePercString']);
	
	$("#deeds").html(data["deeds"]);
	
	if (data['allCount'] > data['doneCount']) {
		$("#next-btn").attr("alt", data['nextLink']).css("display", "inline");
			
	}else {
		$("#finish-info").css("display", "inline");
	
	}
}




function getAnswerTextArea(jq_obj) {
	return jq_obj.parent().prev();
}


function cancelClick() {
	if ($("#finish-info").css("display") == "inline") {
		
		$("#cancel-btn").attr("disabled", "disabled");
		$("#cancel-btn").attr("value", "zamykanie...");
		window.location = getClearUrl(String(window.location));
		return;
	}


	var t = confirm("Czy na pewno chcesz przerwać odpowiadanie na pytania?");
	if (t == true) {
		$("next-btn").attr("disabled", "disabled");
		$("#cancel-btn").attr("disabled", "disabled");
		$("#cancel-btn").attr("value", "zamykanie...");
		window.location = getClearUrl(String(window.location));
	}
	
}


function getClearUrl(url) {
	var i = url.indexOf("?");
	if (i != -1)
		return url.substring(0, i);
	else
		return url;
	
}




function showNext(obj) {
	var url = $(obj).attr("alt");
	$(obj).attr("value", "pobieranie...");
	$(obj).attr("disabled", "disabled");
	tb_show(null, url, false);
	$(obj).blur();
}




function switcher(divid, obj) {
	obj = $(obj);
	var div = $($("#"+divid)[0]);
	if (div.css("display") == "none") {
		div.css("display", "block");
		obj.text("zwiń listę");

	}else {
		div.css("display", "none");
		obj.text("rozwiń listę");
	}

}

